I am facing issues using log4j with Maven. I’ve one properties file i.e log4j.properties
and I’ve put that file at the same path where project’s pom.xml
is stored.
pom.xml
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> <scope>test</scope> </dependency>
I’ve used log4j in my code which is under test
folder.
Code
package com.example.automationtest; import org.apache.log4j.*; import org.junit.Test; public class AppTest { @Test public void testLogger(){ //PropertyConfigurator.configure("C:\Users\Desktop\AutomationTest\log4j.properties"); Logger log = Logger.getLogger("exampleLogger"); log.debug("Hello, World!"); } }
I would like to know, how does maven identify where the log4j.properties file is located?
In the above scenario if I run the command mvn test
it gives a warning, please check the screenshot below for warning messages.
so as a workaround I am providing the complete path of log4j.properties in my code. I’ve used below line:
PropertyConfigurator.configure("C:\Users\Desktop\AutomationTest\log4j.properties")
Is it necessary to use the above line of code, or is there any specific directory where maven looks for log4j.properties
file?
Advertisement
Answer
The file needs to go into src/main/resources/
or src/test/resources/
(if you need it only for unit tests).
Longer explanation: Maven splits the Java classpath into source code and resources (non-Java things like property files, images, etc). The main reason for this is that Maven can do filtering of resources for you and to make this extra safe, they put the resources into a different folder than the sources.