set the default jdk for maven-compiler-plugin

In your pom specify the following to set the compiler to JDK5:

<properties>
    <maven.compiler.source>1.5</maven.compiler.source>
    <maven.compiler.target>1.5</maven.compiler.target>
</properties>

i.e.

<project>
    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
    ...
  </project>

I specify mine prior to the dependencies, although so long as its part of the project element you should be able to place it anywhere inside the pom.

I ran into a similar issue with Maven previously, this fixed it for me. Essentially what this does is set the -source and -target flags to the value specified and passes it to the compiler. Newer plugins default to 1.5.

In order to use the default approach without specifying the properties, you will need to be running a later version of Maven.

I suppose you could also set up a template via your IDE to include this in all new pom files. Of course the actual implementation would depend upon your IDE...

See The apache maven compiler plugin documentation as well as Setting the source and compiler examples for more details.


I tried the maven-compiler-plugin approach and it proved cumbersome as there are plugins like maven-surefire-plugin and maven-cobertura-plugin which still fail due to incompatibility issues.

The better approach was to use maven-toolchain-plugin.

Step 1 Create /.m2/toolchains.xml

<?xml version="1.0" encoding="UTF8"?>
<toolchains>
<!-- JDK toolchains -->
<toolchain>
    <type>jdk</type>
    <provides>
        <version>1.8</version>
        <vendor>sun</vendor>
    </provides>
    <configuration>
          <jdkHome>/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home</jdkHome>
    </configuration>
</toolchain>
<toolchain>
    <type>jdk</type>
    <provides>
        <version>1.7</version>
        <vendor>sun</vendor>
    </provides>
    <configuration>
        <jdkHome>/Library/Java/JavaVirtualMachines/jdk1.7.0_67.jdk/Contents/Home</jdkHome>
    </configuration>
</toolchain>
<toolchain>
    <type>jdk</type>
    <provides>
        <version>1.6</version>
        <vendor>apple</vendor>
    </provides>
    <configuration>
        <jdkHome>/Library/Java/JavaVirtualMachines/1.6.0_65-b14-462.jdk/Contents/Home</jdkHome>
    </configuration>
</toolchain>

<!-- other toolchains -->
<!--
<toolchain>
    <type>netbeans</type>
    <provides>
        <version>5.5</version>
    </provides>
    <configuration>
        <installDir>/path/to/netbeans/5.5</installDir>
    </configuration>
</toolchain>
-->

Step 2 Add maven-toolchain-plugin to plugins section in your project pom.xml.

*If using maven 3, ensure this goes into pluginManagement as well *

   <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-toolchains-plugin</artifactId>
            <version>1.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>toolchain</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <toolchains>
                    <jdk>
                        <version>1.7</version>
                        <vendor>sun</vendor>
                    </jdk>
                </toolchains>
            </configuration>
        </plugin>

Voila all your other plugins pick up the right JDK. Hope it helps. I spent almost half day on this exact issue today.


Default value for source and target was 1.3 in older versions of maven-compiler plugin (like 2.0.2-6). Use at least a 3.0 version of the Maven compiler plugin to get this back to the original behaviour, or just configure that plugin to get source and target to appropriate values.

Tags:

Java

Maven