Search code examples
javaeclipsemodulejava-platform-module-systemmodularization

Modularizing a Java Project in Eclipse


I have four Java packages in Eclipse: augustus, caligula, julius, and nero. I want to place augustus and caligula in a module. I want to place julius and nero in another module. How do I go about doing that?

I tried creating a "Module1" folder under the "src" source folder to hold augustus and caligula. And I tried creating a "Module2" folder under "src" source folder to hold julius and nero. However, these folders are automatically turned into packages in Eclipse.

Then I tried creating a "Module1" source folder to hold augustus and caligula. And I tried creating a "Module2" source folder to hold julius and nero. However, it seems Eclipse allows only one module-info.java file. In other words, I can't put a module-info.java file in each source folder.


Solution

  • Each Java Project holds a single module in the Java Platform Module System. I primarily used this video as a resource: Creating a Multi-Module Maven Project. However, a book titled "The Java Module System" by Nicolai Parlog can help understand the underlying theories behind the system.


    Here is a tutorial on how I was able to solve my problem:

    Create a Maven Project

    This parent project holds all the modules (child projects). Press the New button ⇒ Maven ⇒ Maven Project ⇒ press the Next button ⇒ check "Create a simple project" ⇒ press the Next button ⇒

    Group Id: org.name 
    Artifact Id: RomanEmperors
    Version: 0.0.1-SNAPSHOT
    Packaging: pom
    

    ⇒ press the Finish button.

    Create a Maven Module

    Press the New button ⇒ Maven ⇒ Maven Module ⇒ press the Next button ⇒ check "Create a simple project" ⇒

    Module Name: MavenModuleName1
    Parent Project: RomanEmperors
    

    ⇒ press the Finish button.

    Create a module-info.java File

    Right-click on your Maven Module ⇒ Configure ⇒ Create module-info.java ⇒ "Project requires compliance level of 9 or above. Do you want to change project compliance and JRE to 20?" press the Yes button ⇒

    Module name: module1
    

    Note. By convention, module names usually start with a lowercase letter. ⇒ press the Create button.

    Create a Package

    Right-click on "src/main/java" ⇒ New ⇒ Package ⇒

    Name: augustus
    

    Note. By convention, package names usually start with a lowercase letter. ⇒ press the Finish button.


    Then, repeat the steps to create the other modules and packages.