Java – way to specify data-sources.xml file to be included in EAR using maven

javamaven-2

First some background information:

We have three environments for our EJB3 application: test, development and production. All require database settings of their own. We use maven2.

Data source settings are placed in ear project, directory /src/main/application/META-INF/data-sources.xml. File contains attributes for three different connection factories (one for each environment). However, only one of those can be active when the application is compiled, and the other two must be commented out.

It's a very common mistake to forget to modify the data-sources.xml before compiling the application to another environment, and this naturally causes interesting issues – and the burden of recompiling and -deploying.

It would be great if we could have three different data-sources.xml files (one for each environment), and depending on the command line switches one could specify which file was included in the ear. Something like this:

mvn install dev
mvn install prod
mvn install test

Is there a way to accomplish this?

Best Answer

Maven has the concept of profiles for this kind of things: Building For Different Environments with Maven 2