Wednesday 10 July 2019

Maven: Build Profiles


Profiles are used to customize the build process. For example, in any typical application development, there are three kinds of environments.
a.   Development Environment: All the development activities go here.
b.   Testing Environment: All the testing activities goes here
c.    Production Environment: Customers use this environment.

The configurations like database, connection pool, number of vms are different for each environment. For example, in production you should work with real time customer data and may require more vms. But in case of development environment, you may work with some dummy data and one virtual machine.

By using maven build profiles, you can configure these environments, and build the artifacts by targeting specific environment.

Where can I configure profiles?
Profiles are configured in pom.xml file. Each profile has an unique identifier associated with it, it is used to distinguish it from other profiles.

How to define a profile?
<profile> element is used to define a profile. All the profile elements are combined under <profiles> element.

For example, below snippet configures a production profile.
<profiles>
   <profile>
      <id>production</id>
      <build />
   </profile>
</profiles>

How to execute a profile?
A profile is executed by passing the command line argument ‘-P <PROFILE_ID>’ to maven.

Example
mvn clean install -Pproduction -X
‘production’ is the profile id. -X option is used to enable the debug output.

What elements a profile contain?
A profile element contains many elements that appear in project element. For example, you can add plugins, dependencies, properties to a profile.

By specifying the elements in <profile>, you can override the default specified in <project> element.


Example

<?xml version="1.0" encoding="UTF-8"?>
<profile>
  <id>production</id>
  
  <dependencies>
    <!-- Add all the dependencies here-->
  </dependencies>
  
  <reporting>...</reporting>
  
  <modules>...</modules>
  
  <dependencyManagement>...</dependencyManagement>
  
  <distributionManagement>...</distributionManagement>
  
  <repositories>...</repositories>
  
  <pluginRepositories>...</pluginRepositories>
  
  <build>
  
    <plugins>
      <!-- Add all the plugins here -->
    </plugins>
 
    <defaultGoal>...</defaultGoal>
 
    <finalName>...</finalName>
 
    <resources>...</resources>
 
    <testResources>...</testResources>
 
  </build>
  
  <activation>
    <activeByDefault>false</activeByDefault>
  </activation>
  
  <properties>
    <!-- Add all the properties here -->
  </properties>
  
</profile>


Previous                                                    Next                                                    Home

No comments:

Post a Comment