简体   繁体   English

Maven:如何在>= Java 9 中向模块路径添加依赖项

[英]Maven: How to add dependencies to modulepath in >= Java 9

Following example project a:以下示例项目a:

project-a
-src/main/
         -java/
              -test/
                   -Test.java
              module-info.java
         -resources
...
pom.xml

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:模块信息.java:

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

when I run eclipse [project > maven > update project], all dependencies are added to the classpath.当我运行 eclipse [project > maven > update project] 时,所有依赖项都会添加到类路径中。 but the artifact javax.json-api must be on the modulepath (the api is a module), the other on the classpath (the implementation of that api is not a module).但是工件javax.json-api必须在模块路径上(api 是一个模块),另一个在类路径上(该 api 的实现不是模块)。

How do I do this?我该怎么做呢?

(I am using Eclipse 2018-12, OpenJDK 11 and maven 3.6.0) (我使用的是 Eclipse 2018-12、OpenJDK 11 和 maven 3.6.0)

Example code of class Test:类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;
    }
}

I couldn't recreate your issue.我无法重现您的问题。 In general, Eclipse puts modules on the module path which are required in module-info.java .通常,Eclipse 会将模块放在module-info.java中所需的模块路径上。 So, most of the time you don't even need to care about this.所以,大多数时候你甚至不需要关心这个。

In your case, I see a different issue.在你的情况下,我看到了一个不同的问题。 Two jar files javax.json-api and javax.json have the same module name and share the same packages.两个 jar 文件javax.json-apijavax.json具有相同的模块名称并共享相同的包。 javax.json-api is an explicit module (with module-info.java ). javax.json-api是一个显式模块(带有module-info.java )。 javax.json is an automatic module (no module-info.java ). javax.json是一个自动模块(没有module-info.java )。 I don't know how Java will handle this but this is not a good situation certainly.我不知道 Java 将如何处理这个问题,但这肯定不是一个好情况。 I recommend fixing this issue first (eg merge modules or use classpath instead of a module path).我建议首先解决这个问题(例如合并模块或使用类路径而不是模块路径)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在&gt; = Java 9中将json的maven依赖项添加到模块中 - How to add maven dependencies of json to modules in >= Java 9 如何使用java 10在maven中添加javafx依赖项 - how to add javafx dependencies in maven with java 10 为什么以及何时 Eclipse 将 Maven 依赖项放在 CLASSPATH 中而不是 MODULEPATH 中? - Why and when is Eclipse putting Maven dependencies in the CLASSPATH and not in the MODULEPATH? 如何从maven依赖项构建java 9依赖项 - How to build java 9 dependencies from maven dependencies 如何在命令行中为java的modulepath指定多个jar文件? - How to specify multiple jar files on the modulepath for java on the command line? Maven - 如何为休眠添加所有必需的依赖项? - Maven - how to add all required dependencies for hibernate? Maven如何将可传递依赖项添加到类路径中? - How does maven add transitive dependencies into classpath? 如何在Maven中一次添加多个依赖项? - How to add multiple dependencies at once in maven? Maven 依赖项以及它们在 Java 项目中的实际工作方式 - Maven dependencies and how they actually work in Java projects 如何在Maven中从Sonatype快照添加依赖项 - How to add dependencies from Sonatype snapshots in Maven
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM