6
votes

I was recently stung by some code making it through our deployment cycle without throwing any compile errors when it should have (we thought)...

The code in question being using the new static method Integer.compare which is since Java 1.7.

The server environment is run on Java 1.6. While our development environments have Java 1.7 installed.

Our assumption was that setting the project preferences to JavaSE-1.6 compliance would at least give us compile warnings on the code in question, however no warning or error is visible in eclipse.

Project > properties > java compiler > JDK Compliance > Use compliance from execution environment 'JavaSE-1.6' on the java build path

Secondarily to that, we use maven to compile the final deployment. The pom is clearly directed to comply with the 1.6 compiler:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
        <source>1.6</source>
        <target>1.6</target>
        <optimize>true</optimize>
    </configuration>
</plugin>

However the maven build runs successfully with the problem code.

How can I tell both maven and eclipse to fail when code will not work in an earlier Jvm than it is being compiled by?

Thanks, Paul.

2
I remember I have faced similar situation before (but it was 1.5 vs 1.6) but at that time it works as what you expected. I doubt if there is any bug in JDK causing that? - Adrian Shum

2 Answers

2
votes

Use the maven animal sniffer plugin to tell you when you use APIs that aren't backward compatible. I'm also told that Java 1.7 has a feature for this, but I have no personal experience with it.

2
votes

Install java 1.6 in the development environment, then right click on the project in eclipse an go to Properties->Java Build Path. Go to the Libraries tab and remove the java 1.7 JRE, then add the java 1.6 JRE.

I'm not familiar enough with maven to answer that half.