Welcome to a series of blog posts describing how Bach.java builds a modular Java project.
This initial introduction prepares the foundation for more blog posts to come. The felt ratio of 99% IDEA-related sections to only 1% relevant for Bach.java is on purpose. Later posts will build upon this one and only refer to IDEA-related instructions covered here.
TL;DR
- Create a modular Java project or clone/download bach-intro-1-greetings from GitHub
- Run
jshell https://sormuras.de/bach/build
- Done
Fast-forward to the Build Modular Java Project with Bach.java section.
The remainder and the majority of the text sections below is about setting up IntelliJ IDEA for a modular Java project. In order to not re-invent wheels this blog borrows ideas and replays commands from the Module System Quick-Start Guide. It doesn’t hurt to (re-)visit that guide before reading on.
Without further a do, let’s start hacking … erm, clicking!
Screenshots for this blog post are taken from IDEA 2020.1.1 (Community Edition). An older version should be okay, too.
In this blog JDK 14.0.1 is used.
If you haven’t already installed a JDK 11+ on your machine, make use of IDEA’s excellent Download JDK… feature offered in the drop-down menu shown below.
Greetings
as the Project nameThe last path element should read
Greetings
/.idea
to the Module file location path in the More Settings group
Keeping all IDEA-related assets in one place, namely below the
.idea/
directory.
Click Finish to close the new project wizard
A popup window may appear - click OK to let IDEA create the
Greetings/.idea
directory for you.
IDEA created an initial project structure. Some settings require a fine-tune pass.
IDEA has its own idea of storing all Java sources of an IDEA module in a nested src/
directory.
In the following, we’ll delete and unmark such a src/
directory twice.
Once per created IDEA module.
Without the src/
directories the structure matches the example from the Quick-Start Guide better.
.idea
element in the existing path of Project compiler output property as shown below
Why? To keep all IDEA-related assets in one place, here below the
.idea/out
directory.
src
folder in Modules/Greetings/SourcesDEL
keycom.greetings
Let’s create an IDEA module named com.greetings
.
It will host the Java module with the same name.
Yes, as mentioned above, the hereby automatically created src/
directory will be deleted.
Click Next
com.greetings
as Module namecom.greetings
) with .idea
in Module file locationsrc
folder as above and don’t forget to unmark itEvery IDEA module that hosts Java source files mandates at least a single Sources Folder to be selected. Here, the content root directory of the just created IDEA module is marked as such.
${BASE}/com.greetings
(i.e. Content Root) from the Sources overviewcom.greetings
With IDEA’s project structure set up and fine-tuned, let’s add Java source files. The Quick-Start Guide Greetings example reads:
The module consists of two source files: the module declaration (
module-info.java
) and the main class.
com.greetings
IDEA moduleIDEA just generated an empty, yet proper Java Module Declaration for us!
com.greetings.Main
After declaring the Java module com.greetings
, now it’s the turn of the main program.
com.greetings
IDEA module, againcom.greetings.Main
IDEA just generated an empty Main.java
file and stored in package com.greetings
.
That’s it. All is set up for running the program.
Press Play On Tape …
Finally…
Change to the base directory of the project, if necessary.
jshell https://sormuras.de/bach/build
and press Enter
This URL redirects to the bach-build.jsh script. That JShell-based script forms the zero-installation feature of Bach.java.
Run
jshell -R-Debug https://sormuras.de/bach/build
to see what Bach.java is doing.
Wait some seconds to let the zero-installation feature bootstrap Bach.java
and build the project.
.bach/workspace/image/bin/greetings
that the main program is executable
of the Greetings project in the generated custom runtime image
Done.
Here’s a short video showing Bach 11.8 in action: https://youtu.be/LyJ9BfZfEsI
.bach
folder for generated assets: classes, modules, image, summaries, ….bach/summary.md
file./** Javadoc */
comment to module com.greetings {}
and run bach-build.jsh
again..bach/src/Build.java
program.https://github.com/sormuras/simplicissimus