2

Possible Duplicate:
Java version mismatch in Maven

I have a project which is configured to use Java 1.5 with maven:

mvn help:effective-pom

...
  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.0.2</version>
    <configuration>
      <source>1.5</source>
      <target>1.5</target>
    </configuration>
...

My machine has Java 1.6 installed, and Maven is using it:

mvn -v

...
Apache Maven 2.2.1 (r801777; 2009-08-06 20:16:01+0100)
Java version: 1.6.0_21

I have imported the project into Eclipse (using mvn eclipse:eclipse), and it is building it with Java 1.5, as specified in the POM.

Now if I mark an interface implementation method with @Override, which is disallowed in Java 1.5 but supported in Java 1.6, then Maven will build the project without errors, but Eclipse will mark the annotation as a compile-time error and refuse to build the project.

How can I either get Maven to really compile the project with Java 1.5, or get Eclipse to compile the project using the same settings as Maven?

Community
  • 1
  • 1
Rich
  • 15,048
  • 2
  • 66
  • 119
  • 3
    Latest compiler plugin is 2.5.1 – artbristol Jul 04 '12 at 09:16
  • 2
    Does http://stackoverflow.com/a/11138745/739090 help? – Crollster Jul 04 '12 at 09:29
  • Try to use the m2e plugin instead of using `maven-eclipse-plugin` and see if there is a difference. http://wiki.eclipse.org/M2E_FAQ#Maven_Integration_for_Eclipse_vs._Maven_eclipse:eclipse_plugin – maba Jul 04 '12 at 10:00
  • I don't think this is a duplicate of the linked question, but I don't have enough rep to vote otherwise. The answers so far have been quite helpful, so I guess there's no great loss if it is closed now (although I haven't resolved my problem yet) – Rich Jul 12 '12 at 10:41

1 Answers1

1

It seems that newer versions of the JDK 1.5 ignore the @Override if it is in an interface implementation method: https://stackoverflow.com/a/2336850/256245 I guess that JDK 1.6 does the same if it is compiling using target 1.5. That´s the reason Maven does not complain about it.

To check if Maven is really compiling to Java 1.5, you should use Animal Sniffer Plugin:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>animal-sniffer-maven-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>check-java15-sun</id>
            <phase>test</phase>
            <goals>
                <goal>check</goal>
            </goals>
            <configuration>
                <signature>
                    <groupId>org.codehaus.mojo.signature</groupId>
                    <artifactId>java15-sun</artifactId>
                    <version>1.0</version>
                </signature>
            </configuration>
        </execution>
    </executions>
</plugin>

Since Eclipse have its own compiler implementation, I guess you would have to remove the @Override annotation from interface implementation methods to make him happy. The removal will not do any harm since the annotation was being ignored by the JDK.

Community
  • 1
  • 1
Fabricio Lemos
  • 2,905
  • 2
  • 31
  • 31
  • Strangely, adding a reference to NavigableMap does not produce compiler errors in either Maven or Eclipse. Thanks for that link, I'll see if I can update the Java 1.5 that Eclipse is using. – Rich Jul 04 '12 at 18:46
  • My bad about NavigableMap. The correct way to verify Java 1.5 compatibility is using Animal Sniffer Plugin. I edited the answer to reflect this. – Fabricio Lemos Jul 05 '12 at 14:43