如何配置maven以在不同环境中使用不同的log4j.properties文件

时间:2012-03-03 03:39:47

标签: maven log4j

我希望能够针对不同的环境使用不同的log4j配置。

在我的开发环境中,我想使用log4j.properties(A)。但是当我在Maven中为生产环境构建时,我想使用log4j.properties(B)。

请告诉我如何在我的pom.xml中配置它?

5 个答案:

答案 0 :(得分:11)

您可以使用配置文件来实现所需的行为:

<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>

答案 1 :(得分:5)

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

答案 2 :(得分:2)

如果您的环境很简单,则不需要maven-resources-plugin。

在此示例中,log4j.properties B是您用于生产的文件,位于src/main/java目录中,log4j.properties A是您用于开发的文件,位于{{1}目录中}}

在你的pom.xml中:

/Users/junger/.m2/

现在,在/Users/junger/.m2/settings.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>

通过使用此方法,每个开发人员可以拥有不同的log4j.properties目录,并保持pom.xml的清洁。

答案 3 :(得分:2)

在某种程度上,您可以引用log4j.properties中的环境变量来添加依赖于环境的行为。 例如

log4j.rootLogger=${rootLoggerLevel}, ${appender}

答案 4 :(得分:1)

对我来说最简单的方法,

  • 定义系统变量ENV并为开发环境设置其值_dev。
  • 您引用此文件的位置请使用此log4j $ {ENV} .properties

所以,

在生产中,它只使用log4j.xml和dev log4j_dev.xml

  • 为了防止出现问题,最好还为生产创建ENV变量作为_pro,因此对于生产log4j_pro.xml,将使用dev log4j_dev.xml。

我认为依靠不同的文件比复制资源更好。

相关问题