【问题标题】:Where should log4j.properties be located when using Maven Exec Plugin?使用 Maven Exec 插件时 log4j.properties 应该放在哪里?
【发布时间】:2011-09-27 08:45:01
【问题描述】:

我正在尝试在使用 exec-maven-plugin 执行的项目中使用 log4j。我已尝试将文件放置在以下位置:

$PROJECT_HOME
$PROJECT_HOME/src/main/resources
$PROJECT_HOME/target
$PROJECT_HOME/target/classes

对于文件的所有位置,我在执行代码时收到以下消息:

log4j:WARN No appenders could be found for logger (mypacakge.MyClass).
log4j:WARN Please initialize the log4j system properly.

log4j.properties 文件应该放在哪里?


exec-maven-plugin 配置:

...
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2</version>
    <configuration>
        <mainClass>mypackage.Main</mainClass>
    </configuration>
</plugin>
...

【问题讨论】:

  • 您能否附加您的exec-maven-plugin 配置。
  • exec插件绑定到哪个执行阶段?

标签: maven-2 configuration log4j


【解决方案1】:

你有两个选择:

  1. 把属性文件放到

./src/main/resources/log4j.xml

  1. 您可以从命令行指定它:

$ mvn compile exec:java -Dexec.classpathScope=compile -Dexec.mainClass=com.lei.java.sample.Test -Dlog4j.configuration=file:./log4j.xml

【讨论】:

    【解决方案2】:

    我不确定这是不是正确的方法(因为我不知道这个插件),但你可以使用插件的配置来为 VM 的类路径指定文件的正确位置:

    <configuration>
       <executable>maven</executable>
       <!-- optional -->
       <workingDirectory>/tmp</workingDirectory>
       <arguments>
           <argument>-classpath</argument>
           <argument>/path/to/dirthatcontainslog4J</argument>
          ...
       </arguments>
    </configuration>
    

    使用这样的插件的目的是什么? 如果这是为了测试您的应用程序,您可能应该使用 maven-dependency-plugin :http://www.sonatype.com/books/mvnex-book/reference/customizing-sect-custom-packaged.html

    您可以在此处找到更多信息: maven jar-with-dependencies log4j

    How can I create an executable JAR with dependencies using Maven?

    问候

    【讨论】:

      【解决方案3】:

      实际上,log4j 已经提供了手动定位配置文件的命令行选项支持。但是它用于java 命令本身,您可以通过-Dexec.args 选项在Maven 构建中使用它来直接传递java 选项:

      $ mvn -Dexec.args="-Dlog4j.configurationFile='./log4j2.xml' -classpath /previously/configured/path" org.codehaus.mojo:exec-maven-plugin:1.2.1:exec
      

      此外,exec.args 中的参数也可以持久写入 Mojo 的 &lt;arguments&gt; 部分,as said in document

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.6.0</version>
        <executions>
          ...
        </executions>
        <configuration>
          <executable>maven</executable>
          <arguments>
            <argument>-Dlog4j.configurationFile="./log4j2.xml"</argument>
            ...
          </arguments>
        </configuration>
      </plugin>
      

      【讨论】:

        猜你喜欢
        • 2010-12-01
        • 2013-03-09
        • 2020-09-02
        • 2014-09-11
        • 1970-01-01
        • 1970-01-01
        • 2019-04-30
        • 1970-01-01
        • 2018-01-18
        相关资源
        最近更新 更多