Main Tutorials

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

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Santosh
5 years ago

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.