【发布时间】:2019-06-14 17:24:20
【问题描述】:
以下示例项目a:
project-a
-src/main/
-java/
-test/
-Test.java
module-info.java
-resources
...
pom.xml
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>test</groupId>
<artifactId>test1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
模块信息.java:
module test1 {
exports test;
requires transitive java.json;
}
当我运行 eclipse [project > maven > update project] 时,所有依赖项都添加到类路径中。
但工件javax.json-api 必须在模块路径上(api 是一个模块),另一个在类路径上(该 api 的实现不是模块)。
我该怎么做?
(我使用的是 Eclipse 2018-12、OpenJDK 11 和 maven 3.6.0)
Test类的示例代码:
package test;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import javax.json.Json;
import javax.json.JsonObjectBuilder;
import javax.json.JsonStructure;
import javax.json.JsonWriter;
import javax.json.JsonWriterFactory;
import javax.json.stream.JsonGenerator;
public class Test {
public static void main(String[] args) {
JsonObjectBuilder b = build("name", "FOO", "surname", "BAR");
String s = toFormattedString(b.build());
System.out.println(s);
}
public static String toFormattedString(JsonStructure json) {
Map<String, Boolean> config = new HashMap<String, Boolean>();
config.put(JsonGenerator.PRETTY_PRINTING, true);
JsonWriterFactory writerFactory = Json.createWriterFactory(config);
StringWriter writer = new StringWriter();
JsonWriter jsonwriter = writerFactory.createWriter(writer);
jsonwriter.write(json);
return writer.getBuffer().toString();
}
public static JsonObjectBuilder build(Object... objs) {
JsonObjectBuilder b = Json.createObjectBuilder();
for (int i = 0; i < objs.length - 1; i += 2) {
String name = (String) objs[i];
Object obj = objs[i + 1];
if (obj == null) {
b.addNull(name);
} else if (obj instanceof String) {
b.add(name, (String) obj);
} else if (obj instanceof Integer) {
b.add(name, (int) obj);
} else if (obj instanceof Double) {
b.add(name, (double) obj);
} else if (obj instanceof Long) {
b.add(name, (long) obj);
} else {
throw new IllegalArgumentException("Value type not supported:" + obj + " [class=" + obj.getClass() + "]");
}
}
return b;
}
}
【问题讨论】:
-
您应该使用 JDK 11 在普通命令行上构建...如果您需要将依赖项作为模块,您应该将其添加到
module-info.java,因为您已经将其作为依赖项? -
我已经将依赖项(java.json)添加到模块路径中。但是 [maven >update project] 将所有 maven 依赖项放在类路径上。
-
1.我只想在 Eclipse 中更新 Maven 项目时自动将任何 Maven 依赖项设置为模块路径。 2. 为什么这没有意义?你有其他想法如何解决这个问题吗?
-
@huidube JPMS 所需的所有额外工作是定义系统库的子集,以减少启动时间和/或创建最小的自定义 JRE(通过
jlink)。但这仅在不依赖于类路径的情况下才有效。您能解释一下为什么要使用 JPMS 吗? -
@huidube 恕我直言,只要 JPMS 不包含语义版本控制并且不提供可以在运行时启动和停止的模块(OSGi 已经可以),希望不会。但这只是我个人的看法。您必须决定在您的情况下,您的努力是否值得 JPMS 带来的好处。除此之外,您还不能在您的案例中使用 JPMS,因为并非所有依赖项都已为 JPMS 做好准备。
标签: java eclipse maven classpath java-11