Friday, September 20, 2019

Post#89.Maven-Java Compiler Version


Maven Java Compiler Version

The default Java compiler version used by Maven is Java 1.5 . 
To make Maven compile your Java code with a newer version of the Java compiler, you need to specify the Java compiler explicitly in your project's POM file (pom.xml).
There are two ways to set the Java compiler version in a Maven POM file:

  1. Via the Maven Java compiler properties.
  2. Via the Maven Java compiler plugin.
Maven Java Compiler Properties

The first, newest and easiest way to set the Java compiler version in your Maven POM file, is via the Maven Java compiler properties. Here is how the Maven Java compiler properties look:

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

These properties have to be included in the properties element of your POM file. I usually have the properties element as the last element in my POM files.

Maven Java Compiler Plugin

The second, oldest, and more verbose to set the Java compiler version in your Maven POM files, is via the Maven Java compiler plugin. Here is how the configuration of the Maven Java compiler plugin looks:

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

Full Maven POM File With Java Compiler Version Set

For your convenience, here is a full Maven POM file with the Java compiler version set using both of the above mechanisms. Please note, that only one of the mechanisms should be used in your POM file. The example only shows both examples so you can see where in the POM file they are to be placed.

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>com.nanosai</groupId>
    <artifactId>grid-ops</artifactId>
    <version>0.8.0</version>
    <packaging>jar</packaging>
 
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
    </properties>
 
</project>


No comments:

Post a Comment