I had some hours to spend on coding, and I didn’t want to do any game development, so I went back to Java a little bit, to checkout if I can refactor my 2D engine to support JIGSAW.
My end goal was to be able to ship a standalone, or so called self-contained application, which means the end user won’t need any JRE installed, the app contains everything it needs to run.
It wasn’t that complex as the code base was not that big, but I hit some walls.
Set up modules in IntelliJ IDEA with Maven
First, I had to refactor the standard one module maven structure into a multi-modularised structure. As both IDEA and Maven have the term module on top of java modules, it was a little bit confusing.
Here are the steps I did:
- Create two new maven module: com.intermetto.engine, com.intermetto.game
- Copy the engine’s code into the engine module, and the game’s code to the game module. Note, that the module name is com.intermetto.engine, but under that I still had to keep the package structure as well — see below.
- Create module-info.java files under module/src/main/java. Note: if you don’t have create module-info.java in the menu, set the module’s language level at least to 9.
- Move the dependencies/resources/tests to the corresponding module’s pom
- Delete the root src, resources, test, etc.
At this point, the hierarchy looked like this:
Resolve module dependencies
Next, I hit build, and it helped me what to put in the module-info files. I hit three problems here.
- I had to add transitive for some requirements in the engine’s module-info, to be able to use required code in the game’s module without importing those there too.
- I had to open some packages for some other packages as those are use reflections.
- I had to replace my SLF4J imports to logback one, because the latter one supports only the modules.
Next step was to build the modules to be able to use jlink. On Udemy, I found a greate course, which helps you to understand how to build modules. Maven also has plugin for building and linking modules, but I found those not maintained.
- To both engine and game module I put this jar plugin to the pom.xml and basically that’s all.
Linking and automatic modules
Then I tried to link the app with this command:
jlink --module-path $JAVA_HOME/jmods:build/mods --add-modules com.intermetto.game --launcher APP=com.intermetto.game/com.intermetto.game.Main --compress 2 --no-header-files --no-man-pages --strip-debug --output build/release
It did not work, because I had an automatic module in the dependencies. Read more here, why automatic modules can’t be linked.
The solution was to update the automatic module jar with a module-info. I followed this article, and it worked!
After this, jlink ran well, and I got my self contained app, which is 28MB! :)