Maven profiles

We have touched the concept of Maven profiles in a couple of previous chapters, but never went into the details. When do we need Maven profiles and why is it a best practice?

Think about a large-scale multi-module Maven project. One of the best examples I am aware of is the WSO2 Carbon project. If you look at the application POM file available at http://svn.wso2.org/repos/wso2/tags/carbon/3.2.3/components/pom.xml, you will notice that there are more than hundred modules. Also, if you go deeper into each module, you will further notice that there are more modules within them: http://svn.wso2.org/repos/wso2/tags/carbon/3.2.3/components/identity/pom.xml. As a developer of the WSO2 Carbon project, you do not need to build all these modules. In this specific example, different groups of the modules are later aggregated into build multiple products. However, a given product does not need to build all the modules defined in the parent POM file. If you are a developer in a product team, you only need to worry about building the set of modules related to your product; if not, it's an utter waste of productive time. Maven profiles help you to do this.

With Maven profiles, you can activate a subset of configurations defined in your application POM file, based on some criteria.

If we take the same example we took previously, you will find that multiple profiles are defined under the <profiles> element: http://svn.wso2.org/repos/wso2/tags/carbon/3.2.3/components/pom.xml. Each profile element defines the set of modules that is relevant to it and identified by a unique ID. Also for each module, you need to define a criterion to activate it, under the activation element. By setting the value of the activeByDefault element to true, we make sure that the corresponding profile will get activated when no other profile is picked. In this particular example, if we just execute mvn clean install, the profile with the default ID will get executed. Keep in mind that the magic here does not lie on the name of the profile ID, default, but on the value of the activeByDefault element, which is set to true for the default profile. The value of the id element can be of any name:

<profiles>
  <profile>
    <id>product-esb</id>
    <activation>
      <property>
        <name>product</name>
        <value>esb</value>
      </property>
    </activation>
    <modules></modules>
  </profile>
  <profile>
    <id>product-greg</id>
    <activation>
      <property>
        <name>product</name>
        <value>greg</value>
      </property>
    </activation>
    <modules></modules>
  </profile>
  <profile>
    <id>product-is</id>
    <activation>
      <property>
        <name>product</name>
        <value>is</value>
      </property>
    </activation
    <modules></modules>
  </profile>
  <profile>
    <id>default</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <modules></modules>
  </profile>
</profiles>

If I am a member of the WSO2 Identity Server (IS) team, then I will execute the build in the following manner:

$ mvn clean install –Dproduct=is

Here, we pass the system property product with the value is. If you look at the activation criteria for all the profiles, all are based on the system property: product. If the value of the system property is is, then Maven will pick the build profile corresponding to the Identity Server:

<activation>
  <property>
    <name>product</name>
    <value>is</value>
  </property>
</activation>

You also can define an activation criterion to execute a profile, in the absence of a property. For example, the following configuration shows how to activate a profile if the product property is missing:

<activation>
  <property>
    <name>!product</name>
  </property>
</activation>

The profile activation criteria can be based on a system property, the JDK version, or an operating system parameter where you run the build.

The following sample configuration shows how to activate a build profile for JDK 1.6:

<activation>
  <jdk>1.6</jdk>
</activation>

The following sample configuration shows how to activate a build profile based on operating system parameters:

<activation>
  <os>
    <name>mac os x</name>
    <family>mac</family>
    <arch>x86_64</arch>
   <version>10.8.5</version>
  </os>
</activation>

The following sample configuration shows how to activate a build profile based on the presence or absence of a file:

<activation>
  <file>
    <exists>……</exists>
    <missing>……</missing>
  </file>
</activation>

In addition to the activation configuration, you can also execute a Maven profile just by its ID, which is defined within the id element. In this case, you need a prefix; use the profile ID with –P, as shown in the following command:

$ mvn clean install -Pproduct-is
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.176.145