【问题标题】:Log4j.properties not found using maven resources folder使用 maven 资源文件夹找不到 Log4j.properties
【发布时间】:2017-10-17 06:16:33
【问题描述】:

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

我关注this question 并将我的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 时,我得到了找不到文件的异常。

我该如何解决这个问题?

【问题讨论】:

    标签: java maven logging log4j


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-08
      • 2011-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多