Explain Codes LogoExplain Codes Logo

Specify JDK for Maven to use

java
maven
java-compiler-plugin
environment-variables
Nikita BarsukovbyNikita Barsukov·Nov 11, 2024
TLDR

Direct Maven to use a specific JDK by setting the JAVA_HOME environment variable to the required JDK path. Alternatively, set JAVA_HOME inline with your Maven command:

On Windows:

set JAVA_HOME=C:\path\to\jdk && mvn clean install

On Linux/macOS:

JAVA_HOME=/path/to/jdk mvn clean install

This approach ensures Maven calls the JDK at JAVA_HOME for the build process. Be sure to validate that the JDK path you've set is accurate and corresponds to the version intended.

Quick switch, little risk: Temporarily change your JDK version

To temporarily adjust JAVA_HOME for a specific Maven execution without reshaping the global settings, you can define the environment variable during the Maven command execution:

# Switching Java versions faster than you can say "JDK" JAVA_HOME=/path/to/jdk mvn -version

This command will output the Maven version confirming your temporal JDK switching wizardry.

Break free from the global: Isolating the JDK for Maven

Break the chains of global environment variables by setting the JAVA_HOME temporarily for the Maven job:

# temporary Java for a temporary job, perfectly balanced JAVA_HOME=/path/to/jdk mvn -version

This command will present the Maven version alongside the JDK version it's using, confirming the one-time setting.

No more JDK misdirection: Specialize your Maven-Compiler-Plugin

You can specify the path to your chosen JDK right within your pom.xml file by tailoring your maven-compiler-plugin:

<plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <executable>/path/to/javac</executable> <!-- Alexa, spin up a new JVM for my compile task --> <fork>true</fork> </configuration> </plugin>

Here, <executable> points to the path of your desired JDK's javac. The fork parameter set to true ensures that a separate JVM is created for the compilation, employing your specified compiler.

Multiple JDKs, no problem: Using Maven Profiles

Rapidly switch between JDK configurations through Maven profiles defined in your settings.xml:

<profile> <id>jdk-11</id> <activation> <activeByDefault>false</activeByDefault> </activation> <properties> <!-- Because we all like to switch our JDK versions sometimes --> <maven.compiler.executable>/path/to/jdk-11/bin/javac</maven.compiler.executable> </properties> </profile>

You activate the profile using the -P flag in the Maven build command:

mvn clean install -Pjdk-11

Loops and hooks: Plugin and tool compatibility

Verify that your chosen JDK is compatible with all plugins and tools in your Maven project. Remember, compatibility is the key to a long-lasting coding relationship:

<plugins> <!-- It's like a first date, making sure everyone gets along --> </plugins>

Making sense in the chaos: Cygwin and Maven JDK settings

For our dear Cygwin users, ensure that your environment variables are set up correctly for peace between Maven, JDK, and any other plugins such as Hudson:

# Cinderella transformation for Cygwin export JAVA_HOME=$(cygpath -m /cygdrive/c/path/to/jdk)

Your very own island: Non-global environment variable changes

Establish your own space in a bustling coding environment by making non-global changes to JAVA_HOME for a Maven build:

# A universe within a universe - a Maven build on its very own island export JAVA_HOME=/path/to/jdk mvn clean install

The grand unveiling: Confirming your JDK version

Confirm he Java version after setting JAVA_HOME and just before you run your Maven tasks. A quick check can save a lot of rework later:

java -version mvn -version