Wednesday, August 29, 2007

Maven Profiles Activation by Property and the Maven Help Plugin

In maven, you can have profiles both in settings.xml and in the pom.xml (and other places, which I won't mention here). Profiles in settings.xml can be used to define properties that are local to your development environment on your local box. Profiles in the pom.xml of the project can define properties, that are specific to certain target environments, like staging or production.

Example: Filtering resources with properties from a profile
In this example, I have a spring applicationContext.xml, that needs to be copied from src/main/resources to target/classes, and in the process have a variable ${webServiceUrl} replaced (filtered) with the value of a property that fits the environment. What comes into the placeholder depends upon which deployment environment your build is targeting: Your local box, staging or production.

Here is the small extract of the applicationContext.xml, that will have something replaced:

<bean id="fooService" class="com.acme.foo.service.FooServiceImpl">
<property name="webserviceEndpointUrl" value="${webserviceEndpointUrl}"/>
</bean>

And in the pom.xml, we define this, to enable filtering:

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

Then, in settings.xml, I define this:

<profiles>
<profile>
<id>foo-development</id>
<activation>
<property>
<name>!environment</name>
</property>
</activation>
<properties>
<webserviceEndpointUrl>http://localhost:8080/services/myservice</webserviceEndpointUrl>
</properties>
</profile>
</profiles>

It defines the property and sets it to a URL, that is correct in my local development environment. Note the use of the activation element, that uses a little trick with the !-sign. It activates the profile, if the property called environment is not set.

Okay, and lastly, I list how the production and staging profiles are setup in pom.xml:

<profiles>
<profile>
<id>production</id>
<activation>
<property>
<name>environment</name>
<value>production</value>
</property>
</activation>
<properties>
<webserviceEndpointUrl>http://www.acme.com/services/myservice</webserviceEndpointUrl>
</properties>
</profile>
<profile>
<id>staging</id>
<activation>
<property>
<name>environment</name>
<value>staging</value>
</property>
</activation>
<properties>
<webserviceEndpointUrl>http://www.acme-test.com/services/myservice</webserviceEndpointUrl>
</properties>
</profile>
</profiles>

These profiles activate if the environment property is set. Which profile is determined by the value of the property. So, how to build with this? Easy!
  • If you simply type "mvn", the environment property is not set, and the profile with the localhost url from settings.xml will be activated.
  • If you type "mvn -Denvironment=production", the profile in settings.xml will not activate (as the environment property is set), but the production profile in pom.xml will.
You can read more about profiles in mavens official introduction to profiles or in the great online maven book by Sonatype in chapter 8 on profiles.

Maven Help Plugin
In my process of setting this up, I stumpled upon a cool maven plugin. The maven help plugin.
Among other things, it can list the actual, active profiles for a build with "mvn help:active-profiles". Even more cool is typing "mvn help:effective-pom" in the project, and get a dump of a pom, which is the actual pom, that maven uses when building (all mixed up with profiles, properties and settings taken from config around in the build setup).

2 comments:

Kocka said...

wow, this looks like a cool feature :)

Daniel Cruz said...

I like this blog is fantastic, is really good written. Congratulation. Do you want to see something more? Read it...: Costa Rica is a country with a extremely sense of freedom. The landscapes are for much the most green in whole center america.The chances of investement are way to high, the average of Americans, European and people of the entire planet who is buying here is up in the sky !!!
Great investment opportunity in Costa Rica: condos, costa rica real estate, costa rica property. Visit us for more info at: http://www.jaco-bay.com/