I have a Java webapp project that I develop in Eclipse (more precisely MyEclipse 10) and build using Maven 3.
I have the following layout (including only the files relevant to my problem :
project root
|-- src
| |-- main
| | |-- java
| | |-- resources
| | | |-- log4j.xml
| | | +-- struts.properties
| | |-- webapp
| | | |-- META-INF
| | | | +--context.xml
| | |-- config
| | | |-- test
| | | | |--log4j.xml
| | | | |--struts.properties
| | | | +--context.xml
| | | +-- prod
| | | | |--log4j.xml
| | | | |--struts.properties
| | | | +--context.xml
| +--test
+--pom.xml
As you can see, I included a number of configuration files. The one who are at their proper location within the project struture, i.e. inside src/main/resources
and src/main/webapp
are the ones that I routinely use when I work in MyEclipse. I can use MyEclipse connectors to automatically update a deployment to e.g. a Tomcat instance on my dev machine. I just click "run server" and I can debug. No need to use Maven at all in this context actually.
Then, when I want to build a release for another environment such as testing or production, I run mvn -P test clean install
and it builds a nice WAR.
My goal is to replace the configuration files inside the final WAR by those in src/config/{environment}/
.
I have set profiles in my pom.xml
:
<profiles>
<profile>
<id>test</id>
<properties>
<environment>test</environment>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<environment>prod</environment>
</properties>
</profile>
</profiles>
Then, I try to copy these resources from the specified profile (using the environment
variable) to the correct location inside the WAR (or the temporary folder that will be zipped into a WAR) :
<webResources>
<resource>
<directory>/src/main/config/${environment}</directory>
<targetPath>META-INF/</targetPath>
<includes>
<include>context.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/config/${environment}</directory>
<targetPath>WEB-INF/classes/</targetPath>
<includes>
<include>
struts.properties
</include>
<include>
log4j.xml
</include>
</includes>
</resource>
</webResources>
Now this seems to work, except that the "standard" resources are copied to the directory AFTER this, so they overwrite these files. So I always end up with e.g. the log4j.xml
from src/main/resources
instead of the one from say src/main/configuration/prod/
Extract from the Maven output :
[INFO] Processing war project
[INFO] Copying webapp webResources [D:\workspace\MyProject/src/main/config/prod] to [D:\workspaces\SITEL\Webfauna\target\Webfauna]
[INFO] Copying webapp webResources [D:\workspace\MyProject\src/main/config/prod] to [D:\workspaces\SITEL\Webfauna\target\Webfauna]
[INFO] Copying webapp resources [D:\workspace\MyProject\src\main\webapp]
As you can see on the last line, stuff from src/main/webapp
is copied AFTER, thus overwriting my custom files :(
My question : How to force Maven to use the files "from the activated profile" and somehow OVERWRITE the "natural" files ?
I see that you would use the same resources but with different configuration in each profile (test and prod).
So, I suggest you to use a configuration like this:
In this scenario, I have two profiles, each one is activated using "env" parameter. Each profile has a file to filter your resources, put your log4j.xml, struts.properties, context.xml files in src/main/resources and use variables to use right configuration in each profile.
Hope that helps.
Here is a simpler version of the solutions above that relies only on a simple snippet inserted into pom.xml.
Build the default (local workstation) webapp with:
Add an environment command line parameter to copy files from an environment-specific directory like resources-prod to the target WEB-INF/classes directory:
Add this inside the project element of pom.xml:
More info and link to working example.
I think you need to overlay the war files. Either creating a dependency in every profile of type war instead of jar, that will overlay the files in your current war file.
Another possibility might be the overlay configuration of the maven-war-plugin.
so the profile would activate the files you want to have copied over the current ones. There is quite some documentation on the plugin site as well with some examples
Hope that works!
As pointed in the second Q/A given by user944849, the simplest solution was to filter out the original files so that they can be replaced by the files from the profile's folder.
So I have added filtering to standard resources :
And to the web resources :
So now the 3 files are not copied into the WAR folder. In the next step (webResources) they are copied from the active profile's folder.
I also added a default folder containing the 3 files with common values. The files from this default folder are also copied, but only after the profile's folder ones. As the resources are not overwritten, they will be copied only if they don't already exist. This is useful if you build without a profile activated, or if you can define sensible default values, no each profile needs to replicate the file if it is identical.
Structure of the config folder:
And the webResources section of my pom.xml:
Take a look at either of these questions/answers. One may work for you.
Run an ant task in maven build phase before war is packaged?
Files got overwritten in maven project when building a war
another attempt :)
I've played around with the overlay parameter. I think that only replaces files within webapp folder you have in another war file inside the webapp folder. So that is not the perfect thing for your setup.
Yet the above mentioned webResource parameter can do this:
The activated profiles will add additional folders to the war file. So that should do the trick!