Maven war plugin

Published: Sunday, 15 December 2013

This plugin builds your war (webapp).

Filtered resources

I wanted to add the build number to the final war. Filtering is a build step where placeholders will be replaced with dynamic values at build time.

Create a parallel webapp directory

Create another directory src/main/webapp-filtered. We make this separate to the default src/main/webapp directory because that usually contains binary files that should not be filtered.

Create a build.json

Add the file src/main/webapp-filtered/build.json. This is a text file with contents:

{
  "build": "${build.number}"
}

Add config to pom.xml

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <webResources>
            <resource>
              <directory>src/main/webapp-filtered</directory>
              <filtering>true</filtering>
            </resource>
          </webResources>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

The above tells maven to include the src/main/webapp-filtered directory when building the webapp, and also to do filtering at build time for files under this directory.

If you are using IntelliJ, then version 12 or higher should recognise this new directory

Test

mvn clean install -Dbuild.number=35

Verify that the build.json in your target directory contains the build number 35.

References

Adding filtered resources