view content/Maven/cross-jdk-project-files.md @ 112:cf31bf5fce72 default tip

Author of the blog post as mail header for efficient spam filtering
author Dirk Olmes <dirk.olmes@codedo.de>
date Tue, 06 Sep 2022 07:04:11 +0200
parents 1d9382b0329b
children
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

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