Java无法读取与.jar文件位于同一目录中的外部属性

时间:2019-03-20 10:20:53

标签: spring maven spring-boot plugins

我创建一个jar应用程序,并使用maven-shade-plugin对其进行打包。 其中包含一些属性文件,我使用PropertyPlaceholderConfigurer读取属性:

   <bean id="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:/properties/core.properties</value>
        </list>
    </property>
</bean>

因为我想灵活更新文件中的属性,所以我这样做是为了在打包时排除属性:

        <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*</include>
            </includes>
            <excludes>
                <exclude>properties/core.properties</exclude>
            </excludes>
        </resource>
    </resources>

打包jar之后,我创建一个包含core.properties的属性目录,并将其与jar文件放在同一目录中。 当我运行jar文件时:

java -jar test.jar

它可以读取core.properties文件中的属性。

但是,如果我创建一个springboot应用程序并执行相同的步骤,则使用spring-boot-maven-plugin打包。 然后使用相同的命令运行jar:

java -jar test.jar

发生错误:

Caused by: java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180)
at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:159)
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:99)
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:73)
at org.springframework.core.io.support.PropertiesLoaderUtils.loadProperties(PropertiesLoaderUtils.java:59)
at org.springframework.core.io.support.ResourcePropertySource.<init>(ResourcePropertySource.java:67)

0 个答案:

没有答案