IntelliJ IDEA 在 Maven 构建后找不到 log4j2.xml

IntelliJ IDEA won't find log4j2.xml after maven build

我在 /src/main/java/resources/log4j2.xml 中有一个带有 log4j2 (2.9.0) 和配置文件的 Maven 项目。只要我留在 IntelliJ 中,它就可以正常工作,并且它会像我配置的那样进行记录。

如果我现在在终端中创建一个 mvn clean install (Ubuntu 14) 然后在 IntelliJ 中启动我的程序的主要方法,我会得到一个错误,它找不到log4j2 配置文件:

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'log4j2.debug' to show Log4j2 internal initialization logging.

只要我在 IntelliJ IDEA 中创建 Build -> Rebuild Project 它就会再次工作。

我真的不明白外部 maven 构建如何使 IntelliJ IDEA 不接受本地源代码中的 .xml。

有什么想法吗?

我找到了解决方案。

IntelliJ 将默认情况下的资源复制到 /target/classes,这是 IntelliJ 实际上从中获取 log4j2.xml 的地方。我的 Maven 构建没有这样做并且 IntelliJ 没有重建项目,所以它找不到 log4j2.xml

我的解决方案是将 log4j2.xml 放在 pom 的资源部分:

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <targetPath>${project.build.directory}/classes</targetPath>
            <includes>
                <include>**/log4j2.xml</include>
            </includes>
        </resource>
    </resources>