Cross JDK project files with Maven

19.03.2008 by Dirk Olmes

Recently we switched the Mule build to be based on JDK5 for some modules. This requires the Maven build to be JDK aware. While the JDK auto-activation of profiles get you to build the project from the commandline, IDE integration was a bit of pain.

Now that the maven-eclipse-plugin has released v2.5 there’s finally support for JDK aware project files. You can add the following to your POMs in order to select a specific JDK for your project

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-eclipse-plugin</artifactId>
    <configuration>
        <classpathContainers>
            <classpathContainer>
                org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/thisismyjdk
            </classpathContainer>
        </classpathContainers>
    </configuration>
</plugin>

While this solution is better than nothing already I really don’t like how you’re forced to hard-code the JDK names into the POM. Fortunately, Maven allows you to specify properties and property resolution works in this case, too. So all you have to do is to define a property placeholder instead of your JDK name in config above, like this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-eclipse-plugin</artifactId>
    <configuration>
        <classpathContainers>
            <classpathContainer>
                org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/${jdk}
            </classpathContainer>
        </classpathContainers>
    </configuration>
</plugin>

Then go and define a property in the pom (for the default value). Anyone who uses a different JDK can specify the name to use on the commandline now using

mvn -Djdk=sun-jdk-1.4.2.14 eclipse:eclipse

Comments

There are no comments yet.

Leave a comment
Your name:
Comment: