How do I filter resource files to include the values of properties?
Author: Deron Eriksson
Description: This tutorial describes how to filter resource files in maven.
Tutorial created using: Windows Vista || JDK 1.6.0_04 || Eclipse Web Tools Platform 2.0.1 (Eclipse 3.3.1)


Page:    1 2 >

Resource files can contain references to properties (in the form ${...}). By default, at build time mavenSW doesn't resolve these values. However, it's possible to filter resource files to resolve property values by specifying build.resources.resource.filtering to be 'true' in your pom.xml file. If you do this, you also need to explicitly specify the build.resources.resource.directory, as shown in the pom.xml file below.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.maventest</groupId>
	<artifactId>aproject</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>aproject</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
			</resource>
		</resources>
	</build>
</project>

My project contains the following resource file, textfile.txt, that contains a reference to the project.artifactId property.

src/main/resources/textfile.txt

this is a test
artifact id: ${project.artifactId}

I'll perform a "mvn clean process-resources" to process the resource files in the project. The "process-resources" is a phase in the maven default lifecycle.

Executing 'mvn clean process-resources' on 'aproject'

The following console output is generated as a result of the "mvn clean process-resources"

Console output from 'mvn clean process-resources' on 'aproject'

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building aproject
[INFO]    task-segment: [clean, process-resources]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean]
[INFO] Deleting directory C:\dev\workspace\aproject\target
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Fri Feb 15 17:21:20 PST 2008
[INFO] Final Memory: 3M/6M
[INFO] ------------------------------------------------------------------------

(Continued on page 2)

Page:    1 2 >