6
votes

I am using maven and would like to compile my code using JDK 1.8.
So I installed JDK 1.8
Then ( this is on windows ) set the 'path' variable and also 'JAVA_HOME'

PATH=C:\construction\tools\ibm_sdk80\bin;C:\construction\tools\apache-maven-3.2.5\bin;

the java home:

C:\Users\satish.marathe>set JAVA_HOME
JAVA_HOME=C:\construction\tools\ibm_sdk80

java version give me:

C:\Users\satish.marathe>java -version
java version "1.8.0"
Java(TM) SE Runtime Environment (build pwa6480sr1fp10-20150711_01(SR1 FP10))
IBM J9 VM (build 2.8, JRE 1.8.0 Windows 7 amd64-64 Compressed References 20150630_255633 (JIT enabled, AOT enabled)
J9VM - R28_jvm.28_20150630_1742_B255633
JIT - tr.r14.java_20150625_95081.01
GC - R28_jvm.28_20150630_1742_B255633_CMPRSS
J9CL - 20150630_255633)
JCL - 20150711_01 based on Oracle jdk8u51-b15

javac version tells me :

C:\Users\satish.marathe>javac -version
javac 1.8.0-internal

So everything seems to point to java 1.8
Now I compile my project using :

mvn clean install  

However when I check the compiled classes I am seeing that the major version is 51 - which means it is Java 1.7
For those who would like to know how the java version is found in a class file ! :)

javap -verbose <<your_package>>.<<your_class>> | findStr "major"  

EDIT1:
i forgot to include output of maven -version so here it is:

C:>mvn -version Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-14T22:59:23+05:30)
Maven home: C:\construction\tools\apache-maven-3.2.5\bin..
Java version: 1.8.0, vendor: IBM Corporation
Java home: C:\construction\tools\ibm_sdk80\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "dos"
C:>

OK - so my question is :
I do not have any entry for maven_compiler_plugin in my pom
so is that required ?
without that entry - shouldnt maven simply pick my java version as 1.8 from system and compile the code ( which it does not seem to be doing apparently )
Thanks

3
possible duplicate of Finding out which compiler Maven usesdotvav
i would not look at it as a duplicate - since everything seems to indicate that maven should be using java 1.8 but seems to be using 1.7satish marathe

3 Answers

6
votes

You can use mvn -version to see which java version is used by Maven.

Your question: I do not have any entry for maven_compiler_plugin in my pom so is that required ?

Answer: If you want a different compiler version to compile your sources then you need to specify that using the maven-compiler-plugin:

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

For further information if you look at the maven website: maven-compiler-plugin

By default The Compiler Plugin is used to compile the sources of your project. Since 3.0, the default compiler is javax.tools.JavaCompiler (if you are using java 1.6) and is used to compile Java sources.

4
votes
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
 <source>1.8</source>
 <target>1.8</target>
</configuration>
2
votes

You can use maven-compiler-plugin, replace JAVA_HOME with your local java sdk path.

      <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <encoding>UTF-8</encoding>
                <compilerId>javac</compilerId>
                <source>1.8</source>
                <target>1.8</target>
                <executable>JAVA_HOME/bin/javac</executable>
                <showDeprecation>true</showDeprecation>
                <showWarnings>true</showWarnings>
                <verbose>true</verbose>
                <fork>true</fork>
            </configuration>
        </plugin>