view content/Maven/cross-jdk-project-files.md @ 74:8de1cc117d89 pelican_comment_system

merge changes from default branch
author Dirk Olmes <dirk@xanthippe.ping.de>
date Sun, 03 Jan 2016 00:54:02 +0100
parents 4cd9b65e10e4
children 1d9382b0329b
line wrap: on
line source

Title: Cross JDK project files with Maven
Date: 2008-03-19
Lang: en

Recently we switched the Mule build to be [based on JDK5](http://www.mulesource.org/display/MULECDEV/Building+from+Source) 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](http://maven.apache.org/plugins/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

    :::xml
    <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:

    :::xml
    <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