Sunday, September 21, 2008

Compile and test with different JDK with a Maven project

If you ever have the need to compile and run tests of your Maven project with different JDK, here are some tips:

1. The cheapest way



Maven by default use your JAVA_HOME to compile the project and then run the tests.
So if want to run your tests with different jdk, just set your JAVA_HOME to the jdk of your desire. Here's a neat treat in Bash to set an env variable locally to the current process.

Assume you have environment variables JAVA_HOME_15 and JAVA_HOME_16 pointing to their respective jdk1.5 and jdk1.6, you can then do this:


%> JAVA_HOME=$JAVA_HOME_15 mvn install # run with 1.5
%> JAVA_HOME=$JAVA_HOME_16 mvn install # run with 1.6


2. The Maven way

(hairy verbose and messy pom.xml profiles)

This only works with Maven-2.1.0-M1. Failed with 2.0.9 for me

You have to specify which JDK you want for 2 of these plugins: maven-compiler-plugin and maven-surefire-plugin.


<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<executable>${jdk}/bin/javac</executable>
<compilerVersion>1.5</compilerVersion>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<jvm>${jdk}/bin/java</jvm>
<forkMode>once</forkMode>
</configuration>
</plugin>
</plugins>
</build>


So you see I use the property ${jdk} Now we just need to define a profile for each of the JDK we want and set the ${jdk} appropriately.


<profiles>
<profile>
<id>default_jdk</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<jdk>${env.JAVA_HOME}</jdk>
</properties>
</profile>
<profile>
<id>jdk15</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<jdk>${env.JAVA_HOME_15}</jdk>
</properties>
</profile>
<profile>
<id>jdk16</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<jdk>${env.JAVA_HOME_16}</jdk>
</properties>
</profile>
</profiles>


Above I defined 3 profiles. The first one is the default one. It's a trick to set ${jdk} to $JAVA_HOME so that Maven knows what to use if you don't specify any profile to run.

Here how you would activate each profile:


%> mvn install # no profile so by default run tests with JAVA_HOME
%> mvn install -Pjdk15 # run with jdk1.5
%> mvn install -Pjdk16 # run with jdk1.6


Note that in the profile I use env variables JAVA_HOME_15 and JAVA_HOME_16 also. You can of course fill in an absolute path to your JDK.

So there you have it. Took me awhile to figure out.
Post a Comment