Java 使用maven资源文件夹找不到Log4j.properties

Java 使用maven资源文件夹找不到Log4j.properties,java,maven,logging,log4j,Java,Maven,Logging,Log4j,我尝试使用PropertyConfigurator.configure(“log4j.properties”)方法加载log4j的配置,但我一直遇到java.io.FileNotFoundException 我随后将我的log4j.properties文件放入resources文件夹 我还编辑了pom.xml,如下所示: <build> <resources> <resource> <directory&g

我尝试使用
PropertyConfigurator.configure(“log4j.properties”)
方法加载log4j的配置,但我一直遇到
java.io.FileNotFoundException

我随后将我的
log4j.properties
文件放入
resources
文件夹

我还编辑了pom.xml,如下所示:

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

src/main/resources
真的
${project.build.directory}
log4j.properties
org.apache.maven.plugins
maven阴影插件
2.1
包裹
阴凉处
主控室
因此,当我运行
mvn package
时,生成的目标文件夹同时包含my.jar和我的log4j.properties,但是当我运行我的jar时,我得到了文件notfound异常

如何解决此问题?

注意: 请不要将log4j.properties包含到最终的Jar文件中,这将导致类路径中有多个log4j.properties文件,如果有人依赖于您的Jar,您可能会意外地覆盖他们的日志配置,这取决于首先加载哪个Jar

供参考: