Friday, October 16, 2009

Extremely useful maven placeholder based on profile

Someone just showed me this example which solves one of the annoying parts of multiple devs working with multiple resource configurations (databases, mailservers, etc) for different environments (dev,test,prod,etc)

(in project pom)
<properties>
...
<project.build.sourceEncoding>ISO-8859-1</project.build.sourceEncoding>
<log4j.priority>DEBUG</log4j.priority>
<project.build.listener-resources-context>staging-listener-resources.xml</project.build.listener-resources-context>
</properties>
<profiles>
<profile>
<id>production</id>
<properties>
<log4j.priority>INFO</log4j.priority>
<project.build.listener-resources-context>prod-listener-resources.xml</project.build.listener-resources-context>
<maven.test.skip>true</maven.test.skip>
</properties>
</profile>
</profiles>
...

Then this goes in the build section:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>

Then in your spring context, you can import just like normal, but using a placeholder now:
<import resource="${project.build.listener-resources-context}"/>

So now a normal build uses the staging resources, and something like
mvn install -Pproduction
now uses the production resources.

No comments: