A coders, hackers heaven.....Hm...I do not think so...
Maven: Configuration for Multipe Environments
I have often heard that it is not possible to create a number of artifacts out of a single Maven module with different configurations.
An example from the wild is to have a web-app (or whatever app) which should be running in development, test, q&a
and production environment. So you need different configurations for the different environments for example the
database connection or whatever. At first glance that seems to be a contradiction cause Maven’s paradigm is “One Module One Artifact”?
Now let us start with a webapp project (packaging war) where the pom.xml looks more or less lilke the following:
If you call Maven on command line via mvn clean package you will get a war-file within the target folder
which contains your webapp incl. web.xml and your compiled classes etc. The contents of the war file
will be created based on the compiled classes coming from src/main/java, the
resources src/main/resources plus src/main/webapp folder with the supplemental
dependencies which you have defined inside your pom file.
Ok. Now let us come to the point to have some kind of configuration for our web application. We put the
configuration into the appropriate folders like test, q&a and production which
results into the following structure of the project:
So we have to create a war file for test, q&a and production. The simplest way to achieve this is by using the
Maven Assembly Plugin
You need to create an assembly descriptor for every environment. Let us start with the test environment
which looks like the following and should be named test.xml and put into src/main/assembly folder:
Now the result is that by using a simple mvn package you get in the target folder three
different war files, named like artifactId-version-test.war, artifactId-version-qa.war and
artifactId-version-production.war which are packaged for the different environments.