How to compile Maven project with different JDK version?

By default, Maven 2 uses JDK 1.4, Maven 3 uses JDK 1.5 to compile the project, which is very old. Fortunately, Maven comes with a Compiler Plugin, which tell Maven to compile the project source with a particular JDK version.

Solution

Configure the plugin compiler directly. (Tested with Maven 2 and 3)

pom.xml

<project>
  
  <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>

</project>

Alternatively, configure via the property values. (Tested with Maven 3)

pom.xml

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

References

  1. Maven Compiler plugin
  2. How to tell Maven to use Java 8

One comment on “How to compile Maven project with different JDK version?

  1. To create a maven project with jdk 1.4.2_16 which version of maven should be used.
    Also please provide me maven download link for the same.

    JFI below are current versions
    U:\>mvn -version
    U:\
    Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-18T00:03:14+05:30)
    Maven home: C:\work\apache-maven-3.5.4\bin\..
    Java version: 1.8.0_172, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_172\jre
    Default locale: en_US, platform encoding: Cp1252
    OS name: “windows 10”, version: “10.0”, arch: “amd64”, family: “windows”

    U:\>java -version
    java version “1.8.0_172”
    Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
    Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)

    U:\>javac -version
    javac 1.8.0_172

    Let me know if i have to change anything else.

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *