Maven:如何在> = Java 9中向modulepath添加依赖项

以下示例项目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>

module-info.java:

module test1 {
    exports test;
    requires transitive java.json;
}

当我运行eclipse时[项目> maven>更新项目],所有依赖项都添加到类路径中.
但是工件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;
    }
}

最佳答案 我无法重现你的问题.通常,Eclipse将模块放在module-info.java中所需的模块路径上.所以,大多数时候你甚至不需要关心这个.

在你的情况下,我看到一个不同的问题.两个jar文件javax.json-api和javax.json具有相同的模块名称并共享相同的包. javax.json-api是一个显式模块(带有module-info.java). javax.json是一个自动模块(没有module-info.java).我不知道Java会如何处理这个问题,但这肯定不是一个好的情况.我建议首先解决此问题(例如合并模块或使用类路径而不是模块路径).

点赞