简体   繁体   English

找不到模块:如何使用模块编译JDK 10 Eclipse Maven项目?

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

Followings are the main sections of my pom.xml: 以下是我的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: module-info.java包含:

/**
 * @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: 当我执行时: mvn clean package它抛出错误,告诉:

[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: 我还尝试通过指定配置在maven-compiler-plugin提供--add-modules参数:

<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. 如果我删除module-info.java它可以工作。

The module-info.java is generated by Eclipse. module-info.java由Eclipse生成。

The java describe-module reveals the followings: java describe-module揭示了以下内容:

$ 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: 因此,如果将module-info.java更改为:

/**
 * @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 : Eclipse Java编辑器开始在文件module-info.java显示编译错误:

在此处输入图片说明

Also in other Java classes where log4j-api's packages are used: 同样在使用log4j-api软件包的其他Java类中:

在此处输入图片说明

But mvn clean package works in this case with warning messages: 但是mvn clean package在这种情况下可以显示警告消息:

[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) Eclipse Oxygen.3a版本(4.7.3a)
  • Oracle JDK 10 Oracle JDK 10
  • Apache Maven 3.5.2 Apache Maven 3.5.2

In another SO thread: Spring Boot module not found error it is asked to clean maven's local-repository. 在另一个SO线程中: 找不到Spring Boot模块错误 ,要求它清除Maven的本地存储库。 I also tried to do the same using mvn clean dependency:purge-local-repository package , but no luck. 我也尝试使用mvn clean dependency:purge-local-repository package做同样的事情,但是没有运气。

问题是log4j-api-2.11.0.jar是一个多发行版jar,在META-INF / versions / 9 / module-info.class中具有module-info.class,该Eclipse尚不支持,请参阅https://bugs.eclipse.org/bugs/show_bug.cgi?id=534624

I was able to make this work somehow. 我能够以某种方式完成这项工作。 I am using JDK 10.0.1 我正在使用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. 我下载了最新的Eclipse Photon Release(4.8.0),但我不知道此修补程序是否适用于较旧的版本。
  2. I changed the module-info.java to have the following lines: requires log4j.api; requires org.apache.logging.log4j.core; 我将module-info.java更改为以下几行: module-info.java requires log4j.api; requires org.apache.logging.log4j.core; requires log4j.api; requires org.apache.logging.log4j.core; I am using version 2.11.1 of log4j. 我正在使用log4j的2.11.1版本。
  3. I changed all my imports that say import org.apache.logging.log4j.*; 我更改了所有导入内容,例如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; 因此,他们将其更改为: import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger;

That's it, it works with both Maven and Eclipse. 就是这样,它可以与Maven和Eclipse一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Eclipse模块问题JDK 10 - Eclipse module problems jdk 10 找不到Maven模块项目 - Maven Module Project not found 如何在多模块Maven项目中编译其中的单个模块? - How to compile a single module inside it in a multi-module maven project? 导入到Eclipse的Maven项目的错误“找不到GWT模块” - Error “No GWT module found” for Maven project imported into Eclipse 如何通过将--patch-module javac选项传递给我的项目编译器,使Eclipse在JDK 9+上编译修补模块? (见图) - How to make Eclipse compile patched modules on JDK 9+ by passing the --patch-module javac option to my project compiler? (see pic) 在Gradle编译一个JDK 8项目+一个JDK 9“module-info.java” - Compile a JDK 8 project + a JDK 9 "module-info.java" in Gradle Java 10 Eclipse + Maven “java.lang.module.FindException:找不到模块 myproject” - Java 10 Eclipse + Maven "java.lang.module.FindException: Module myproject not found" 如何在 eclipse 中将 spring 引导项目作为 maven 项目的模块导入? - How to import spring boot project as module of maven project in an eclipse? 将eclipse java项目转换为maven模块项目 - Convert eclipse java project to maven module project 如何在Gradle中指定模块的JDK编译级别 - How to specify the JDK compile level of a module in Gradle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM