Resource Filtering with Maven - bahkified/Notes GitHub Wiki

Maven can do simple string substitution for project resources.

Take, for example, the .properties file below. One of the properties has the ${…} notation. Maven recognizes this form and is able to replace it with the correct property as defined in the POM.

defaultErrorPage=uncheckedException
defaultErrorMessage=${unhandled.error.message}

another.property=Hello!

In the POM, the unhandled.error.message property is defined in the <properties> tag.

<properties>
    <unhandled.error.message>An error has occurred.</unhandled.error.message>
</properties>

Also in the POM, Maven needs to be told that there are resources that need to be filtered. For resources on the projects classpath, the maven-resources-plugin is used.

<build>
    …
    <plugins>
        …
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <!-- specify UTF-8, ISO-8859-1 or any other file encoding -->
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
    </plugins>

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

</build>

If the resources to be filter are not on the classpath, but instead are in the webapps directory, filtering must be done in the maven-war-plugin.

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <webResources>
                <resource>
                    <directory>src/main/webapp</directory>
                    <filtering>true</filtering>
                    <includes>
                        <include>**/*.jsp</include>
                    </includes>
                </resource>
            </webResources>
        </configuration>
    </plugin>

Resources

⚠️ **GitHub.com Fallback** ⚠️