Search code examples
javaeclipsemavenjava-module

Making Eclipse play well with JPMS and Maven


Given a Maven project created in Eclipse from archetype maven-archetype-quickstart ( with junit upgraded to 5.5.2, which has JPMS support) , how can it be modularized to a JPMS project and still behave well in Eclipse?

When I tried this, I first created the module folder in the java folder and moved the package structure into that. The project structure then looks like this:

jpms (project folder) -> src/main/java/org.myorg.mymodule/org/myorg/myproject/jpmspackage

. So far so good. The project builds with mvn clean install in console and the unit test is executed. Then the module folder (containing the module-info.java file + packages) was set to source folder ("use as source folder" in Eclipse).

Performing a Maven build in Eclipse now fails with

Cannot nest'jpms/src/main/java/org.myorg.mymodule' inside 'jpms/src/main/java'. To enable the nesting exclude 'org.myorg.mymodule/' from 'jpms/src/main/java'

How can the source folder or build path be set to appease Eclipse?

Module descriptor:

module org.myorg.mymodule {
    requires org.junit.jupiter.api;
}

Updated JUnit dependency (the project was originally generated with JUnit 3-something)

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.5.2</version>
</dependency>

Versions: Eclipse 2019-09, Java 12, Maven 3.6.2


Solution

  • Keep the project structure, just add the module-info.java file into the default package:

    1. Right-click the project folder and choose Configure > Create module-info.java

    Please note, that for each module a separate project is required as it does not make sense to have multiple modules (which have different dependencies) in one project (if required, use nested projects instead).

    See also this Java modules Maven example.