Skip to content
July 8, 2010 / edeustace

Processing resources with the Maven war plugin

The problem:
You have some files in your maven web application folder that you’d like to process and then add them to the generated .war. However the maven war plugin doesn’t have a phase that will let you execute such processing before the war is built.

The solution:
Configure an additional folder as a ‘webResource’ in the war plugin configuration, that you will send your processed files to.

1. In a earlier build phase (eg: process-resources) copy the source file that you want to process to predefined folder (eg: target/preassembly).
2. Process the file however you like
3. When the war plugin begins, because you have specified the folder where the processed file is as a webResource, it will take that file and copy it into the war, overwriting the file that was originally copied over from src/main/resources.

So say we have a simple war project that has a properties file:
/src/main/resources/myapp.properties
And this properties file contains a token: “${tokenToReplace}” that you want to replace.

To modify something within this file and have it added to your .war, the following pom.xml will do the job:

<project 
  ...>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ee.example</groupId>
  <artifactId>maven-war-plugin-processing-example</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <properties>
	<preassembly.dir>${basedir}/target/preassembly</preassembly.dir>
  </properties>
  <build>
    <finalName>maven-war-plugin-processing-example</finalName>
	<plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.0</version>
        <configuration>
          <webResources>
            <resource>
				<!-- copy resources from this folder into the webapp before packaging -->
              <directory>${preassembly.dir}</directory>
            </resource>
          </webResources>
        </configuration>
      </plugin>
	  
	 <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.4</version>
        <executions>
          <execution>
			<!-- do some processing on your resources files -->
            <phase>process-resources</phase>
            <configuration>
              <tasks>
				<echo message="processing files..."/>
				<!-- copy the file to be processed -->
				<copy 
					file="${basedir}/src/main/resources/myapp.properties" 
					todir="${preassembly.dir}"/>
				<!-- do the processing (replace the token value with a value passed in on the command line) -->
				<replace 
					summary="true"
					file="${preassembly.dir}/myapp.properties"
					token="${tokenToReplace}" value="${value.to.input}"/>
                
              </tasks>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
	  
    </plugins>
  </build>
</project>

You can add a value here from the command line if you want:
mvn install -Dvalue.to.input=”I’m the new value”

The line above will add “I’m the new value” as the value for the ‘value’ key in myapp.properties.

Credit to:
http://www.mail-archive.com/users@maven.apache.org/msg104443.html
http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html
http://www.mail-archive.com/users@maven.apache.org/msg96185.html

Advertisements

2 Comments

Leave a Comment
  1. hjohn06 / Dec 8 2010 8:57 pm

    Nice post. I was looking for something like this. I was one step away before reading your post. Well I was exploding the war and then trying to keep the plugin from copying the files overwriting my changes during packaging, but this is easier. This is a lot simpler. But I believe you could have just used filters on the war plugin, since you are using maven style variables (the ${variable} thing). I was looking to replace values without using ${variable}. I need a default value in place prior to maven running.

  2. edeustace / Dec 12 2010 2:38 am

    Thanks for the tip, I’ll check it out. I wonder about the filtering file structure. Is that set in stone? The docs don’t really explain why its in that structure. So I wonder can one pass in variables from the command line?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: