Maven profiles - conditional activation

Maven profiles can be activated in one of the following ways
  • maven command
    mvn {groupId}:{artifactId}:{goal commalist} -P profile-1,profile-2
  • Through the IDE (e.g., eclipse launch)
  • In user or in shared Maven settings
  • In build profile declaration.

In settings

In Maven 3.0, profile activation of the profile can be embedded within the settings declaration.
in the following fashion
<settings>
  .....
  <activeProfiles>
    <activeProfile>profile-xyz</activeProfile>
  </activeProfiles>
  .....
</settings>

In build profile

As previously stated, build profiles can be declared
  • Per Project
    Defined in the POM itself (pom.xml), as exemplified above.
  • Per User
    Defined in the Maven-settings(%USER_HOME%/.m2/settings.xml
  • Shared
    Defined in the global Maven-settings (%M2_HOME%/conf/settings.xml)
Activation conditions of a build profile can embedded in the build profile itself in this manner
<profile>
  .....
  <activation>
  {condition specification}
  </activation>
  .....
</profile>
specifying conditions based on
  • explicitly declared properties
  • implicitly defined properties such as build properties and environment variables
  • OS settings
  • presence of files

Activation due to a property value of xyz-name satisfies the condition {xyz-name == xyz-value}.
<activation>
  <property>
    <name>xyz-name</name>
    <value>xyz-value</value>
  </property>
</activation>

Activation due to a property having been defined regardless of its value.
<activation>
  <property>
    <name>xyz-name</name>
  </property>
</activation>

Activation due to a property NOT having been defined.
<activation>
  <property>
    <name>!xyz-name</name>
  </property>
</activation>

Activation if JDK version is 1.4
<activation>
  <jdk>1.4</jdk>
</activation>

Activation requires JDK minimum version 1.4 and maximum version 1.7
<activation>
  <jdk>[1.4,1.7)</jdk>
</activation>

Activation due to absence or presence of a file
<activation>
  <file>
    <missing>src/main/webapp/WEB-INF/list.txt</missing>
  </file>
</activation>
<activation>
  <file>
    <exists>src/main/webapp/WEB-INF/list.txt</exists>
  </file>
</activation>

Actvation based on OS attributes
<activation>
  <os><family>Linux</family></os>
</activation>

<activation>
  <os>
    <name>Windows XP</name>
    <family>Windows</family>
    <arch>x86</arch>
    <version>5.1.2600</version>
  </os>
</activation>

This is the complete algorithm determining activation due to OS attributes, by returning the truth of retValue (hi-lited at the end of the algorithm)
if ( family != null ){
  if ( family.equalsIgnoreCase( FAMILY_WINDOWS ) ){
    isFamily = OS_NAME.indexOf( FAMILY_WINDOWS ) > -1;
  }
  else if ( family.equalsIgnoreCase( FAMILY_OS2 ) ){
    isFamily = OS_NAME.indexOf( FAMILY_OS2 ) > -1;
  }
  else if ( family.equalsIgnoreCase( FAMILY_NETWARE ) ){
    isFamily = OS_NAME.indexOf( FAMILY_NETWARE ) > -1;
  }
  else if ( family.equalsIgnoreCase( FAMILY_DOS ) ){
    isFamily = PATH_SEP.equals( ";" ) && !isFamily( FAMILY_NETWARE );
  }
  else if ( family.equalsIgnoreCase( FAMILY_MAC ) ){
    isFamily = OS_NAME.indexOf( FAMILY_MAC ) > -1;
  }
  else if ( family.equalsIgnoreCase( FAMILY_TANDEM ) ){
    isFamily = OS_NAME.indexOf( "nonstop_kernel" ) > -1;
  }
  else if ( family.equalsIgnoreCase( FAMILY_UNIX ) ){
    isFamily = PATH_SEP.equals( ":" ) && !isFamily( FAMILY_OPENVMS )
      && ( !isFamily( FAMILY_MAC ) || OS_NAME.endsWith( "x" ) );
  }
  else if ( family.equalsIgnoreCase( FAMILY_WIN9X ) ){
    isFamily = isFamily( FAMILY_WINDOWS )
      && ( OS_NAME.indexOf( "95" ) >= 0 || OS_NAME.indexOf( "98" ) >= 0
        || OS_NAME.indexOf( "me" ) >= 0 || OS_NAME.indexOf( "ce" ) >= 0 );
  }
  else if ( family.equalsIgnoreCase( FAMILY_ZOS ) ){
    isFamily = OS_NAME.indexOf( FAMILY_ZOS ) > -1 || OS_NAME.indexOf( "os/390" ) > -1;
  }
  else if ( family.equalsIgnoreCase( FAMILY_OS400 ) ){
    isFamily = OS_NAME.indexOf( FAMILY_OS400 ) > -1;
  }
  else if ( family.equalsIgnoreCase( FAMILY_OPENVMS ) ){
    isFamily = OS_NAME.indexOf( FAMILY_OPENVMS ) > -1;
  }
  else{
    isFamily = OS_NAME.indexOf( family.toLowerCase( Locale.US ) ) > -1;
  }
}
if ( name != null ){
  isName = name.toLowerCase( Locale.US ).equals( OS_NAME );
}
if ( arch != null ){
  isArch = arch.toLowerCase( Locale.US ).equals( OS_ARCH );
}
if ( version != null ){
  isVersion = version.toLowerCase( Locale.US ).equals( OS_VERSION );
}
retValue = isFamily && isName && isArch && isVersion;

No comments:

Post a Comment