The Alias Maven Plugin allows to configure shell aliases. In the past we provided two aliases to switch between different Java versions to start Maven build processes according to their requirements.

There is a better way to do this in Maven. But in the end there are – as often – some problems to fix. The Alias Maven Plugin may help!

Old Alias Configuration

With our alias configuration the created aliases were:

 j6  = set JAVA_HOME=E:\java\edition\jdk1.6.0_25x64
 j7  = set JAVA_HOME=E:\java\edition\jdk1.7.0

The trick has been to change the JAVA_HOME environment variable that was part of the PATH. Unfortunately, supposedly due to an obscure operating system update, this configuration does not work any longer. Fortunately, there is a Maven way to get the same result that is even more convenient than the alias we had used so far. It is called toolchains.

Maven Toolchains

The use of Maven Toolchains is well documented. Here is a very brief summary of the steps to configure toolchains for a Maven project:

toolchains.xml

Add a toolchains.xml to your .m2 folder in your home folder (right next to your settings.xml):

<?xml version="1.0"?>
 
<toolchains>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>1.7</version>
      <vendor>sun</vendor>
      <id>1.7</id>
    </provides>
    <configuration>
      <jdkHome>D:\\java\\edition\\jdk1.7.0_05</jdkHome>
    </configuration>
  </toolchain>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>1.6</version>
      <vendor>sun</vendor>
      <id>1.6</id>
    </provides>
    <configuration>
      <jdkHome>D:\\java\\edition\\jdk1.6.0_25x64</jdkHome>
    </configuration>
  </toolchain>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>1.5</version>
      <vendor>sun</vendor>
      <id>1.5</id>
    </provides>
    <configuration>
      <jdkHome>D:\\java\\edition\\jdk1.5.0_22</jdkHome>
    </configuration>
  </toolchain>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>1.4</version>
      <vendor>sun</vendor>
      <id>1.4</id>
    </provides>
    <configuration>
      <jdkHome>D:\\java\\edition\\j2sdk1.4.2_19</jdkHome>
    </configuration>
  </toolchain>
</toolchains>

maven-toolchains-plugin

Then add the Apache Maven Toolchains Plugin to the plugin’s section of your project’s pom.xml file:

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

Lifecycle Mapping

You may also want to add the plugin to your lifecycle mappings to get rid of any warnings in your IDE. This is specified in your pom.xml in the pluginManagement section:

<pluginManagement>
  <plugins>
     <plugin>
      <groupId>org.eclipse.m2e</groupId>
      <artifactId>lifecycle-mapping</artifactId>
      <version>1.0.0</version>
      <configuration>
        <lifecycleMappingMetadata>
          <pluginExecutions>
            <pluginExecution>
              <pluginExecutionFilter>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-toolchains-plugin</artifactId>
                <versionRange>[0.0.0,)</versionRange>
                <goals>
                  <goal>toolchain</goal >
                </goals>
              </pluginExecutionFilter>
              <action>
                <ignore/>
              </action>
            </pluginExecution>
          </pluginExecutions>
        </lifecycleMappingMetadata>
      </configuration>
    </plugin>
  </plugins>
</pluginManagement>

Commandline Support

Since Maven 3 (alpha3) the location of the toolchain.xml may be specified on the commandline.

-t,--toolchains <arg>     Alternate path for the user toolchains file

This may be especially useful if you want to use a specific toolchains configuration on your build server for certain builds.

Reporting Projects

So everything just fine? No, unfortunately not. Not all plugins are toolchains-aware, especially our reporting plugins :-(, like the one of smartics Exceptions or smartics Properties, do not support toolchains.

If you start the build process on a project using exceptioncodes report and requiring Java 7 with a Java 6 VM, the following error is printed to the console:

Caused by: org.apache.maven.plugin.PluginContainerException: 
An API incompatibility was encountered while executing 
  de.smartics.exceptions:exceptioncodes-maven-plugin:0.7.0-SNAPSHOT:export: 
java.lang.UnsupportedClassVersionError: de/smartics/exceptions/sample/MyCode : 
Unsupported major.minor version 51.0

has been started with. What we should have done is fork another VM according to the toolchain configuration and evaluate the project classes there.

So is this the end? Certainly not!

You may launch Maven with the most recent VM and due to backward compatibility get the desired result. But then you have – again – to fiddle with JAVA_HOME and the PATH. Reenter the Alias Maven Plugin …

Alias Plugin

Now the Alias Maven Plugin comes handy to let us change the Java version in our shell more easily. Therefore we made a small change to always adjust the PATH when we change JAVA_HOME.

doskey j6  = set JAVA_HOME=%JAVA_HOME_6%^&^& set PATH=%JAVA_HOME_6%\bin;%PATH%
doskey j7  = set JAVA_HOME=%JAVA_HOME_7%^&^& set PATH=%JAVA_HOME_7%\bin;%PATH%

So please update to version 1.0.7 of config-smartics-alias to use this feature – until we finally fixed the toolchains issue in our reporting plugins.


Link

Link

Posts

Tagcloud

Loading tagcloud ...