Activate Maven Profile by Java Version

By Arvind Rai, June 04, 2021
Maven profiles can be automatically triggered by using <activation> element within <profile> parent element. Profiles are triggered based on detected state of the build environment such as JDK version, the presence of a system property or the value of a system property.
In case of Maven profile activation by Java version, profiles will trigger when the specified JDK version by <jdk> element within parent <activation> element, matches our application JDK version.
<activation>
  <jdk>1.4</jdk>
</activation> 

Specify JDK Version

Suppose we want to activate a Maven profile when our application is using JDK 1.4 version. We just need to specify Java version using <jdk> element within parent <activation> element as following.
pom.xml
<project>
 ------  
 <profiles>
  <profile>
	<id>jdk4</id>
	<activation>
	  <jdk>1.4</jdk>
	</activation>
	<build>
	  <resources>
		<resource>
		  <directory>src/main/resources/old-files</directory>
		</resource>
	  </resources>
	</build>
  </profile>
  ------
 </profiles>
</project> 
When the Java version used by application will be JDK 1.4, our profile with profile id 'jdk4' will be activated. If JDK version is "1.4.0_08", Maven matches only prefix "1.4".
Java version history are 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 etc. So if we want to activate profile for Java 12, write is as following.
<activation>
  <jdk>12</jdk>
</activation> 

Specify JDK Version Range

We can specify a JDK version range to activate profile. Maven will match the prefix of Java version of our application with the specified Java version range.
<profile>
  <id>jdk-versions</id>
  <activation>
    <jdk>(1.4, 8]</jdk>
  </activation>
  ------
</profile> 
The profile id 'jdk-versions' will be activated when our application Java version is either JDK 1.5 or JDK 1.6 or JDK 7 or JDK 8 .
We can specify upper bound and lower bound for Java version range as following.
1. [1.4, 8] : JDK 1.4, JDK 1.5, JDK 1.6, JDK 7, JDK 8
2. (1.4, 8) : JDK 1.5, JDK 1.6, JDK 7
3. [1.4, 8) : JDK 1.4, JDK 1.5, JDK 1.6, JDK 7
4. (1.4, 8] : JDK 1.5, JDK 1.6, JDK 7, JDK 8

References

Introduction to Build Profiles
Version Range Specification
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us