gpt4 book ai didi

java - 找不到模块 : How to compile JDK 10 Eclipse Maven Project with Module?

转载 作者:行者123 更新时间:2023-11-30 07:46:25 27 4
gpt4 key购买 nike

以下是我的 pom.xml 的主要部分:

属性:

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

项目结构是:

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

module-info.java 包含:

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

requires log4j.api;
}

当我执行: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

我还尝试通过指定配置将 --add-modules 参数提供给 maven-compiler-plugin:

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

但这也没有用。

几点:

如果我删除 module-info.java 它会起作用。

module-info.java 由 Eclipse 生成。

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

因此,如果将 module-info.java 更改为:

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

requires org.apache.logging.log4j;
}

Eclipse Java Editor 开始在文件 module-info.java 中显示编译错误:

enter image description here

同样在其他使用 log4j-api 包的 Java 类中:

enter image description here

但是 mvn clean package 在这种情况下可以使用警告消息:

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

我正在使用:

  • Eclipse Oxygen.3a 版本 (4.7.3a)
  • 甲骨文 JDK 10
  • Apache Maven 3.5.2

在另一个 SO 线程中:Spring Boot module not found error它被要求清理 Maven 的本地存储库。我也尝试使用 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

关于java - 找不到模块 : How to compile JDK 10 Eclipse Maven Project with Module?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50621741/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com