Where should log4j.properties be located when using Maven Exec Plugin?

I am trying to use log4j in a project that I am executing with exec-maven-plugin. I tried to place the file in the following places:

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

For all file locations, I get the following message when I execute 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 log4j.properties ?


Exec-maven-plugin configuration:

 ... <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2</version> <configuration> <mainClass>mypackage.Main</mainClass> </configuration> </plugin> ... 
+6
source share
3 answers

You have two options:

  • Put the properties file in

./SRC/home/resources/log4j.xml

  • You can specify it on the command line:

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

+1
source

I'm not sure if this is the right way (because I don’t know this plugin), but you can use the plugin configuration to specify the path to the virtual machine classes in the right place 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 necessary to test your application, you should probably use the maven-dependency-plugin: http://www.sonatype.com/books/mvnex-book/reference/customizing-sect-custom-packaged.html

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

How to create an executable dependency JAR using Maven?

Hi

0
source

Actually, log4j provided command line options support for locating the configuration file locally. However, it is used for the java command itself, and you can use it in Maven with the -Dexec.args option to directly access java options:

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

In addition, arguments in exec.args can also be persistently written to the Mojo <arguments> section, as the document says :

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

Source: https://habr.com/ru/post/898133/


All Articles