Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: how to fill a variable in web.xml file

I am working in a Maven web project through Eclipse. In the web.xml, I have a context-param which value should change according the profile I use when I run the Maven.

<context-param>
    <param-name>producao</param-name>
    <param-value>${ambiente.producao}</param-value>
</context-param>

In the pom file for project I have the following configuration:

<project>

    <profiles>
        <profile>
            <id>prod</id>
            <properties>
                <ambiente.producao>true</ambiente.producao>
            </properties>
        </profile>
        <profile>
            <id>desenv</id>
            <properties>
                <ambiente.producao>false</ambiente.producao>
            </properties>
        </profile>
    </profiles>

    <build>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/webapp/WEB-INF/</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/web.xml</include>
                </includes>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <webResources>
                        <resource>
                            <filtering>true</filtering>
                            <directory>src/main/webapp</directory>
                            <includes>
                                <include>**/web.xml</include>
                            </includes>
                        </resource>
                    </webResources>
                    <warSourceDirectory>src/main/webapp</warSourceDirectory>
                    <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
                </configuration>
            </plugin>
        </plugins>

    </build>

</project>

I am using both resources tag as maven-war-plugin plugin according to the references I found on the internet. However, it did not work as expected. In the Eclipse I run the maven with the goals clean install and as "Profiles" either prod or desenv. After I run the Maven, I observed that in the web.xml, the ${ambiente.producao} property is not replaced. Therefore, I would like to know what I did wrong. Should I use only Filtering resource or the maven-war-plugin?

Thanks,

Rafael Afonso

like image 896
Rafael Afonso Avatar asked Dec 19 '14 12:12

Rafael Afonso


1 Answers

Don't believe everything you read on the Internet. You should remove src/main/webapp/WEB-INF/ from the resources list, it will just filter your web.xml to target/classes

Your maven-war-plugin configuration just needs :

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.5</version>
  <configuration>
    <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
  </configuration>
</plugin>

Now, whether you build in Maven CLI or within Eclipse, you need to enable one of these profiles. In command line, you need to run

mvn clean package -Pdesenv

In Eclipse (assuming you're using the Luna Java EE distribution or more recent), you can enable the profile you want by pressing Ctrl+Alt+P. If you enable desenv, you'll see the filtered web.xml under target/m2e-wtp/web-resources/WEB-INF/ This is the file that will get published to your application server.

If you happen to enable both profiles at the same time, the latest profile listed in your pom.xml takes precedence.

Personally, I would remove the production profile and put the production properties in the default properties section of your pom.xml. That way, you always filter your web.xml with actual values and you'll have less chances of accidentally building and deploying your app with non-production settings.

And in order to enable the desenv profile automatically in Eclipse, you can add the following activation rule :

<profile>
  <id>desenv</id>
  <!-- This profile is only activated when building in Eclipse with m2e -->
  <activation>
    <property>
      <name>m2e.version</name>
    </property>
  </activation>
  <properties>
    <ambiente.producao>false</ambiente.producao>
  </properties>
</profile>

More on dynamic resource filtering support in m2e-wtp here: https://developer.jboss.org/en/tools/blog/2011/05/03/m2eclipse-wtp-0120-new-noteworthy

And a screencast there : http://screencast.com/t/hwodHqODBB

like image 53
Fred Bricon Avatar answered Oct 16 '22 14:10

Fred Bricon