X
    Categories: Java Core

How to use Maven profiles

Simply said, Maven profile is a set of configuration value which override default values. By using it, it allows you to create a custom build for different environments (Production/Development).

Before we proceed with the tutorial’s content, it is assumed you have Maven installed. In case you don’t, follow this tutorial for a step-by-step guide.

To specify a profile in Maven, you need to use the activeProfiles or profiles elements in the pom.xml file. The pom.xml is being modified at run time.

There are 3 build profile types.

  1. Per Project
    1. defined in the pom.xml file
  2. Per User
    1. defined in the Maven settings xml file (%USER_HOME%/.m2/settings.xml)
  3. Global
    1. Defined in Maven global settings xml file (%M2_HOME%/conf/settings.xml)

How to prompt a Maven Build profile? There are a couple of ways:

  1. Terminal – Covered in this tutorial
  2. Maven settings – Covered in this tutorial
  3. Environment variables – Covered in thsi tutorial
  4. OS settings
  5. Present or missing files

Profile Activation explicitly

Create your Maven Project(If you haven’t already) and let’s create our first simple profile called test1.

This is my pom.xml code that I added

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>mavenprofilesdemo</groupId>
  <artifactId>mavenprofilesdemo</artifactId>
  <packaging>jar</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>Maven Quick Start Archetype</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
    <profiles> 
            <profile>
                <id>test1</id> 
                    <build> 
                            <plugins>
                                <plugin>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-compiler-plugin</artifactId>
                                    <configuration>
                                    	<fork>true</fork>
                                        <compilerVersion>1.5</compilerVersion>
                                    </configuration>
                                </plugin>
                            </plugins>
                    </build>
            </profile>
    </profiles>
</project>

profiles->profile->id ; That’s where we specify how we are going to reference the profile. It is very important you don’t miss that line as it is not only mandatory but if you omit it, you won’t be able to access your profile.

What we did in our pom.xml file is that we have overridden the compiler plugin settings. We have set the compiler’s version to 1.5 and set fork to true.



Keep in mind that in this case we have created only 1 profile, but we can also add more <profile> tags within the <profiles> tag.

After we have overridden the plugin we want, it is time to run our profile. You run it by typing in the command line mvn test -P<id>

In our case, we need to write mvn test -Ptest1 because our profile we created, we gave it an id of value “test1”.

Now go to your project’s folder location, and type mvn test -P<your profile’s id>. If I run this command on the above example, this is the result I am getting:

[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< mavenprofilesdemo:mavenprofilesdemo >-----------------
[INFO] Building Maven Quick Start Archetype 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mavenprofilesdemo ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Eclipse Projects\mavenprofilesdemo\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mavenprofilesdemo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mavenprofilesdemo ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Eclipse Projects\mavenprofilesdemo\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mavenprofilesdemo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mavenprofilesdemo ---
[INFO] Surefire report directory: D:\Eclipse Projects\mavenprofilesdemo\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running mavenprofilesdemo.mavenprofilesdemo.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.072 s
[INFO] Finished at: 2019-08-18T09:15:55+01:00
[INFO] ------------------------------------------------------------------------

Profile Activation using Maven settings

Navigate to your user home directory and then open the .m2 folder. If there isn’t settings.xml file there, create one.

Then add the profile we created as an active profile. Use the code below:

<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, navigate to your folder that contains the pom.xmlfile and execute mvn test.

Profile Activation using Environment variables

Remove the settings.xml file and add the value env in the name tag. Like so:

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

You have to create an environment variable that is called env and set its value as “test1”.

Navigate to the folder that contains pom.xml and type mvn test.

If you are interested in including a custom library into maven local repository, you can follow this article.

5 2 votes
Article Rating
ym_coding:
Related Post