Define a Java 9 multi-moduled project in Eclipse
Asked Answered
D

2

19

I'm trying out Java 9 Jigsaw module system (no module experience yet) and would like to use it for capsuling the classes within my project, but it's confusing.

According to this article it should be possible to have multiple modules within ONE project. I made a new project in Eclipse Oxygen (Java 9 is supported) with the same structure as shown in the article. But Eclipse keeps telling me that I must not have more than one module-info.java in a project.

I really don't know how to tell Eclipse that it should use the "multi-module-mode". And I really would appreciate not having to create a new project for every single module.

This works:

enter image description here

This not:

enter image description here

But according to this article something like that should work:

enter image description here

And how about deployment of a modularized project with Eclipse? There is nothing to see about the new jmod extension. Do I still export it as a runnable JAR file like before?

Notice that my questions refer to working with the IDE (no command line, I mean with an IDE that should be possible, right?) Thank you for enlightening me.

Deucalion answered 5/2, 2018 at 16:37 Comment(3)
Please add screenshot to the error and the project structure that you're using.Ambulacrum
I added example screenshots.Deucalion
Currently, Eclipse does not support multiple modules per project (e. g. because each module has its own Java build path).Foreconscious
F
14

Currently, Eclipse requires you to create a separate project for each module (e. g. because each module has its own Java Build Path).

To understand this design decision, consider that Java modules correspond to OSGi bundles / Eclipse plug-ins and it has always been to have a separate project for each bundle/plug-in. If you come from the Maven world, you would probably expect a deeper folder structure instead. But modules are self-contained and combining several modules into one project would only add an additional folder level without meaning. However, Eclipse supports nested projects and so-called working sets if you need an additional folder level.

Exporting modules as images is planned for Eclipse 2019-03 (4.11), on March 20, 2019 (see Eclipse bug 518445). Exporting modules as JARs that can be used on the modulepath (-m) already works (see my video).

Foreconscious answered 5/2, 2018 at 23:50 Comment(8)
What a shame. But that at least answers my question.Deucalion
@Arceus, after working for 15 years with modules in Eclipse I never saw any reason for "multi module projects". As howlger hinted at, several other mechanisms exist for grouping modules. This is different from the plain javac view, where folders is the only thing we have, so all grouping has to happen via folders. In a manner of speaking, Eclipse is always in multi-module mode, just that the container is not called "project" but "workspace".Cell
Eclipse project == module. Already accepted it. It's just "a shame" that command line compiling seems to support this multi module mode while eclipse simply does not, because I prefer to have only one workspace for having the same workspace settings in all my projects. And I already used working sets for other meanings of grouping.Deucalion
Do we always need a second Eclipse project for the unit tests?Intoxicate
@ClaudeMartin Since Oxygen.3 JUnit tests also work in a module project (see Eclipse bug 525948 and video).Foreconscious
Architecture-wise I see lots of reasons to have more than 1 module within an eclipse project. Packages are to weak to ensure a certain architecture, whereas modules are strict enough to keep an architecture from eroding over time. I would like to create a hexagonal architecture with a domain kernel and several adapters surrounding it. All within a single project - because that's what it is: just a single project. I know that osgi has a different perspective on it, saying that a module is a project. But honestly, I would like to go with the Java9 approach and not stick with any osgi tradition.Lynnell
@SirHackalot Both, Java modules and OSGi bundles, are about encapsulation: API vs. internal code; explicit dependencies to other modules/bundles. Each module/bundle can have different dependencies that are displayed in Eclipse at the project level. If you want to group multiple modules/bundles, create a general project that contains the modules/bundles as nested Java projects. But the question remains: why and according to which criteria should modules/bundles, which are encapsulated building blocks from which products are built, be grouped?Foreconscious
@SirHackalot Java modules are used to modularize the JRE and are not an alternative to OSGi bundles. In OSGi, bundles have a version and dependencies can refer to a version range. OSGi bundles can be started, stopped and upgraded at runtime. All this is not possible with Java modules.Foreconscious
H
2

I don't know if this question is still open for an answer, but you can solve this problem by simply removing all source folders on the build path. At least this works for Eclipse 2021-12 version.

As you can see this is a demo project from the Official Gradle Guide Book and it has multiple modules. Each module has its own module-info.java.

project structure in IntelliJ IDEA

If I open this project in Eclipse it will give me the 'duplicated entries on module-info.java' error.

Eclipse shows the error

But if I delete all the source folders on the build path, the error is gone and the project can be built and run without problem.

project properties: Java build path

The only problem is that you have to build the project with Gradle so that it will produce the .jar of each module and you have to include them in the libraries later.

include all the .jar in libraries

I think this is probably the same solution mentioned by howlger above.

Heck answered 12/12, 2021 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.