Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Apache Maven builds profiles


May 26, 2021 Maven


Table of contents


Maven - Build a profile

What is a build profile?

A build profile is a collection of configurations that set or override the default configuration built by Maven. With build profiles, you can customize the build process for different environments, such as Production and Development environments.

Profile is specified .xml activeProfiles / profiles element in pom and can be triggered in many ways. Profile modifies the POM at build time and sets different target environments for variables (for example, database server paths in development, test, and product environments).

Profile type

Profile has three main types.

Type Where to define it
Per Project Defined in the project POM .xml pom
Per User Defined in Maven settings xml file (%USER_HOME%/.m2/settings.xml)
Global Defined in Maven global configuration xml file (%M2_HOME%/conf/sets.xml)

Profile is activated

Maven's Profile can be activated in several different ways.

  • Explicitly enter using the command console
  • Set up by maven
  • Based on environment variables (user / system variables)
  • Operating system configuration (for example, Windows family)
  • Existing / Missing Files

Profile activation example

Let's assume that your engineering catalog looks like this:

Apache Maven builds profiles

There are now three environment profiles in the src/main/resources directory:

The file name Describe
env.properties The default configuration when there is no profile
env.test.properties The test configuration when using the test profile
env.prod.properties The product configuration when the product profile is used

Explicit Profile activation

In the following example, we add the attach maven-antrun-plugin:run target to the test phase. T his allows us to output text information in different Profiles. We'll use .xml to define different Profiles and activate Profiles with maven commands in the command console.

Let's say that we created the following pom file in the C:/MVN.xml directory.

<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/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.companyname.projectgroup</groupId>
   <artifactId>project</artifactId>
   <version>1.0</version>
   <profiles>
      <profile>
      <id>test</id>
      <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.1</version>
            <executions>
               <execution>
                  <phase>test</phase>
                  <goals>
                     <goal>run</goal>
                  </goals>
                  <configuration>
                  <tasks>
                     <echo>Using env.test.properties</echo>
            <copy file="src/main/resources/env.test.propertiestofile
            ="${project.build.outputDirectory}/env.properties"/>
                  </tasks>
                  </configuration>
               </execution>
            </executions>
         </plugin>
      </plugins>
      </build>
      </profile>
   </profiles>
</project>

Now open the command console, jump to the directory .xml where the pom is located, and execute the following mvn command. Use the -P option to specify the name of the Profile.

C:\MVN\project>mvn test -Ptest

Maven will begin processing and displaying the results of test Profile.

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------
[INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0
[INFO]    task-segment: [test]
[INFO] ------------------------------------------------------------------
[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}]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\MVN\project\src\test\resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Nothing to compile - all classes are up to date
[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] ------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Sun Jul 08 14:55:41 IST 2012
[INFO] Final Memory: 8M/64M
[INFO] ------------------------------------------------------------------

Now let's practice, you can follow these steps:

  • Add another profile .xml profile element to the profile element of the pom element (copy the existing profile element and paste it to the end of the profiles element).
  • Modify the id of this profile element from test to normal.
  • Modify the task section to echo env.properties, and copy env.properties to the target directory
  • Repeat the above three steps again, modifying the id to prod and the task section to env.prod.properties
  • That's all. Now you have three build profiles (normal / test / prod).

Now open the command console, jump to the .xml directory where the pom is located, and execute the mvn command below. Use the -P option to specify the name of the Profile.

C:\MVN\project>mvn test -Pnormal
C:\MVN\project>mvn test -Pprod

Check the output of the build to see what's different.

Activate Profile with Maven settings

Open Maven's settings .xml file, which can be found in the %USER_HOME%/.m2 directory, and %USER_HOME% represents the user's home directory. If the settings .xml file does not exist, you need to create one.

As shown in the following example, use the activeProfiles node to add a test configuration as an active Profile.

<settings 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/xsd/settings-1.0.0.xsd">
   <mirrors>
      <mirror>
         <id>maven.dev.snaponglobal.com</id>
         <name>Internal Artifactory Maven repository</name>
         <url>http://repo1.maven.org/maven2/</url>
         <mirrorOf>*</mirrorOf>
      </mirror>
   </mirrors>
   <activeProfiles>
      <activeProfile>test</activeProfile>
   </activeProfiles>
</settings>

Now open the command console, jump to the .xml where the pom is located, and execute the mvn command below. D o not use the -P option to specify the name of profile. Maven displays the results of the activated test Profile.

C:\MVN\project>mvn test

Activate Profile with environment variables

Now remove the activated Profile .xml from maven's settings and update the test Profile .xml the pom file. Add the following to the activation element of the profile element.

When the system property "env" is set to "test," the test configuration is triggered. Create an environment variable "env" and set its value to "test".

<profile>
   <id>test</id>
   <activation>
      <property>
         <name>env</name>
         <value>test</value>
      </property>
   </activation>
</profile>

Now open the command console, jump to the .xml where the pom is located, and execute the mvn command below.

C:\MVN\project>mvn test

Activate Profile via the operating system

The activation element contains the following operating system information. Test Profile is triggered when the system is windows XP.

<profile>
   <id>test</id>
   <activation>
      <os>
         <name>Windows XP</name>
         <family>Windows</family>
         <arch>x86</arch>
         <version>5.1.2600</version>
      </os>
   </activation>
</profile>

Now open the command console, jump to the .xml where the pom is located, and execute the mvn command below. D o not use the -P option to specify the name of profile. Maven displays the results of the activated test Profile.

C:\MVN\project>mvn test

Activate Profile with existing /missing files

Now use the activation element to contain the following operating system information. Test Profile is triggered when target/generated-sources/axistools/wsdl2java/com/companyname/group is missing.

<profile>
   <id>test</id>
   <activation>
      <file>
         <missing>target/generated-sources/axistools/wsdl2java/
         com/companyname/group</missing>
      </file>
   </activation>
</profile>

Now open the command console, jump to the .xml where the pom is located, and execute the mvn command below. D o not use the -P option to specify the name of profile. Maven displays the results of the activated test Profile.

C:\MVN\project>mvn test