2

I saw that in Gradle, we declare a module as artifact/src/main/java/module-info.java, but in OpenJDK they declare it as src/com.group.artifact/module-info.java. Then in Jenkov we have src/main/java/com.group.artifact/module-info.java.

I am confused, which one should I use? I have a Gradle project that contains multiple subprojects inside. And I am trying to use the subprojects as modules. IntelliJ IDEA gives an error when I use the Jenkov method, and I have to switch to Gradle one. Is it the same?

1 Answer 1

3

OpenJDK and Gradle simply have different conventions for structuring source code. Since you're using Gradle (and therefore Gradle's Java language plugin), you should follow it's conventions. Gradle's Java language plugin will automatically compile the code in the directory src/main/java.

Assuming you have two modules, com.example.moduleone and com.example.moduletwo, your project's structure should look like this:

build.gradle
settings.gradle
moduleone
├──src
│  └──main
│     └──java
│        ├──{PACKAGES_AND_CODE}
│        └──module-info.java
└──build.gradle
moduletwo
├──src
│  └──main
│     └──java
│        ├──{PACKAGES_AND_CODE}
│        └──module-info.java
└──build.gradle

Not using Gradle and compiling your project yourself with javac -d out --module-source-path src -m com.example.moduleone,com.example.moduletwo, it should look like this:

src
├──com.example.moduleone
│  ├──{PACKAGES_AND_CODE}
│  └──module-info.java
└──com.example.moduletwo
   ├──{PACKAGES_AND_CODE}
   └──module-info.java

As for the 'Jenkov method': I've never seen this structure before, it looks like a weird mix and I recommend not to use it. It would probably be just confusing for other developers.

Sign up to request clarification or add additional context in comments.

2 Comments

I am revisiting this post, and I found that Gradle doesn't share the module directory name with the module name itself. In Gradle's module docs , the directory name excludes group id.
@Arkyo I've updated the answer – the main point of it was never wrong though. I'm sure, the names of a submodule's directory are irrelevant to Gradle. I added two missing build.gradle files in the submodules directories as well, which where missing in the original post.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.