I'm using maven in IntelliJ, JDK1.8, maven 3.2.5. Got compilation error: use -source 7 or higher to enable diamond opera. details are as follows:
[ERROR] COMPILATION ERROR : [INFO] ------------------------------------------------------------- [ERROR] TrainingConstructor.java:[31,55] diamond operator is not supported in -source 1.5 (use -source 7 or higher to enable diamond operator) [ERROR] DTM.java:[79,21] try-with-resources is not supported in -source 1.5 (use -source 7 or higher to enable try-with-resources) [ERROR] ticons.java:[53,44] diamond operator is not supported in -source 1.5 (use -source 7 or higher to enable diamond operator)
Any suggestions? Is there any other configuration to set this -source level? seems it doesn't use java 1.8.
Maven Compilation Error: Use -source 7 or higher to enable diamond operator. This error happens only in Maven command line compile or in your IDE if you use new features of JDK that is not available outside 1.5. The reason is that MAVEN compiler default for source and target is 1.5 JDK version.
The error message indicates that Maven is unable to find the Java compiler, which comes only with a JDK and not with a JRE.
Check how your maven-compiler-plugin
is configured, it should use java version 7 or higher:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin>
For a more complete answer see the one below.
<properties> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties>
<build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> ...
The problem arises because
[...] at present the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with. If you want to change these defaults, you should set source and target as described in Setting the -source and -target of the Java Compiler.
Maven Compiler Plugin Introduction (until version 3.3)
and with recent Maven versions:
Also note that at present the default source setting is 1.6 and the default target setting is 1.6, independently of the JDK you run Maven with. You are highly encouraged to change these defaults by setting source and target as described in Setting the -source and -target of the Java Compiler.
Maven Compiler Plugin Introduction
That's why changing the JDK has no effect on the source level. So you have a couple of way to tell Maven what source level to use.
If you set a target 1.7 like in this example be sure that the mvn command is actually launched with a jdk7 (or higher)
Usually IDEs use maven pom.xml file as a source of project configuration. Changing the compiler settings in the IDE not always has effect on the maven build. That's why, the best way to keep a project always manageable with maven (and interoperable with other IDEs) is edit the pom.xml files and instruct the IDE to sync with maven.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With