Maven: The Complete Reference - 5.3. Profile Activation |
||||||||||||||
|
||||||||||||||
In the previous section we showed you how to create a profile that
overrides default behavior for a specific target environment. In the
previous build the default build was designed for development and the
Take the following example, assume that we have a Java library that has a specific feature only available in the Java 6 release: the Scripting Engine as defined in JSR-223. You’ve separated the portion of the library that deals with the scripting library into a separate Maven project, and you want people running Java 5 to be able to build the project without attempting to build the Java 6 specific library extension. You can do this by using a Maven profile that adds the script extension module to the build only when the build is running within a Java 6 JDK. First, let’s take a look at our project’s directory layout and how we want developers to build the system. When someone runs Dynamic Inclusion of Submodules Using Profile Activation. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.sonatype.mavenbook</groupId> <artifactId>simple</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>simple</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <profiles> <profile> <id>jdk16</id> <activation> (1) <jdk>1.6</jdk> </activation> <modules> (2) <module>simple-script</module> </modules> </profile> </profiles> </project>
If you run
Activations can contain one of more selectors including JDK versions,
Operating System parameters, files, and properties. A profile is
activated when all activation criteria has been satisfied. For
example, a profile could list an Operating System family of Windows,
and a JDK version of 1.4, this profile will only be activated when the
build is executed on a Windows machine running Java 1.4. If the
profile is active then all elements override the corresponding
project-level elements as if the profile were included with the Profile Activation Parameters: JDK Version, OS Parameters, and Properties. <project> ... <profiles> <profile> <id>dev</id> <activation> <activeByDefault>false</activeByDefault> (1) <jdk>1.5</jdk> (2) <os> <name>Windows XP</name> (3) <family>Windows</family> <arch>x86</arch> <version>5.1.2600</version> </os> <property> <name>customProperty</name> (4) <value>BLUE</value> </property> <file> <exists>file2.properties</exists> (5) <missing>file1.properties</missing> </file> </activation> ... </profile> </profiles> </project>
This previous example defines a very narrow set of activation parameters. Let’s examine each activation criterion in detail:
You can activate a profile based on the value of a property like
Activating Profiles in the Absence of a Property. <project> ... <profiles> <profile> <id>development</id> <activation> <property> <name>!environment.type</name> </property> </activation> </profile> </profiles> </project>
Note the exclamation point prefixing the property name. The exclamation point is often referred to as the "bang" character and signifies "not". This profile is activated when no ${environment.type} property is set. |