英文:
How to add a non-modular Maven dependency to the module path as an automatic module?
问题
假设我们想要构建一个简单的Java 11模块化Maven项目,其中有一个单一的非模块化依赖org.json:json
(为了实际示例的实用性而随机选择)。
项目结构:
Foo.java
:
package foo.bar.project;
import org.json.JSONObject;
public class Foo {
static JSONObject o = new JSONObject();
}
module-info.java
:
module foo.bar.project {
}
pom.xml
:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>foo.bar</groupId>
<artifactId>project</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>11</maven.compiler.release>
</properties>
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20200518</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
<id>compile</id>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
我的问题是如何告诉Maven将org.json
作为自动模块放在模块路径上。默认情况下,Maven将非模块化的JAR包放在classpath
上作为未命名模块。
目前,运行mvn clean compile
会失败并显示以下错误:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project project: Compilation failure
[ERROR] <PathToProject>/src/main/java/foo.bar.project/foo/bar/project/Foo.java:[3,11] package org.json is not visible
[ERROR] (package org.json is declared in the unnamed module, but module org.json does not read it)
[ERROR]
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
系统信息:
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T20:41:47+02:00)
Maven home: C:\Tools\apache-maven-3.6.0
Java version: 11.0.1, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk-11.0.1
Default locale: sl_SI, platform encoding: UTF8
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
英文:
Lets say we would like to build a simple Java 11 modular Maven project with a single non-modular dependency org.json:json
(picked at random for the sake of a practical example).
Project structure:
<!-- language: none -->
project
|-- pom.xml
`-- src
`-- main
`-- java
|-- foo.bar.project
| `-- foo
| `-- bar
| `-- project
| `-- Foo.java
`-- module-info.java
Foo.java
:
package foo.bar.project;
import org.json.JSONObject;
public class Foo {
static JSONObject o = new JSONObject();
}
module-info.java
:
module foo.bar.project {
}
pom.xml
:
<!-- language: xml -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>foo.bar</groupId>
<artifactId>project</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>11</maven.compiler.release>
</properties>
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20200518</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
<id>compile</id>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
My question is how to tell Maven to put org.json
on the module path as an automatic module. By default, Maven places non-modular jars on the classpath
as unnamed modules.
As it stands, running mvn clean compile
fails with:
<!-- language: none -->
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project project: Compilation failure
[ERROR] <PathToProject>/src/main/java/foo.bar.project/foo/bar/project/Foo.java:[3,11] package org.json is not visible
[ERROR] (package org.json is declared in the unnamed module, but module org.json does not read it)
[ERROR]
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
System info:
<!-- language: none -->
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T20:41:47+02:00)
Maven home: C:\Tools\apache-maven-3.6.0
Java version: 11.0.1, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk-11.0.1
Default locale: sl_SI, platform encoding: UTF8
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
答案1
得分: 1
我觉得这条信息有点奇怪,
> [错误](包org.json在未命名模块中声明,但是
> 模块org.json没有读取它)
应该是应该读作 "..但模块foo.bar.project
不会读取它"。
然后你会发现你需要在module-info.java
中添加一个指令,如下:
requires org.json;
英文:
Ii find the message
> [ERROR] (package org.json is declared in the unnamed module, but
> module org.json does not read it)
strange, it should have read "..but module foo.bar.project
does not read it".
Then you would have found that you need to place a directive in module-info.java
such as:
requires org.json;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论