Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 436
Juan Calero Avatar asked Mar 21 '12 09:03

Juan Calero


3 Answers

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

like image 123
Juan Calero Avatar answered Oct 27 '22 01:10

Juan Calero


I had a similar problem with filtering the web.xml. I solved the problem by reimporting the whole project in eclipse.

The reason was a corrupt /.settings/org.eclipse.wst.common.component file. In this file the order of the files copied to the local web servers deploy directory is defined. For example:

<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
  <wb-module deploy-name="liquidvote-rest">
    <wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
    <wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
    <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
    <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
    <property name="context-root" value="myapp"/>
    <property name="java-output-path" value="/myapp/target/classes"/>
  </wb-module>
</project-modules>

If the web.xml or application.xml exists in several directories it will be taken from the first directory found. Therefore its important that

    <wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>

is the first entry.

You will find more informations at http://wiki.eclipse.org/M2E-WTP_FAQ in the section "What is this web resources folder?"

like image 44
insideout Avatar answered Oct 27 '22 01:10

insideout


Have you tried to put the resources under src/main/resources/WEB-INF/... instead of and configured the resources area to filter the resources instead of putting configuration into a non default maven location.

like image 20
khmarbaise Avatar answered Oct 27 '22 01:10

khmarbaise