How to Specify Java Version in Maven and Differences Between Compiler Properties & Plugin

Muhammad Zeeshan Feb 02, 2024
  1. Differences Between Compiler properties and plugin
  2. Specify Java Version in Maven
How to Specify Java Version in Maven and Differences Between Compiler Properties & Plugin

Today, we will discuss the distinctions between Java compiler properties and the compiler plugin. We’ll also learn about specifying the Java version in Maven.

Differences Between Compiler properties and plugin

Both are similar, in fact maven-compiler-plugin’s <source> tag uses <maven.compiler.source> property and <target> uses <maven.compiler.target> property.

We are generally satisfied to use properties, but plugin is more versatile since it has a wide variety of configurations we can set up in the wild. For example, we have fork, annotationProcessors, compilerArgs, and a few other tags.

In the compile step, adding specific parameters can be accomplished with the help of a plugin. For example, the plugin can specify the Java JDK version number and bootclasspath.

Specify Java Version in Maven

Throughout this section, we will examine several ways that we can use to specify the Java version in Maven projects. A maven is a tool utilized during the construction and packaging of Java applications.

We need to ensure that the Java compiler is specified explicitly in the pom.xml file for our project if we want Maven to run our Java compilations using a more recent version of the Java compiler.

There are two different ways to set the Java compiler version in a Maven pom.xml file, with Java 8 and the earlier version. The following sections will each explain one of these two approaches to setting the Java compiler version in Maven.

1. Maven Compiler properties

Maven’s Java version can be set using the following properties, which can be configured. The Maven compiler properties offer the simplest and most straightforward method for specifying the Java compiler version used by our Maven pom.xml file.

Both maven.compiler.source and maven.compiler.target are required. These two properties should be added to the pom.xml file as follows:

<properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
</properties>

2. Maven Compiler plugin

When configuring the Maven compiler plugin in the pom.xml file, add the source and target elements to the configuration. Use the code below if Java 8 is required.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

3. The compilerArgs Option in pom.xml File

Here we will configure the Java version by putting compiler parameters into the compilerArgs option. The following example delivers the same outcome as those that were produced earlier.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <compilerArgs>
            <arg>-source</arg>
            <arg>1.8</arg>
            <arg>-target</arg>
            <arg>1.8</arg>
        </compilerArgs>
    </configuration>
</plugin>
Muhammad Zeeshan avatar Muhammad Zeeshan avatar

I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.

LinkedIn

Related Article - Java Version