Kohei Nozaki's blog 

Conditional inclusion of whole element for deployment descriptor with Maven


Posted on Wednesday Jan 21, 2015 at 11:26AM in Technology


Sometimes I need to tweak deployment descriptors (e.g. web.xml) for different environment (e.g. development/integration-test/production). I frequently see the case which tweaks only values of context-param, or choose appropriate one from separated files for each environment but I’m looking for a way to include or exclude whole an element using single template. I found a solution so I leave some note about that.

pom.xml

Specify <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors> in plugin configuration.

<build>
    <plugins>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
            </configuration>
        </plugin>
    </plugins>
</build>

Define variables for each environment. in my case I need to include a context-param of JSF for development environment only. if I didn’t use profile named development when the time of build, then that context-param will be simply excluded from web.xml.

    <profiles>
        <profile>
            <id>development</id>
            <properties>
                <context-params><![CDATA[ -->
    <context-param>
        <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
        <param-value>0</param-value>
    </context-param>
    <!-- ]]></context-params>
            </properties>
        </profile>
        <profile>
            <id>default</id>
            <properties>
                <context-params></context-params>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
    </profiles>

web.xml

Just put an expression inside XML comment syntax. it’s intended to avoid that complaining XML editor of your IDE.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <!-- ${context-params} -->
    ...
</web-app>

Processed web.xml

If I build with mvn clean package -P development then web.xml will be:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <!-- -->
    <context-param>
        <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
        <param-value>0</param-value>
    </context-param>
    <!-- -->
    ...

If I use mvn clean package then web.xml will be:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <!--  -->
    ...



Comments:

Thanks! It works :)

Posted by Ignatius on August 25, 2016 at 05:29 PM JST #


Very useful, Thank you!

Posted by Nicola Musatti on November 19, 2016 at 01:29 AM JST #


Leave a Comment

HTML Syntax: NOT allowed