Restart with Java and Maven

For professional reasons and to revive my memory I am diving into Java.

My current system is Fedora, a Linux (or GNU/Linux) OS, so the installation commands in this post target it.

The JDK is not the JRE

Even if Java is already installed in the OS, it’s almost certainly only the Java Runtime Environment (JRE). For development we need the Java Development Kit (JDK).

To achieve this we need to select the version from the package manager in the system.

  • sudo dnf install java-latest-openjdk-devel installs the latest OpenJDK

The -devel suffix is the part that brings the compiler and the rest of the development tooling; the plain java-latest-openjdk package is only the runtime. One caveat: “latest” currently means an early access build of the next JDK release, which can be ahead of what some projects expect. If you prefer a stable release, install a numbered package instead — e.g. sudo dnf install java-25-openjdk-devel — after checking what the repositories offer:

dnf list --available 'java-*-openjdk-devel'

With the JDK in place, we can confirm which version is active:

java -version

Now we have the development kit, and should be able to start developing.

Managing dependencies with Maven

In today’s open source ecosystem we have a lot of available packages that we can use as dependencies. Having dependencies means we need a way to manage them, and for that we could go for the old school Maven (that downloads half of the internet, wink wink).

  • sudo dnf install maven installs the latest Maven version

  • mvn -version shows the Maven and JDK versions in use

Scaffolding a project with Archetype

Creating a new project, in older days, I basically used the IDE to set it up. At the moment I’m using neovim and don’t have a plugin for that (and don’t really want to add one dedicated to it). But with some investigation I found out that Maven has a workaround for this.

Maven has the Archetype, a project templating tool. With it and some parameters we can easily create a project, provided that an Archetype exists for the type of project that we are looking for.

In my case I want to create a Cucumber project, which has an Archetype. To create it I’ll execute the following command.

mvn archetype:generate                        \
  -DarchetypeGroupId=io.cucumber              \
  -DarchetypeArtifactId=cucumber-archetype    \
  -DarchetypeVersion=8.0.4                    \
  -DgroupId=hellocucumber                     \
  -DartifactId=hellocucumber                  \
  -Dpackage=hellocucumber                     \
  -Dversion=1.0.0-SNAPSHOT                    \
  -DinteractiveMode=false

If -DinteractiveMode=false is left out, Maven asks for each of those values one by one, which is handy when you don’t want to memorise the property names.

This will generate a project with cucumber, JUnit, and AssertJ dependencies. Besides the dependencies, it adds the base file and folder structure to build a testing project, with an example test. To confirm that everything is wired up:

cd hellocucumber
mvn test

Takeaway

Even with an IDE capable of generating a Java project, Archetype is an awesome piece of templating that Maven gave to developers, allowing a new project to be generated from a template instead of having to create every file by hand.

It also connects with the way I already work: both at my job and in my personal repositories I keep ‘starter’ projects. The idea is to use them as templates, so starting something new never begins completely from scratch. A feature like this opens new ways to generate and consume those starters.

For now I’ll keep exploring Cucumber and refreshing my memory of Java, but I’m seeing a path to explore, and maybe to create a custom Archetype if I’m able to generate one locally. I need to squeeze some time in a near future to explore it.