Profile Activation File
MNG-5360 profile/activation/file does not work with project.basedir, only with basedir Closed.
Profile Activation. A Maven Build Profile can be activated in various ways. Explicitly using command console input. Through maven settings. Based on environment.
Maven – Introduction to build profiles
What is Build Profile.
A Build profile is a set of configuration values which can be used to set or override default values of Maven build. Using a build profile, you can customize build for different environments such as Production v/s Development environments.
Profiles are specified in pom.xml file using its activeProfiles / profiles elements and are triggered in variety of ways. Profiles modify the POM at build time, and are used to give parameters different target environments for example, the path of the database server in the development, testing, and production environments.
Types of Build Profile
Build profiles are majorly of three types
TypeWhere it is definedPer ProjectDefined in the project POM file, pom.xmlPer UserDefined in Maven settings xml file USER_HOME /.m2/settings.xml GlobalDefined in Maven global settings xml file M2_HOME /conf/settings.xml Profile Activation
A Maven Build Profile can be activated in various ways.
Explicitly using command console input.
Through maven settings.
Based on environment variables User/System variables.
OS Settings for example, Windows family.
Present/missing files.
Profile Activation Examples
Let us assume following directory structure of your project:
Now, under src/main/resources there are three environment specific files:
File NameDescriptionenv.propertiesdefault configuration used if no profile is mentioned.env.test.propertiestest configuration when test profile is used.env.prod.propertiesproduction configuration when prod profile is used.Explicit Profile Activation
In the following example, We ll attach maven-antrun-plugin:run goal to test phase. This will allow us to echo text messages for different profiles. We will be using pom.xml to define different profiles and will activate profile at command console using maven command.
Assume, we ve created following pom.xml in C: MVN project folder.
project xmlns
xmlns:xsi
xsi:schemaLocation
4.0.0
com.companyname.projectgroup
project
1.0
test
org.apache.maven.plugins
maven-antrun-plugin
1.1
run
Using env.test.properties
Now open command console, go to the folder containing pom.xml and execute the following mvn command. Pass the profile name as argument using -P option.
C: MVN project mvn test -Ptest
Maven will start processing and display the result of test build profile.
INFO Scanning for projects
INFO ------------------------------------------------------------------
INFO Building Unnamed - com.companyname.projectgroup:project:jar:1.0
INFO task-segment: test
INFO resources:resources execution: default-resources
WARNING Using platform encoding Cp1252 actually to copy filtered resources,
i.e. build is platform dependent.
INFO Copying 3 resources
INFO compiler:compile execution: default-compile
INFO Nothing to compile - all classes are up to date
INFO resources:testResources execution: default-testResources
INFO skip non existing resourceDirectory C: MVN project src test resources
INFO compiler:testCompile execution: default-testCompile
INFO surefire:test execution: default-test
INFO Surefire report directory: C: MVN project target surefire-reports
-------------------------------------------------------
T E S T S
There are no tests to run.
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
INFO antrun:run execution: default
INFO Executing tasks
echo Using env.test.properties
INFO Executed tasks
INFO BUILD SUCCESSFUL
INFO Total time: 1 second
INFO Finished at: Sun Jul 08 :41 IST 2012
INFO Final Memory: 8M/64M
Now as an exercise, you can do the following steps
Add another profile element to profiles element of pom.xml copy existing profile element and paste it where profile elements ends.
Update id of this profile element from test to normal.
Update task section to echo env.properties and copy env.properties to target directory
Again repeat above three steps, update id to prod and task section for env.prod.properties
That s all. Now you ve three build profiles ready normal / test / prod.
Now open command console, go to the folder containing pom.xml and execute the following mvn commands. Pass the profile names as argument using -P option.
C: MVN project mvn test -Pnormal
C: MVN project mvn test -Pprod
Check the output of build to see the difference.
Profile Activation via Maven Settings
Open Maven settings.xml file available in USER_HOME /.m2 directory where USER_HOME represents user home directory. If settings.xml file is not there then create a new one.
Add test profile as an active profile using activeProfiles node as shown below in example
settings xmlns
maven.dev.snaponglobal.com
Internal Artifactory Maven repository
Now open command console, go to the folder containing pom.xml and execute the following mvn command. Do not pass the profile name using -P option.Maven will display result of test profile being an active profile.
C: MVN project mvn test
Profile Activation via Environment Variables
Now remove active profile from maven settings.xml and update the test profile mentioned in pom.xml. Add activation element to profile element as shown below.
The test profile will trigger when the system property env is specified with the value test. Create a environment variable env and set its value as test.
env
Let s open command console, go to the folder containing pom.xml and execute the following mvn command.
Profile Activation via Operating System
Activation element to include os details as shown below. This test profile will trigger when the system is windows XP.
Windows XP
Windows
x86
5.1.2600
Now open command console, go to the folder containing pom.xml and execute the following mvn commands. Do not pass the profile name using -P option.Maven will display result of test profile being an active profile.
Profile Activation via Present/Missing File
Now activation element to include os details as shown below. The test profile will trigger when target/generated-sources/axistools/wsdl2java/com/companyname/group is missing.
target/generated-sources/axistools/wsdl2java/com/companyname/group
C: MVN project mvn test.
Root element of the profiles.xml file. Element Type Description; profiles/profile The ID of this build profile, for activation purposes. activation.
Apply the host profile or update the answer file to be prompted for input. If no activation profile exists, right-click Activation Profile and select New Profile. 5.
File profile activation. In a parent profile, I have a profile that is supposed to activate based on the presence of a file in the tree of the child that uses the parent.
When working with Maven profiles, sometimes you need to activate a profile when multiple conditions are true. Although the activation element in pom.xml lets you.
Jul 08, 2015 If you do not already have a separate file share for roaming user profiles this roaming profile path box, enter the path to the file share where.
Introduction to Build Profiles. Apache Maven 2.0 goes to great lengths to ensure that builds are portable. Among other things, this means allowing build configuration.
Explicitly
Through Maven settings
Based on environment variables
OS settings
Present or missing files
Profiles can be explicitly specified using the -P CLI option.
This option takes an argument that is a comma-delimited list of profile-ids to use. When this option is specified, no profiles other than those specified in the option argument will be activated.
mvn groupId:artifactId:goal -P profile-1,profile-2
Profiles can be activated in the Maven settings, via the section. This section takes a list of elements, each containing a profile-id inside.
profile-1
Profiles listed in the tag would be activated by default every time a project use it.
Profiles can be automatically triggered based on the detected state of the build environment. These triggers are specified via an section in the profile itself. Currently, this detection is limited to prefix-matching of the JDK version, the presence of a system property or the value of a system property. Here are some examples.
The following configuration will trigger the profile when the JDK s version starts with 1.4 eg. 1.4.0_08, 1.4.2_07, 1.4 :
1.4
Ranges can also be used as of Maven 2.1 refer to the Enforcer Version Range Syntax for more information. The following honours versions 1.3, 1.4 and 1.5.
1.3,1.6
Note: an upper bound such as, 1.5 is likely not to include most releases of 1.5, since they will have an additional patch release such as _05 that is not taken into consideration in the above range.
This next one will activate based on OS settings. See the Maven Enforcer Plugin for more details about OS values.
Windows XP
Windows
x86
5.1.2600
The profile below will activate the profile when the system property debug is specified with any value:
debug
The next example will trigger the profile when the system property environment is specified with the value test :
environment
test
To activate this you would type this on the command line:
mvn groupId:artifactId:goal -Denvironment test
As of Maven 3.0, profiles in the POM can also be activated based on properties from active profiles from the settings.xml.
Note: Environment variables like FOO are available as properties of the form env.FOO. Further note that environment variable names are normalized to all upper-case on Windows.
This example will trigger the profile when the generated file target/generated-sources/axistools/wsdl2java/org/apache/maven is missing.
target/generated-sources/axistools/wsdl2java/org/apache/maven
As of Maven 2.0.9, the tags and could be interpolated. Supported variables are system properties like user.home and environment variables like env.HOME. Please note that properties and values defined in the POM itself are not available for interpolation here, e.g. the above example activator cannot use project.build.directory but needs to hard-code the path target.
Profiles can also be active by default using a configuration like the following:
true
This profile will automatically be active for all builds unless another profile in the same POM is activated using one of the previously described methods. All profiles that are active by default are automatically deactivated when a profile in the POM is activated on the command line or through its activation config.
Deactivating a profile
Starting with Maven 2.0.10, one or more profiles can be deactivated using the command line by prefixing their identifier with either the character . or - as shown below:
mvn groupId:artifactId:goal -P. profile-1.profile-2
This can be used to deactivate profiles marked as activeByDefault or profiles that would otherwise be activated through their activation config.
The activation element lists the conditions for profile activation. In this example, we ve specified that this profile will be activated by Java versions that begin.