Reputation: 1
When Running install from Eclipse we have no issues as the compiler version is set to 1.8.
When running mvn install in terminal we get the following error.
ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project servicehelper: Compilation failure
[ERROR] try-with-resources is not supported in -source 1.5
[ERROR] (use -source 7 or higher to enable try-with-resources)
[ERROR]
When using mvn install -X we're seeing -target 1.5
However here are my java and javac versions
mvn -version
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-03T14:39:06-05:00)
Maven home: /usr/local/Cellar/maven/3.5.0/libexec
Java version: 1.8.0_131, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.12.5", arch: "x86_64", family: "mac"
java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
javac -version
javac 1.8.0_131
I seems like from every version i run that we should have 1.8 compliance but maven target continues to try and install with 1.5 compliance.
Upvotes: 0
Views: 3706
Reputation: 166
You can try adding the tag java.version
on your pom.xml
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
Upvotes: 0
Reputation: 97467
The way to go is first to define the version of the used plugin which means:
<project>
<build>
<pluginManagement>
<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>
</pluginManagement>
</build>
</project>
You should always pin plugin versions which means all used plugin in your build.
The other option to define the java version (source/target) can be done via:
<project>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Or you can omit the pluginManagement if you have already pinned the plugin version via a corporate pom parent and only give the above properties.
If you have to run Maven with another JDK than you need to compile/test your code you need to go via toolchains
Or you can define the compiler to use via fork option which means to hard code the location of your JDK in your pom file which is not a good idea.
Upvotes: 1
Reputation: 871
Add this plugin to your pom.xml file.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<compilerVersion>1.8</compilerVersion>
</configuration>
</plugin>
Upvotes: 2