Pages

Wednesday, February 16, 2011

Maven Profiles Example

This is an example on a simple use-case for Maven profiles. It will be useful for someone who wants to implement one in their build or for someone who wants to know what is the use of having profiles.
Let us imagine that you need to have two builds. The difference in the two builds is the version of a single jar. For example, let us say that you need to have a build named profile-A with the version 1.0.0 of a jar and profile-B with the version 2.0.0 of a jar. The following is the pom.xml which shows how to do this.

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>profiles-example</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>profiles-example</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>profile-A</id>
          <dependencies>
              <dependency>
                <groupId>com.example</groupId>
                <artifactId>dependency</artifactId>
                <version>1.0.0</version>
              </dependency>
          </dependencies>
      </profile>
      <profile>
          <id>profile-B</id>
          <dependencies>
              <dependency>
                <groupId>com.example</groupId>
                <artifactId>dependency</artifactId>
                <version>2.0.0</version>
              </dependency>
          </dependencies>
      </profile>
  </profiles>
</project>

Now you can invoke profile-A using the command
mvn -Pprofile-A install
which will build the project with the dependency version 1.0.0.

Why would someone want to do this? This is a good example where  profiles can be used. To learn more about profiles, take a look at introduction to profiles in the maven documentation. You can specify other build specific parameters such as "plugins"," dependency management" etc. via profiles.

No comments:

Post a Comment