Maven build profiles

Creating a sample application

  1. From command prompt just execute
    mvn archetype:generate
    -DgroupId=com.companyname.bank 
    -DartifactId=consumerBanking 
    -DarchetypeArtifactId=maven-archetype-quickstart 
    -DinteractiveMode=false

    it will create project under the current directory with folder name consumerbanking

  2. Next open the file pom.xml under consumerbanking folder and modify it as suggested
    <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.properties" tofile
    		    ="${project.build.outputDirectory}/env.properties"/>
                      </tasks>
                      </configuration>
                   </execution>
                </executions>
             </plugin>
          </plugins>
          </build>
          </profile>
       </profiles>
       <dependencies>
          <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version>3.8.1</version>
             <scope>test</scope>
          </dependency>
       </dependencies>
    </project>

     

  3. Next create three properties files under the folder: \consumerBanking\src\main\resources
    env.properties

    environment=debug

    env.test.properties

    environment=test

    env.prod.properties

    environment=prod
  4. Once all this changes are completed execute the command
    consumerbanking> mvn test –Ptest
    output:

    Results :
    
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    
    [INFO]
    [INFO] --- maven-antrun-plugin:1.1:run (default) @ project ---
    [INFO] Executing tasks
         [echo] Using env.test.properties
    [INFO] Executed tasks
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 4.953 s
    [INFO] Finished at: 2015-09-27T11:54:45+05:30
    [INFO] Final Memory: 9M/247M
    [INFO] ------------------------------------------------------------------------
  5. Profile activation via (maven settings/environment variable/OS/Missing files
  6. 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="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 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”.

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

    Let’s open command console, go to the folder containing pom.xml and execute the following mvn command.

    C:\MVN\project>mvn test

    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.

    <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 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.

    C:\MVN\project>mvn test

    Profile Activation via Present/Missing File

    Now activation element to include os detail as shown below. The test profile will trigger 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 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.

Leave a Reply