Maven - Maven Build Profiles - maven tutorial
maven tutorial tags : apache maven , maven repository , maven central
How to Build Profile in Maven ?
- 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:
Type | Where it is defined |
---|---|
Per Project | Defined in the project POM file, pom.xml |
Per User | Defined in Maven settings xml file (%USER_HOME%/.m2/settings.xml) |
Global | Defined 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 Name | Description |
---|---|
env.properties | default configuration used if no profile is mentioned. |
env.test.properties | test configuration when test profile is used. |
env.prod.properties | production configuration when prod profile is used. |
Explicit Profile Activation:
- 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.
Learn Maven Tutorial - Maven Build - Maven Example
maven tutorial tags : apache maven , maven repository , maven central
Sample Code
- And assume, we've created following properties file in C:\MVN\project\src\resources folder.
env.properties
env.test.properties
env.prod.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.
- Maven will start processing and show the output of test build profile.
maven tutorial tags : apache maven , maven repository , maven central
Output
- 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.
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
maven tutorial tags : apache maven , maven repository , maven central
Sample Code
- 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.
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".
- 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 detail as shown below. This test profile will trigger when the system is windows XP.
- 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.
maven tutorial tags : apache maven , maven repository , maven central
Profile Activation via Present/Missing File
- Now activation element to include os.
- 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.