2
votes

Is there any way I can force maven to compile using java 1.7 standard without using the maven-compiler-plugin? I have read about the JAVA_HOME being not set properly, but I don't think this is the case. I have added the following line in the mvn script:

echo $JAVA_HOME

Below is the output of mvn --version

Apache Maven 3.0.4

Maven home: /usr/share/maven

Java version: 1.7.0_45, vendor: Oracle Corporation

Java home: /usr/lib/jvm/java-7-oracle/jre

Default locale: en_US, platform encoding: UTF-8

OS name: "linux", version: "3.11.0-12-generic", arch: "i386", family: "unix"

One problem would be that $JAVA_HOME is set to /usr/lib/jvn/java-7-oracle while manven reports it to be $JAVA_HOME/jre.

The question is is there a way to configure maven tu use java 1.7 (similar to javac -target 1.7) ?

3

3 Answers

2
votes

You may configure the compiler plugin by passing appropriate -D... arguments when invoking maven. In your case this might be

mvn -Dmaven.compiler.target=1.7 compile

But i'd consider this an antipattern, as this makes your build dependent on additional parameters which must be documented. It is way better to define a proper configuration in some parent pom.

See compiler:compile for complete description of the maven-compiler-plugin

4
votes

The question is is there a way to configure maven tu use java 1.7 (similar to javac -target 1.7) ?

There is a way, and that is by configuring maven-compiler-plugin. That is what Maven uses for compiling, so that's what you need to configure.

If you insist on using javac, see what maven compiler plugin manual says:

Since 3.0, the default compiler is javax.tools.JavaCompiler (if you are using java 1.6) and is used to compile Java sources. If you want to force the plugin using javac, you must configure the plugin option forceJavacCompilerUse.

but even in that case it would be also about configuring the maven-compiler-plugin.

0
votes

Based on http://maven.apache.org/plugins/maven-compiler-plugin/testCompile-mojo.html, it looks like you can set these properties can be set without "poluting" your pom.xml file by setting in the command line the properties maven.compiler.target and maven.compiler.source.

So, a command like below should solve your problem. mvn install -Dmaven.compiler.target=1.7 -Dmaven.compiler.source=1.7

The default version for these atributes is 1.5. You can also modify the mvn script and add in the exec line these two atributes.