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

问题描述 投票:20回答:6

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

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

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

maven log4j
6个回答
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

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>

2
投票

如果您有一个简单的环境,则不需要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的清洁。


2
投票

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

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

1
投票

对我来说最简单的方式,

  • 定义系统变量ENV并为开发环境设置其值_dev。
  • 你引用这个文件的地方就像这个log4j $ {ENV} .properties一样

所以,

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

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

我认为依靠不同的文件而不是复制资源是更好的做法。


0
投票

对于使用jar包装的小型项目,有一个非常简单的解决方案(我没有在war打包项目上测试过)。唯一的缺点是你必须复制所有资源,但如果你唯一的资源是log4j.properties,这不是问题。

如果您有这样的目录树:

log4j.properties maven profile project structure ...

enter image description here

你应该有以下pom:

<build>
    <finalName>${project.artifactId}</finalName>
    <sourceDirectory>src/</sourceDirectory>
    <resources>
        <resource>
            <directory>${resources.path}</directory>
        </resource>
    </resources>
</build>

<profiles>
    <profile>
        <id>default</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <resources.path>resources/prod</resources.path>
        </properties>
    </profile>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <resources.path>resources/dev</resources.path>
        </properties>
    </profile>
</profiles>

然后当你使用dev时,使用来自log4j.propertiesresources/dev。当您使用任何其他配置文件或根本没有配置文件时,使用来自log4j.propertiesresources/prod。所以你的*.jar应该是这样的:

log4j.properties maven profile project jar directory structure 当然,如果你有不同的资源位置,例如main/java/resources/...,你应该指定它而不是resources/...

© www.soinside.com 2019 - 2024. All rights reserved.