简体   繁体   中英

Compilation failure: module not found: org.apache.logging.log4j

I have simple application written in Java 11. mvn clean verify (maven 3.6.0) executes with error:

[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  

Dependencies:

<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 configuration is default and in .xml file. Usage:

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

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

logger.info("Boom!");

I tried all related questions on stackoverflow with no success.

You shall upgrade to the maven-compiler-plugin:3.8.0 and specify the release as 11 .

Reason being, on describing the log4j-api.jar using --release 11 states the same name as in your directive.

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

which is primarily because the log4j-api is a modular jar .

On the other hand, log4j-core is derived as an automatic module still which is overridden in its MANIFEST.MF as

org.apache.logging.log4j.core

Despite changing updating the compiler plugin as mentioned by @Naman similar error might occur if you are using maven javadoc plugin to generate javadocs with command like 'mvn clean install 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.                                           

The solution for now (until the problem is resolved on the plugin level) is to generate the api docs setting source version as java 8 (while keeping compiler version still as java 9+):

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

In above example please see that I also set false that is due to some other bug which is there while generting sources in java 8. Anyway setting source param to 8 and detectJavaApiLink solves the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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