Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8647185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:00:03+00:00 2026-06-12T13:00:03+00:00

I have a Java webapp project that I develop in Eclipse (more precisely MyEclipse

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:00:05+00:00Added an answer on June 12, 2026 at 1:00 pm

    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 :

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <!-- Exclude those since they are copied from the profile folder for the build -->
                    <exclude>log4j.xml</exclude>
                    <exclude>struts.properties</exclude>
                </excludes>
                <filtering>false</filtering>
            </resource>
        </resources>
    

    And to the web resources :

    <warSourceExcludes>**/context.xml</warSourceExcludes>
    

    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:

    -- config
       |-- test
       |   |--log4j.xml
       |   |   |--struts.properties
       |   |   +--context.xml
       |   +-- prod
       |   |   |--log4j.xml
       |   |   +--context.xml
       |   +-- default
       |       |--log4j.xml
       |       |--struts.properties
       |       +--context.xml
    ...
    

    And the webResources section of my pom.xml:

    <webResources>
        <!-- Resources from the activated profile folder -->
        <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>
        <!-- Default resources in case some file was not defined in the profile folder -->
        <!-- Files are not overwritten so default files will be copied only if it does not exist already -->
        <resource>
            <directory>/src/main/config/default</directory>
            <targetPath>META-INF/</targetPath>
            <includes>
                <include>context.xml</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/config/default</directory>
            <targetPath>WEB-INF/classes/</targetPath>
            <includes>
                <include>
                    struts.properties
                </include>
                <include>
                    log4j.xml
                </include>
            </includes>
        </resource> 
    </webResources>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a maven project that I run using jetty: $ mvn run:jetty Where
I have a multi module Spring project that I set up using Maven: my-root
I have a really simple webapp project with maven and jetty that has been
I have a Java EE webapp that works perfectly when I run it on
I have a Java web app running on Tomcat 7 that is using JDBC
Situation is pretty straightforward. I have a Java webapp that I'm converting to be
I have a project created in Eclipse that has roughly this structure: src conf
I have a Java/Spring MVC/JSP/JSTL webapp. We have a designer that gave us some
I am currently trying to build a Java Project with several dependencies. I have
I have a war packaging Maven project, in which I have a Java properties

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.