logo

How To Set Source/Target Level In Maven

Error

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project <your-project-name>: Compilation failure
[ERROR] /path/to/code.java:[51,46] diamond operator is not supported in -source 1.5
[ERROR] (use -source 7 or higher to enable diamond operator)

Reason

If maven-compiler-plugin is not specified, a default will be used(in this case 3.1, which sets -source to 1.5 by default)

Solution

Add maven-compiler-plugin to pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.3</version>
    <configuration>
        <source>7</source>
        <target>7</target>
    </configuration>
</plugin>

this will set -source and -target of the Java Compiler