Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Won't Compile Java 7 with 1.7 JDK

Tags:

spring

maven

I must be the anti-Maven since every time I try to use it I spend a lot of time struggling and then just give up. My latest is I can not compile Java 7 source code despite having a 1.7 JDK.

Here is the output of mvn compile. The interesting thing is that when I was using Maven 3.0.4, the error message said -source 1.5 instead of -source 1.6 with Maven 3.2.1

[ERROR] .../src/main/java/pox/common/ServiceResponse.java:[300,43] diamond operator is not supported in -source 1.6
[ERROR] (use -source 7 or higher to enable diamond operator)
[ERROR] .../src/main/java/pox/common/ServiceRequest.java:[185,43] diamond operator is not supported in -source 1.6
[ERROR] (use -source 7 or higher to enable diamond operator)

As you can see I am using a recent version of Maven and Maven is using Java 1.7.

$ mvn -v
Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T12:37:52-05:00)
Maven home: /usr/local/maven
Java version: 1.7.0_40, vendor: Oracle Corporation
Java home: /usr/local/jdk1.7.0_40-x64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "2.6.18-371.4.1.el5", arch: "amd64", family: "unix"

I think the mvn script is adding the /jre since my environment variable does not specify it.

$ echo -e "$JAVA_HOME\n$M2_HOME"
/usr/local/java7-x64
/usr/local/maven

$ which javac
/usr/local/java7-x64/bin/javac

I was writing some POJOs that I wanted to Springify so I decided to use Maven and found this getting started with Maven on the Spring site.

http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#getting-started

I copied the pom.xml and source from the site into my STS/Eclipse project that only contained Java source and tried to compile. STS has no problem compiling any of the source code in src/main/java. I can remove the code that is dependent on Java 1.7 and get Maven to compile.

Every thing I have found so far implies I do not have my JAVA_HOME set correctly but I am pointing to a 1.7 JDK.

like image 498
Wes Avatar asked Dec 19 '22 13:12

Wes


1 Answers

Well I had a similar problem, I had a project with maven and eclipse but there were problems to compile because the default level source of maven was java 1.5 and of course my code was in java 1.6, so i had to add the compiler plugin of maven to set the right version of my source of code, here is my configuration, so I guess you should change only the 1.6 to 1.7

<build>
    <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.1</version>
          <configuration>
            <source>1.6</source>
            <target>1.6</target>
          </configuration>
        </plugin>
    </plugins>
</build>
like image 159
neo_lestat Avatar answered Dec 22 '22 01:12

neo_lestat