编译失败:找不到模块:org.apache.logging.log4j

时间:2018-11-15 19:31:52

标签: java maven log4j2 java-module java-11

我有一个用Java 11编写的简单应用程序。mvn clean verify(maven 3.6.0)执行错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project parser: Compilation failure
[ERROR] ...src/main/java/module-info.java:[2,32] module not found: org.apache.logging.log4j  

依赖项:

<log4j.version>2.11.1</log4j.version>

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>${log4j.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>${log4j.version}</version>
</dependency>

Module-info.java:

module abc {
    requires org.apache.logging.log4j;
}

Log4j2配置为默认配置,位于.xml文件中。用法:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

private static final Logger logger = LogManager.getLogger(Abc.class); 

logger.info("Boom!");

我在stackoverflow上尝试了所有相关问题,但均未成功。

2 个答案:

答案 0 :(得分:2)

您应升级到maven-compiler-plugin:3.8.0,并将版本指定为11

使用log4j-api.jar描述--release 11时,原因与指令中的名称相同。

jar --file=.../.m2/repository/org/apache/logging/log4j/log4j-api/2.11.1/log4j-api-2.11.1.jar --describe-module --release 11
releases: 9

org.apache.logging.log4j jar:file://.../.m2/repository/org/apache/logging/log4j/log4j-api/2.11.1/log4j-api-2.11.1.jar/!META-INF/versions/9/module-info.class
exports org.apache.logging.log4j
exports org.apache.logging.log4j.message
exports org.apache.logging.log4j.simple
exports org.apache.logging.log4j.spi
exports org.apache.logging.log4j.status
exports org.apache.logging.log4j.util
requires java.base mandated
uses org.apache.logging.log4j.message.ThreadDumpMessage$ThreadInfoFactory
uses org.apache.logging.log4j.spi.Provider
uses org.apache.logging.log4j.util.PropertySource

这主要是因为log4j-api是一个模块化jar

另一方面,log4j-core是作为自动模块派生的,仍然在其 MANIFEST.MF 中被

覆盖。
org.apache.logging.log4j.core

答案 1 :(得分:0)

尽管更改了@Naman 提到的更新编译器插件,但如果您使用 maven javadoc 插件生成带有诸如“mvn clean install javadoc:javadoc”之类的命令的 javadoc,则可能会发生类似的错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-javadoc-plugin:3.2.0:javadoc (default-cli) on project data-exchange-common-configuration: An error has occurred in Javadoc report generation:       
[ERROR] Exit code: 1 - your.module\src\main\java\module-info.java:20: error: module not found: org.apache.logging.log4j          
[ERROR]   requires org.apache.logging.log4j;                                                                                                                                                                      
[ERROR]                              ^                                                                                                                                                                            
[ERROR]                                                                                                                                                                                                           
[ERROR] Command line was: cmd.exe /X /C "C:\Programs\Java\Oracle\jdk-11.0.6\bin\javadoc.exe @options @packages @argfile"                                                                                          
[ERROR]                                                                                                                                                                                                           
[ERROR] Refer to the generated Javadoc files in 'C:\Source\dex\dex1\ais-data-exchange\data-exchange-common\data-exchange-common-configuration\target\site\apidocs' dir.                                           

现在的解决方案(直到问题在插件级别得到解决)是生成 api 文档,将 source 版本设置为 java 8(同时将编译器版本保持为 java 9+):< /p>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <source>8</source>
                    <detectJavaApiLink>false</detectJavaApiLink>
                </configuration>
            </plugin>
            

在上面的例子中,请看到我还设置了 false,这是由于在 java 8 中生成源代码时存在的一些其他错误。无论如何将 source 参数设置为 8 和 detectJavaApiLink 解决了问题。