【发布时间】:2014-06-29 13:44:55
【问题描述】:
我使用 Spring 和 Maven 构建我的项目,并且有两个文件位于 src/main/resources 路径(application.properties、log4j.properties)。如果我在 IDE 中打开我的项目并运行它,一切正常,可以更改 application.properties 文件中的值并使用更改的值再次运行项目。但是当我运行命令 mvn clean install 时,是的,我得到了 .jar 文件,但我无法使用 .properties 文件配置这个 .jar。此 .jar 在构建之前使用属性文件中的值。
我不知道这是否重要,但这是我在 Spring 应用程序上下文中定义属性文件的方式。
@PropertySource({"application.properties", "log4j.properties"})
public class AppConfig {
我将这些插件放在 pom.xml 文件中。可以吗?你能帮我吗?看起来构建的 .jar 项目不知道同一目录级别的 .properties 文件。
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<!-- nothing here -->
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>cz.cvut.fit.bouchja1.ensemble.main.EnsembleApp</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path> <!-- HERE IS THE IMPORTANT BIT -->
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
【问题讨论】:
-
如果您离开 Class-Path 会发生什么?
-
你的意思是我删除了它吗?我会试试的。
-
您是否尝试为@PropertySource 指定文件路径?我认为这会起作用:@PropertySource({"classpath:application.properties", "classpath:log4j.properties"})
-
@gerardribas 不幸的是:(
标签: java spring maven properties