Web resources filtering with Maven war plugin does not work in Eclipse with m2e

Juan Calero picture Juan Calero · Mar 21, 2012 · Viewed 36.2k times · Source

I'm trying to filter a Spring configuration file using Maven filtering. My POM is configured like this:

        ...
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-war-plugin</artifactId>
          <version>2.2</version>
          <configuration>
            <webResources>
              <resource>
                <filtering>true</filtering>
                <targetPath>WEB-INF/context</targetPath>
                <directory>src/main/webapp/WEB-INF/context</directory>
                <includes>
                  <include>applicationContext.xml</include>
                </includes>
              </resource>
            </webResources>
          </configuration>
        </plugin>
        ...

and

  <profiles>
    <profile>
        <id>desarrollo</id>
         <activation>
          <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <filters>
              <filter>src/main/properties/dev.properties</filter>
            </filters>
      </build>
    </profile>
    <profile>
        <id>pruebas</id>
        <build>
            <filters>
              <filter>src/main/properties/test.properties</filter>
            </filters>
      </build>
    </profile>
            ...

It works great when invoking Maven directly.

Unfortunately, when hot-deploying the webapp in Tomcat 6 with Eclipse WTP and m2e it always picks the unfiltered version of applicationContext.xml. (The file applicationContext.xml in the folder target/m2e-wtp/web-resources/WEB-INF/context is never filtered)

I can't find any useful documentation on the subject. I'm not even sure if it is implemented in m2e.

Is something wrong with my configuration or this is an unimplemented feature?

Answer

Juan Calero picture Juan Calero · Mar 22, 2012

Well, finally I got it.

First of all, I did what khmarbaise pointed out. I moved applicationContext.xml to the resources folder. War plugin webResources are meant to work with external resources, and filtering a file in the destination folder itself was not the best practice. I updated the POM to reflect the new configuration

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>

and

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <webResources>
                    <resource>
                        <filtering>true</filtering>
                        <targetPath>WEB-INF/context</targetPath>
                        <directory>src/main/resources/WEB-INF/context</directory>
                        <includes>
                            <include>applicationContext.xml</include>
                        </includes>
                    </resource>
                </webResources>
            </configuration>
        </plugin>

So, half of the credit to him. But that's was not enough, it still didn't work. I realized that Maven/m2e was indeed filtering my file, but it didn't get my defined properties files. After some testing I found out that m2e is ignoring the activeByDefault option in the profiles activation section.

So, I added my default profile to the project Maven configuration and then it worked

enter image description here