java - 如何将非模块化 Maven 依赖项作为自动模块添加到模块路径?
问题描述
假设我们想要构建一个简单的 Java 11 模块化 Maven 项目,它具有单个非模块化依赖项org.json:json
(为了实际示例而随机选择)。
项目结构:
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
:
<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
as unnamed modules上。
就目前而言,运行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"
解决方案
我找到消息
[错误](包org.json在未命名的模块中声明,但模块org.json没有读取它)
奇怪,它应该是“..but module foo.bar.project
doesn't read it”。
然后你会发现你需要放置一个指令,module-info.java
例如:
requires org.json;
推荐阅读
- c - 如何在 ESP32 服务器上处理带有 JSON 有效负载的 POST 请求?
- python - AttributeError:“命名空间”对象没有属性“x”
- install4j - 如何在 Install4j 中配置与 JRE 相关的错误消息
- node.js - 如何确认响应从 NodeJS 服务器离开的时间?
- .net-core - 如何使 .Net Core Console 应用程序连接到 SignalR 服务(在无服务器模式下)然后从服务器接收消息
- sql-order-by - 为什么会有总计行?
- reactjs - NextJs 基于 API 的嵌套动态路由
- database - 如何将大量记录更新为一批n条记录
- java - 从文本文件生成随机单词,但组合始终是 sam
- node.js - 客户端获取对服务器上节点 js 文件的请求