Activate Maven Profile by Java Version
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>
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>
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 ProfilesVersion Range Specification