Signup/Sign In

Build Profiles in Maven

Profile in maven is nothing but subset of elements which allows to customize builds for particular environment. Profiles are also portable for different build environments.

Build environment basically means a specific environment set for production and development instances. When developers work on development phase, they are intend to use database from the production instance and for the production phase, the live database will be used.

So, in order to configure these instances maven provides the feature of build profiles. Any no. of build profiles can be configured and also can override any other settings in the pom.xml

These defined profiles have the ability to modify the pom.xml during the build time. I.e. to configure separate environments for development and production instances. Based on the parameters passed, the corresponding profiles are activated accordingly. E.g. profiles can be set for dev, test and production phases.


Maven: Types of build profiles

The below table shows the types of build profiles in Maven :

Build Profile TypeDefined in
Per projectpom.xml
Per User/DeveloperMaven settings.xml (%USER_HOME%/.m2/settings.xml)
GlobalMaven global settings.xml (%M2_HOME%/conf/settings.xml)

Maven: Build Portability

As mentioned above, different environments can be set up based on the requirements for a given project. So, with this the portability of a given project can be secured and handled effectively.

Build portability can be defined as the ability of a project which can be compiled and deployed successfully across different set of environments which also involves the applying different environmental configurations for the same. Any portable project should always tend to work without any customization of any properties.

And Any portable project will always eliminates the complexities and issues associated to contributing to a project.


Maven: Activating profiles

Below are ways in which build profiles of maven can be activated or triggered :

  • Explicitly using commands
  • Maven settings
  • Based on environment variables
  • Operating system settings
  • Present/missing files

Maven: Explicitly using commands

<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 app.test.properties</echo>
                        <copy file="src/main/resources/app.test.properties" 
                        	tofile="${project.build.outputDirectory}/env.properties"/>
                     </tasks>
                     </configuration>
                  </execution>
               </executions>
            </plugin>
         </plugins>
      </build>
   </profile>
</profiles>

Maven Settings Example

Following is a basic Maven settings example:

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

Based on Environment Variables

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

Operating System Settings

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

Present/missing files

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