Skip to content
Advertisement

Gradle build – add module path

My question: How do I set a module path for gradle build?

I’ve become comfortable working with Java modules from the command line. I do a frequent exercise in Powershell which results in these source files.

JavaScript

appMod/module-info

JavaScript

appMod/appPack.Entry

JavaScript

greetMod/module-info

JavaScript

greetMod/greetPack.Hello

JavaScript

Since the appMod module requires greetMod, I compile and jar greetMod first.

JavaScript

Then I compile and jar appMod, but as I do so I specify the module path (-p) where the new greetMod jar (greetJar) can be found (in lib).

JavaScript

I can then run this or jlink it in part by adding a module path.

JavaScript

I now want to do this same exercise in Gradle, but I can’t figure out how to do the equivalent of setting a module path such as -p lib. I’ve seen code for sourceSets, and countless variations of dependencies, but so far I haven’t been able to put together something that works. I’ve also read conflicting statements that both say that Gradle doesn’t fully support Java modules, and that Gradle does support them.

Advertisement

Answer

I know it can be confusing, but it can definitely be done by gradle. You will need to use a multiproject build to have this work. In your top-most build.gradle, do this:

JavaScript

In your settings.gradle:

JavaScript

Everything inside appMod should be moved into a folder called appModSrc. Do the same for greetMod so use greetModSrc.


greetMod

Directory structure:

JavaScript

build.gradle

JavaScript

appMod

Directory structure:

JavaScript

build.gradle

JavaScript

With this setup, you can simply run ./gradlew :src:appMod:run:

JavaScript

You can download the idea project here: https://github.com/MVCE-Superstars/multi-java9-gradle

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement