I've spent some time to migrate my project written in Groovy to Java 10. Now it's possible to compile and run it. But still it doesn't use any benefits of Java 9 modularity.
Googling about Groovy and Java 9 modules gives almost nothing.
So is it possible to migrate Groovy project to use JDK 10 with Project Jigsaw modules?
Building, testing and running Java Modules With this release, Gradle supports the Java Module System with everything you need to compile and execute tests for Java modules. You can also build Javadoc and run applications.
Java 8 with Project Jigsaw brings a module system to the SDK. I see it as a good thing as it's part of the package (built-in).
module-info. java file. It declares the dependencies within the module system and allows the compiler and the runtime to police the boundaries/access violations between the modules in your application.
Well, after a few days of experiments I come up with the answer - yes, it is possible to use Groovy with Project Jigsaw modules.
But it needs some additional effort.
Let's say we have following file structure:
├── build
├── jigsaw
│ └── module
│ └── test
│ └── Application.groovy
├── lib
│ └── groovy.all.jar
└── module-info.java
module-info.java
module main {
requires groovy.all;
}
Application.groovy
package jigsaw.module.test
class Application {
static void main(String[] args) {
println "Hello module!"
}
}
First of all we need to compile module-info.java file with javac instead of compiling all files using groovyc because groovy treats module file as closure.
Let's do it:
javac -d build --module-path lib/ module-info.java
--module-path will include our groovy.all.jar as automatic module with name derived from JAR-file name.
Next we need to compile Application.groovy
groovyc -d build jigsaw/module/test/Application.groovy
It goes smoothly.
After compilation we have module-info.class (aka module descriptor) and Application.class.
├── build
│ ├── jigsaw
│ │ └── module
│ │ └── test
│ │ └── Application.class
│ └── module-info.class
├── jigsaw
│ └── module
│ └── test
│ └── Application.groovy
├── lib
│ └── groovy.all.jar
└── module-info.java
Now let's try to run our compiled module.
java --module-path build:lib --module main/jigsaw.module.test.Application
And this is what we get
Error occurred during initialization of boot layer
java.lang.module.FindException: Unable to derive module descriptor for lib/groovy.all.jar
Caused by: java.lang.module.InvalidModuleDescriptorException: Provider class moduleName=groovy-all not in module
And what does it mean? I don't know. After a lot of googling I found something similar.
So we need to manually remove from JAR these files:
Finally our Java module is able to start
java --module-path build:lib --module main/jigsaw.module.test.Application
Hello module!
All manipulations were done using Oracle JDK 10 and Groovy 2.4.15.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With