Main Tutorials

How to tell Maven to use Java 8

In pom.xml, defined this maven.compiler.source properties to tell Maven to use Java 8 to compile the project.

1. Maven Properties

Java 8

pom.xml

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

Java 7

pom.xml

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

2. Compiler Plugin

Alternative, configure the plugin directly.

pom.xml

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

References

  1. Maven – Setting the -source and -target of the Java Compiler
  2. How to compile Maven project with different JDK version?

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
7 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
sachin mishra
7 months ago

thank you

Zol
1 year ago

Hi, having target and source for 1.8 (java8) at project and compiler, However mvn versions:display-dependency-updates suggests me to upgrade to spring6 and logback 1.4.5. Are you aware flags or tricks to get all jars included a war match the needs of a target platform? thx

Munivelu
4 years ago

Hi, I am using java8 , maven 3.5.2 version and using below configuration in pom.xml for compile java.

org.apache.maven.plugins
maven-compiler-plugin
3.8.0

1.8
1.8

Question: Showing two versions like 49, 52 when I check java major version using javap. Requesting you to suggest me what could be the wrong?

Regards,
Munivelu.

Prem
5 years ago

Hello mkyong,
I did use this for java 8. But it is showing error :
lambda expressions are not supported in -source 1.5
[ERROR] (use -source 8 or higher to enable lambda expressions)

This is my pom.xml :

4.0.0

com.example.project
Proect-MVN-Parent
0.0.1-SNAPSHOT

WebApp

false
1.8
1.8

war
Project Maven Webapp

junit
junit
3.8.1
test

org.springframework
spring-webmvc
${spring.version}

org.springframework
spring-web
${spring.version}

org.springframework
spring-tx
3.2.4.RELEASE

javax.ws.rs
javax.ws.rs-api
2.0-m03

com.fasterxml.jackson.core
jackson-core
2.6.3

com.fasterxml.jackson.core
jackson-databind
2.6.3

org.springframework
spring-jdbc
3.0.3.RELEASE

javax.servlet
javax.servlet-api
${servlet.version}
provided

org.hibernate
hibernate-core
${hibernate.version}

org.hibernate
hibernate-entitymanager
${hibernate.version}

org.springframework.data
spring-data-jpa
1.10.5.RELEASE

aopalliance
aopalliance
1.0

org.aspectj
aspectjrt
1.8.11

org.aspectj
aspectjweaver
1.8.11

YLogAPI

org.apache.maven.plugins
maven-compiler-plugin
3.6.1

${maven.compiler.source}
${maven.compiler.target}

5 years ago
Reply to  Prem

Are you compiling with idea or mvn by itself form command line?

srini
6 years ago

With jdk 8 tomcat integration getting lots of issues

NMNaufaldo
4 years ago

I’m using IntelliJ, i must klik File -> Invalidate Chaces / Restart