Where does maven search for log4j.properties file?

10,790

Solution 1

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.

Solution 2

There's a lot of answers relating to the popular solution src/main/resources or the src/test/resources but generally not having the log4j.properties in your application can be useful. Rather than providing a hardcoded log4j.properties in your code, leave it to the client (app-server, stage environment, etc) to configure the desired logging. So, I would suggest to be cautious on the specifics of your deployment to make the decision of having the log4j.properties included or excluded of your maven assembly.

Reference: If using maven, usually you put log4j.properties under java or resources?

Share:
10,790
Paras
Author by

Paras

Updated on June 05, 2022

Comments

  • Paras
    Paras almost 2 years

    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.

    enter image description here

    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?