How do I activate a profile based on a particular version of java?
Author: Deron Eriksson
Description: This tutorial describes how to activate a profile based on a particular version of java.
Tutorial created using: Windows Vista || JDK 1.6.0_04 || Eclipse Web Tools Platform 2.0.1 (Eclipse 3.3.1)


With mavenSW, it's possible to activate a profile based on different conditions. One such condition is the version of JavaSW that is being run. By specifying an 'activation' section of a profile with a 'jdk' element within it, we we can specify to activate a profile based on a particular version of Java. For example, take the following profiles from settings.xml:

Profiles from settings.xml

...
		<profile>
			<id>my.1.5.profile</id>
			<properties>
				<my.property>this is 1.5</my.property>
			</properties>
			<activation>
				<jdk>1.5</jdk>
			</activation>
		</profile>
		<profile>
			<id>my.1.6.profile</id>
			<properties>
				<my.property>this is 1.6</my.property>
			</properties>
			<activation>
				<jdk>1.6</jdk>
			</activation>
		</profile>
...

The 'my.1.5.profile' profile is activated if Java 1.5 is being run. The 'my.1.6.profile' is activated if Java 1.6 is being run. I'm running Java 1.6, so if I list the active profiles for the 'mytest' project, I should see that the 'my.1.6.profile' is active and the 'my.1.5.profile' is not. This is indeed what we see:

Console output from 'mvn help:active-profiles'

[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'help'.
[INFO] ------------------------------------------------------------------------
[INFO] Building mytest
[INFO]    task-segment: [help:active-profiles] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] [help:active-profiles]
[INFO] 
Active Profiles for Project 'com.maventest:mytest:jar:1.0-SNAPSHOT': 

The following profiles are active:

 - my.1.6.profile (source: settings.xml)



[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: < 1 second
[INFO] Finished at: Sun Feb 10 11:09:55 PST 2008
[INFO] Final Memory: 2M/5M
[INFO] ------------------------------------------------------------------------

In the settings.xml fragment up above, notice that both the 'my.1.5.profile' and 'my.1.6.profile' set a value for a property called 'my.property'. Since the 'my.1.6.profile' is the active profile, we should see that the actual value of 'my.property' available comes from the 'my.1.6.profile'. This is indeed the case, which we can see by examining the effective pomW of the 'mytest' project. Here is the properties section from the effective pom:

  <properties>
    <my.property>this is 1.6</my.property>
  </properties>

This can be a useful way to conditionally set a value of a property. The full 'help:effective-pom' listing for the 'mytest' project is shown here.

Console output from 'mvn help:effective-pom'

[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'help'.
[INFO] ------------------------------------------------------------------------
[INFO] Building mytest
[INFO]    task-segment: [help:effective-pom] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] [help:effective-pom]
[INFO] 
************************************************************************************
Effective POM for project 'com.maventest:mytest:jar:1.0-SNAPSHOT'
************************************************************************************
<?xml version="1.0"?><project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.maventest</groupId>
  <artifactId>mytest</artifactId>
  <name>mytest</name>
  <version>1.0-SNAPSHOT</version>
  <url>http://maven.apache.org</url>
  <build>
    <sourceDirectory>C:\dev\workspace\mytest\src\main\java</sourceDirectory>
    <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>C:\dev\workspace\mytest\src\test\java</testSourceDirectory>
    <outputDirectory>C:\dev\workspace\mytest\target\classes</outputDirectory>
    <testOutputDirectory>C:\dev\workspace\mytest\target\test-classes</testOutputDirectory>
    <resources>
      <resource>
        <directory>C:\dev\workspace\mytest\src\main\resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>C:\dev\workspace\mytest\src\test\resources</directory>
      </testResource>
    </testResources>
    <directory>C:\dev\workspace\mytest\target</directory>
    <finalName>mytest-1.0-SNAPSHOT</finalName>
    <plugins>
      <plugin>
        <artifactId>maven-help-plugin</artifactId>
        <version>2.0.2</version>
      </plugin>
    </plugins>
  </build>
  <repositories>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Maven Repository Switchboard</name>
      <url>http://repo1.maven.org/maven2</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Maven Plugin Repository</name>
      <url>http://repo1.maven.org/maven2</url>
    </pluginRepository>
  </pluginRepositories>
  <dependencies>
    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>2.3</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <reporting>
    <outputDirectory>target/site</outputDirectory>
  </reporting>
  <properties>
    <my.property>this is 1.6</my.property>
  </properties>
</project>
************************************************************************************


[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: < 1 second
[INFO] Finished at: Sun Feb 10 11:10:46 PST 2008
[INFO] Final Memory: 2M/5M
[INFO] ------------------------------------------------------------------------