Wednesday, March 07, 2007

Using maven to switch runtime properties

For those who don't know all of Maven's features, I will show how to leverage maven at built-time to easily share run-time properties between service units in ServiceMix.

Let's say that you expose a few services over HTTP/SOAP using servicemix-http. If you are building several service assemblies, you will end up having several files containing:

<http:endpoint service="test:MyConsumerService"
soapVersion="1.1" />

You will certainly want all your services to be exposed on the same port, so it can become a bit tedious if you need to change them all, or if you need to change between different work environments (test, production, etc...).

When working with several service assemblies at the same time, I would recommend to use a hierachical organization for your maven projects:

|- module1
| |- module1-http-su
| |- module1-jsr181-su
| \- module1-sa
|- module2
| |- module2-http-su
| |- module2-jsr181-su
| \- module2-sa

This has several advantages: aside from organizing your code logically, you can use mvn jbi:projectDeploy at the root level, or at a sub-level.

So, now, how can we use maven for our properties ? Well, we just have to create them in the root pom:


Then, we need to enable maven resource filtering in our root pom:


And of course, we need to use them. So instead of the previous xbean.xml, we can now use:

<http:endpoint service="test:MyConsumerService"
soapVersion="${deployment-soap-version}" />

So now, if you want to switch your port, just change the root pom property and rebuild :-)

The next step is to use maven profiles to define environment and easily switch between those. This is maybe not useful for changing the HTTP port use, but if you configure some database access, you can easily switch between your production database and an embedded one for testing ...


Anonymous said...

I use exactly the same idea Guillaume,
seems that we think alike cause its literally the same :)

Anonymous said...

oops, forgot my name on the prev comment, lol

Eduardo Burgos

twit88 said...

thanks for the info. It is useful.