【问题标题】:How to configure maven to use different log4j.properties files in different environments如何配置maven在不同环境下使用不同的log4j.properties文件
【发布时间】:2012-03-03 03:39:46
【问题描述】:

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

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

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

【问题讨论】:

  • 你使用 maven-release-plugin 管理生产构建吗?
  • 不,我以前从未对 maven-release-plugin 感兴趣。还是谢谢,我会看看它是否可以解决这个问题。
  • 这是一个反复出现的安静问题,我想加两分钱。客户通常不接受为不同环境创建多个构建。他们希望为所有环境构建一个版本,以消除多个构建的任何回归问题。 log4j 文件应该在您的应用程序之外声明,就像任何任何特定于环境的变量一样。 Maven 构建配置文件似乎总是迷失“构建一次部署到任何地方”的理念。
  • @tom 我热衷于改进如何为所有环境进行一次构建。我的想法是我们的​​构建服务器在完成之前为所有区域/环境构建工件的特定执行。你所说的我的问题是排除了特定于环境的变量——我们打包的工件应该是完整的并且可以部署,不是吗?
  • @Crowie 我们的工件已准备好部署,但始终有一个每个环境的配置文件,它是 log4j 文件或属性文件。这些文件在应用程序工件之外但在应用程序服务器内配置。

标签: maven log4j


【解决方案1】:

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

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

【讨论】:

  • 嗨。资源元素只需要多一点爱:&lt;resource&gt;&lt;directory&gt;someResourceDir&lt;/directory&gt; &lt;includes&gt; &lt;include&gt;path_to_file_A&lt;/include&gt; &lt;/includes&gt;&lt;/resource&gt;
  • 只是一个简单的问题:copy-resources 是“正常”maven lifecylce 的一部分还是插件附带的?
  • @al.您必须明确指定它。 resources:resources 是 Maven 生命周期的一部分。
【解决方案2】:
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

【讨论】:

  • 添加:默认
  • 资源使 pom 配置过载,因此如果您有其他文件或目录,则必须定义所有 :(
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    对我来说最简单的方法,

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

    所以,

    在生产中它只是使用 log4j.xml 和你的开发 log4j_dev.xml

    • 为了防止出现问题,最好也为生产创建 ENV 变量为 _pro,这样对于生产 log4j_pro.xml,将使用开发 log4j_dev.xml。

    我相信依赖不同的文件而不是复制资源是更好的做法。

    【讨论】:

      【解决方案5】:

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

      如果你有这样的目录树:


      ...

      你应该有以下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 时,使用来自 resources/dev 的配置文件 log4j.properties。当您使用任何其他配置文件或根本没有配置文件时,将使用来自resources/prodlog4j.properties。所以你的*.jar 应该是这样的:


      当然如果你有不同的资源位置,例如main/java/resources/...,你应该指定它而不是resources/...

      【讨论】:

        【解决方案6】:

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

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

        【讨论】:

        • 这在我的情况下不起作用。您是否安装了任何插件或模块以使其正常工作?
        猜你喜欢
        • 1970-01-01
        • 2016-04-27
        • 2022-06-19
        • 2015-10-07
        • 1970-01-01
        • 2018-06-18
        • 2018-06-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多