44

I am trying to add some filtering to the application context file, which resides in the WEB-INF directory.

I have the file which is to be filtered (xmlgateway-context.xml) in the folder /src/main/resources.

I have the properties files (config-e05.properties) in the folder src/main/filters

And I have the POM set up as follows:

<!-- environment profiles -->
<profiles>
 <profile>
  <id>e04</id>
  <properties>
   <targetenv>e04</targetenv>
  </properties>
 </profile>
 <profile>
  <id>e05</id>
  <properties>
   <targetenv>e05</targetenv>
  </properties>
 </profile>
</profiles>

<!-- build settings (filtering) -->
<build>
 <filters>
  <filter>src/main/filters/config-${targetenv}.properties</filter>
 </filters>
 <resources>
  <resource>
   <targetPath>WEB-INF</targetPath>
   <filtering>true</filtering>
   <directory>src/main/resources</directory>
  </resource>
 </resources>
</build>

This will mvn install correctly, but when I open the output war file, I was expecting the file xmlgateway-context.xml to be in the /WEB-INF directory, but it ends up in the folder /WEB-INF/classes/WEB-INF.

How can I get this file into the right place.

Alternatively, can I put the application context into a different location and have it referenced there.

Robert Munteanu
  • 67,031
  • 36
  • 206
  • 278
Xetius
  • 44,755
  • 24
  • 88
  • 123

4 Answers4

92
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
            <webResources>
                <resource>
                    <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                    <filtering>true</filtering>
                    <targetPath>WEB-INF</targetPath>
                    <includes>
                        <include>**/xmlgateway-context.xml</include>
                     </includes>
                </resource>
            </webResources>
        </configuration>
    </plugin>
</plugins>

Add the above to your pom.xml. EDIT: Just to explain what the above conf is doing. With this added, mvn is going to filter files under src/main/webapp/WEB-INF and in particular filter the included files xmlgateway-context.xml and after filtering it is going to push the files in WEB-INF folder (thats what the target tag is saying).

Update if something is not clear.

Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60
peakit
  • 28,597
  • 27
  • 63
  • 80
  • 5
    Just an observation to add, version 2.4 doesn't seem to work with this configuration, the most recent and working with this setting I found was 2.3, cheers. – tomasz_kusmierczyk Aug 25 '13 at 17:40
  • How is this different than simply configuring the `src/main/webapp` directory as a filtered resource directory as any other? What benefit is there to associate it with a configuration of the `maven-war-plugin`? – Garret Wilson Dec 14 '16 at 22:07
  • confirming that this works with 3.3.2 version of maven-war-plugin. in the official documentation it is not clear that an absolute path should be used when specifying - and to give answer to another comment - i think that stuff put in the usual filtered resources will end up inside WEB-INF/classes directory within the WAR file, with no option to put anything outside of it – hello_earth Jan 31 '23 at 10:50
5

you should configure filtering via the maven war plugin: checkout these examples.

dfa
  • 114,442
  • 31
  • 189
  • 228
  • 4
    Would have accepted this as well if I could thanks to links, but @peakit wins due to cut and paste laziness for me. – Xetius Nov 23 '09 at 11:25
0

With filteringDeploymentDescriptors set to true

     <build>
        <finalName>web-project-name</finalName>
        <filters>
            <filter>src/main/resources/app.properties</filter>
        </filters>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
                </configuration>
            </plugin>           
        </plugins>
    </build>
Igor Vuković
  • 742
  • 12
  • 25
-2

Put the file xmlgateway-context.xml in src/main/webapp/WEB-INF and configure like this:

<build>
    <filters>
        <filter>src/main/filters/config-${targetenv}.properties</filter>
    </filters>
    <resources>
        <resource>
                <filtering>true</filtering>
                <directory>src/main/webapp/WEB-INF</directory>
        </resource>
    </resources>
</build>
EJB
  • 2,383
  • 14
  • 15