Module not found: How to compile JDK 10 Eclipse Maven Project with Module?

江枫思渺然 提交于 2019-12-10 23:45:54

问题


Followings are the main sections of my pom.xml:

properties:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.build.targetEncoding>UTF-8</project.build.targetEncoding>
    <jdk.release>10</jdk.release>
    <jdk.vendor>oracle</jdk.vendor>
    <maven.compiler.source>1.10</maven.compiler.source>
    <maven.compiler.target>1.10</maven.compiler.target>

    <dependency.asm.version>6.2</dependency.asm.version>
    <dependency.log4j2.version>2.11.0</dependency.log4j2.version>

    <plugin.maven-toolchains.version>1.1</plugin.maven-toolchains.version>
    <plugin.maven-compiler.version>3.7.0</plugin.maven-compiler.version>    
    <plugin.maven-dependency.version>3.1.1</plugin.maven-dependency.version>
    <plugin.maven-jar.version>3.1.0</plugin.maven-jar.version>
</properties>

dependencies:

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

plugins:

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-toolchains-plugin</artifactId>
        <version>${plugin.maven-toolchains.version}</version>
        <executions>
            <execution>
                <goals>
                    <goal>toolchain</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <toolchains>
                <jdk>
                    <version>${jdk.release}</version>
                    <vendor>${jdk.vendor}</vendor>
                </jdk>
            </toolchains>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${plugin.maven-compiler.version}</version>
        <configuration>
            <source>${maven.compiler.source}</source>
            <target>${maven.compiler.target}</target>
            <release>${jdk.release}</release>
        </configuration>
        <dependencies>
            <dependency>
                <groupId>org.ow2.asm</groupId>
                <artifactId>asm</artifactId>
                <version>${dependency.asm.version}</version>
            </dependency>
        </dependencies>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>${plugin.maven-dependency.version}</version>
        <executions>
            <execution>
                <id>copy-dependencies</id>
                <phase>package</phase>
                <goals>
                    <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                    <outputDirectory>${project.basedir}/dist/lib/</outputDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>${plugin.maven-jar.version}</version>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <classpathPrefix>./lib/</classpathPrefix>
                    <mainClass>com.mycompany.tbe.myproject.Application</mainClass>
                    <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                    <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                </manifest>
            </archive>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <outputDirectory>${project.basedir}/dist</outputDirectory>
        </configuration>
    </plugin>       
</plugins>

The project structure is:

project
+
|-- src
|   +
|   |-- main
|   |   +
|   |   |-- java
|   |   |   +
|   |   |   |-- com
|   |   |   |   +
|   |   |   |   |-- mycompany
|   |   |   |   |   +
|   |   |   |   |   |-- tbe
|   |   |   |   |   |   +
|   |   |   |   |   |   |-- myproject
|   |   |   |-- module-info.java
|   |   |-- resources    
|-- pom.xml

The module-info.java contains:

/**
 * @author Tapas Bose
 */
module project {
    exports com.mycompany.tbe.myproject;

    requires log4j.api;
}

When I'm executing: mvn clean package it is throwing error, telling:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project <my-project>: Compilation failure
[ERROR] <path-to-my-project>/src/main/java/module-info.java:[7,15] error: module not found: log4j.api

I also have tried to supply --add-modules argument into the maven-compiler-plugin by specifying configuration:

<configuration>
    <compilerArgs>
        <arg>--add-modules</arg>
        <arg>log4j.api</arg>
    </compilerArgs>
</configuration>

But that didn't work either.

Few points:

If I delete the module-info.java it works.

The module-info.java is generated by Eclipse.

The java describe-module reveals the followings:

$ jar --file=log4j-api-2.11.0.jar --describe-module
releases: 9

No root module descriptor, specify --release

$ jar --file=log4j-api-2.11.0.jar --describe-module --release 9
releases: 9

org.apache.logging.log4j jar:file:///<path-to-maven-local-repository>/org/apache/logging/log4j/log4j-api/2.11.0/log4j-api-2.11.0.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

Hence if change the module-info.java as:

/**
 * @author Tapas Bose
 */
module project {
    exports com.mycompany.tbe.myproject;

    requires org.apache.logging.log4j;
}

Eclipse Java Editor starts showing compilation error in the file module-info.java:

Also in other Java classes where log4j-api's packages are used:

But mvn clean package works in this case with warning messages:

[WARNING] ********************************************************************************************************************
[WARNING] * Required filename-based automodules detected. Please don't publish this project to a public artifact repository! *
[WARNING] ********************************************************************************************************************

I'm using:

  • Eclipse Oxygen.3a Release (4.7.3a)
  • Oracle JDK 10
  • Apache Maven 3.5.2

In another SO thread: Spring Boot module not found error it is asked to clean maven's local-repository. I also tried to do the same using mvn clean dependency:purge-local-repository package, but no luck.


回答1:


The problem is that log4j-api-2.11.0.jar is a multi-release jar with a module-info.class in META-INF/versions/9/module-info.class, which eclipse doesn't support yet, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=534624




回答2:


I was able to make this work somehow. I am using JDK 10.0.1

  1. I downloaded the latest Eclipse Photon Release (4.8.0), I don't know if this fix will work with older versions.
  2. I changed the module-info.java to have the following lines: requires log4j.api; requires org.apache.logging.log4j.core; I am using version 2.11.1 of log4j.
  3. I changed all my imports that say import org.apache.logging.log4j.*; to not use the wildcard. So they changed to: import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger;

That's it, it works with both Maven and Eclipse.



来源:https://stackoverflow.com/questions/50621741/module-not-found-how-to-compile-jdk-10-eclipse-maven-project-with-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!