title: How to overwrite files in the WAR file during maven build?
body: <p>I have a Java webapp project that I develop in Eclipse (more precisely MyEclipse 10) and build using Maven 3.</p>
  
  <p>I have the following layout (including only the files relevant to my problem :</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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 <code>src/main/resources</code> and <code>src/main/webapp</code> 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.</p>
  
  <p>Then, when I want to build a release for another environment such as testing or production, I run <code>mvn -P test clean install</code> and it builds a nice WAR.</p>
  
  <p>My goal is to replace the configuration files inside the final WAR by those in <code>src/config/{environment}/</code>.</p>
  
  <p>I have set profiles in my <code>pom.xml</code>:</p>
  
  <pre><code>&lt;profiles&gt;
      &lt;profile&gt;
          &lt;id&gt;test&lt;/id&gt;
          &lt;properties&gt;
              &lt;environment&gt;test&lt;/environment&gt;
          &lt;/properties&gt;
      &lt;/profile&gt;
  
      &lt;profile&gt;
          &lt;id&gt;prod&lt;/id&gt;
          &lt;properties&gt;
              &lt;environment&gt;prod&lt;/environment&gt;
          &lt;/properties&gt;
      &lt;/profile&gt;
  &lt;/profiles&gt;
  </code></pre>
  
  <p>Then, I try to copy these resources from the specified profile (using the <code>environment</code> variable) to the correct location inside the WAR (or the temporary folder that will be zipped into a WAR) :</p>
  
  <pre><code>&lt;webResources&gt;
      &lt;resource&gt;
          &lt;directory&gt;/src/main/config/${environment}&lt;/directory&gt;
          &lt;targetPath&gt;META-INF/&lt;/targetPath&gt;
          &lt;includes&gt;
              &lt;include&gt;context.xml&lt;/include&gt;
          &lt;/includes&gt;
      &lt;/resource&gt;
      &lt;resource&gt;
          &lt;directory&gt;src/main/config/${environment}&lt;/directory&gt;
          &lt;targetPath&gt;WEB-INF/classes/&lt;/targetPath&gt;
          &lt;includes&gt;
              &lt;include&gt;
                  struts.properties
              &lt;/include&gt;
              &lt;include&gt;
                  log4j.xml
              &lt;/include&gt;
          &lt;/includes&gt;
      &lt;/resource&gt;
  &lt;/webResources&gt;
  </code></pre>
  
  <p>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 <code>log4j.xml</code> from <code>src/main/resources</code> instead of the one from say <code>src/main/configuration/prod/</code></p>
  
  <p>Extract from the Maven output :</p>
  
  <pre><code>[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]
  </code></pre>
  
  <p>As you can see on the last line, stuff from <code>src/main/webapp</code> is copied AFTER, thus overwriting my custom files :(</p>
  
  <p>My question : How to force Maven to use the files "from the activated profile" and somehow OVERWRITE the "natural" files ?</p>