7

I am trying to use log4j in a project I am executing with the exec-maven-plugin. I have tried placing the file in the following locations:

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

For all locations of the file, I am getting the following message when executing the code:

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

Where should the log4j.properties file be located?


exec-maven-plugin config:

...
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2</version>
    <configuration>
        <mainClass>mypackage.Main</mainClass>
    </configuration>
</plugin>
...
Armand
  • 23,463
  • 20
  • 90
  • 119

3 Answers3

2

You have two choices:

  1. Put the property file under

./src/main/resources/log4j.xml

  1. You can specify it from the command line:

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

stones333
  • 8,588
  • 1
  • 25
  • 27
0

I'm not sure this is the right way (because I don't know this plugin), but you may use the configuration of the plugin to specify to the classpath of the VM the right location of your file :

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

What is the purpose of using such a plugin ? If this is in order to test your app, you should probably use maven-dependency-plugin : http://www.sonatype.com/books/mvnex-book/reference/customizing-sect-custom-packaged.html

You can find more info here : maven jar-with-dependencies log4j

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

Regards

Community
  • 1
  • 1
Jean-Rémy Revy
  • 5,607
  • 3
  • 39
  • 65
0

Actually, log4j has provided command line option support for locating configuration file manually. However it is used for java command itself, and you could use it in Maven building through -Dexec.args option to pass java options directly:

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

Besides, arguments in exec.args could also be written persistently into Mojo's <arguments> section, 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>
千木郷
  • 1,595
  • 2
  • 19
  • 30