Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Following example project 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;
}

when I run eclipse [project > maven > update project], all dependencies are added to the classpath. 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).

How do I do this?

(I am using Eclipse 2018-12, OpenJDK 11 and maven 3.6.0)

Example code of class 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;
    }
}
like image 558
huidube Avatar asked Jan 21 '19 10:01

huidube


People also ask

How do I add a dependency in Maven?

Add a Java Maven Dependency to the Utility ProjectRight-click the utility project, and select Maven>Add Dependency. Type a dependency name in the Enter groupID… field (e.g., commons-logging) to search for a dependency. Select the dependency, and click OK.

How do I add dependencies to an existing project?

If you are using STS or Eclipse for your development then you can easily add dependencies using CLI . You need to press the Ctrl+Space Bar in your pom. xml . Save this answer.


1 Answers

I couldn't recreate your issue. In general, Eclipse puts modules on the module path which are required in 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. javax.json-api is an explicit module (with module-info.java). javax.json is an automatic module (no module-info.java). I don't know how Java will handle this but this is not a good situation certainly. I recommend fixing this issue first (e.g. merge modules or use classpath instead of a module path).

like image 106
ZhekaKozlov Avatar answered Oct 29 '22 13:10

ZhekaKozlov