简体   繁体   中英

Log4j.properties not found using maven resources folder

I try to load a configuration for log4j using the PropertyConfigurator.configure("log4j.properties") method but I keep running into java.io.FileNotFoundException .

I followed this question and placed my log4j.properties file into the resources folder.

I Also edited my pom.xml like this :

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <targetPath>${project.build.directory}</targetPath>
            <includes>
                <include>log4j.properties</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>src.Main</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

So when I run mvn package the target folder generated contains my .jar and my log4j.properties side by side but when I run my jar I get the file not found exception.

How can I fix this issue ?

Please, DO NOT include the log4j.properties into the final Jar file, it will cause multiple log4j.properties files in the classpath, if someone is depending on your Jar, you may accidentally override their logging configurations, depends which Jar is loaded first. 请不要将log4j.properties包含在最终的Jar文件中,否则将导致类路径中有多个log4j.properties文件,如果有人依赖您的Jar,则可能会意外覆盖其日志记录配置,具体取决于加载了哪个Jar第一。

For reference : https://www.mkyong.com/maven/maven-exclude-log4j-properties-in-jar-file/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM