繁体   English   中英

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

[英]Log4j.properties not found using maven resources folder

我尝试使用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>

因此,当我运行mvn package ,生成的目标文件夹并排包含我的.jar和log4j.properties,但是当我运行我的jar时,出现找不到文件的异常。

如何解决此问题?

注意:请不要将log4j.properties包含在最终的Jar文件中,否则将导致类路径中有多个log4j.properties文件,如果有人依赖您的Jar,则可能会意外覆盖其日志记录配置,具体取决于加载了哪个Jar第一。

供参考: https : //www.mkyong.com/maven/maven-exclude-log4j-properties-in-jar-file/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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