首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将maven配置为在不同环境中使用不同的log4j.properties文件

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

Stack Overflow用户
提问于 2012-03-03 11:39:47
回答 5查看 21.3K关注 0票数 23

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

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

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

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2015-10-01 23:24:32

代码语言:javascript
复制
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
票数 8
EN

Stack Overflow用户

发布于 2012-03-03 16:27:51

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

代码语言:javascript
复制
<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>
票数 11
EN

Stack Overflow用户

发布于 2013-03-29 02:00:35

如果你有一个简单的环境,你不需要maven-resources-plugin。

在本例中,log4j.properties B是用于生产的文件,位于src/main/java目录中;log4j.properties A是用于开发的文件,位于/Users/junger/.m2/目录中。

在您的pom.xml中:

代码语言:javascript
复制
<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中(如果不存在则创建一个):

代码语言:javascript
复制
<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的整洁。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9543219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档