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:
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:
This has several advantages: aside from organizing your code logically, you can use
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
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 ...
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"
endpoint="myConsumer"
role="consumer"
locationURI="http://0.0.0.0:8192/Service/"
soap="true"
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:
root
|- 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:
<properties>
<deployment-port>8192</deployment-port>
<deployment-soap-version>1.2</deployment-soap-version>
</properties>
Then, we need to enable maven resource filtering in our root pom:
<build>
<resources>
<resource>
<directory>src/main/resources
<filtering>true
</resource>
</resources>
</build>
And of course, we need to use them. So instead of the previous
xbean.xml
, we can now use:
<http:endpoint service="test:MyConsumerService"
endpoint="myConsumer"
role="consumer"
locationURI="http://0.0.0.0:${deployment-port}/Service/"
soap="true"
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 ...
Comments
seems that we think alike cause its literally the same :)
Eduardo Burgos
eburgos@gmail.com