我希望能够对不同的环境使用不同的log4j配置。
在我的开发环境中,我想使用log4j.properties (A)。但是当我为生产环境构建Maven时,我想使用log4j.properties (B)。
请告诉我如何在我的pom.xml中配置它?
发布于 2015-10-01 23:24:32
1. in your project add 3 folders :
Your Project\src\main\resources\
\A > log4j.properties
\B > log4j.properties
\Default > log4j.properties
2. in pom.xml
<properties>
<param>Default</param>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources/${param}</directory>
</resource>
</resources>
</build>
3.
- if : mvn clean install : classpath => log4j.properties(Default)
- if : mvn clean install -Dparam=A : classpath => log4j.properties(A)
- if : mvn clean install -Dparam=B : classpath => log4j.properties(B)
> much better than using profiles is more extensible without touching the pom
发布于 2012-03-03 16:27:51
您可以使用配置文件来实现所需的行为:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>log4j</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>output_directory</outputDirectory>
<resources>
<resource>${log4j.file}</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<log4j.file>path_to_file_A</log4j.file>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<log4j.file>path_to_file_B</log4j.file>
</properties>
</profile>
</profiles>
发布于 2013-03-29 02:00:35
如果你有一个简单的环境,你不需要maven-resources-plugin。
在本例中,log4j.properties B
是用于生产的文件,位于src/main/java
目录中;log4j.properties A
是用于开发的文件,位于/Users/junger/.m2/
目录中。
在您的pom.xml中:
<properties>
<log4j.properties.directory>src/main/java</log4j.properties.directory>
</properties>
<build>
<resources>
<resource>
<directory>${log4j.properties.directory}</directory>
<includes>
<include>log4j.properties</include>
</includes>
</resource>
</resources>
</build>
现在,在您的/Users/junger/.m2/settings.xml中(如果不存在则创建一个):
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<log4j.properties.directory>/Users/devuser/.m2/</log4j.properties.directory>
</properties>
</profile>
</profile>
通过使用此方法,每个开发人员可以拥有不同的log4j.properties目录,并且您可以保持pom.xml的整洁。
https://stackoverflow.com/questions/9543219
复制相似问题