If you want to filter and copy your project resources to the output directory, the Apache Maven Resources Plugin is the best plugin to perform that. But sometimes you want to filter and rename resources with Maven and the last version of the Maven Resources Plugin (current 3.0.1) does not provide a direct way to do that.
This tutorial will show a workaround to filter and rename resources with Maven.
Project example
Let’s consider a file file-to-rename.txt located under src/main/ which we want to filter and rename to renamed-file.txt.
Below the project structure :
The content of file-to-rename.txt :
1 2 3 |
firstname=${user.firstname} lastname=${user.lastname} date=${maven.build.timestamp} |
Content of pom.xml :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.roufid.tutorial</groupId> <artifactId>filter-copy-rename-resource-tutorial</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>Filter, copy and rename resource tutorial</name> <properties> <user.firstname>Radouane</user.firstname> <user.lastname>ROUFID</user.lastname> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.0.1</version> <executions> <execution> <id>filter-resources</id> <phase>process-resources</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.directory}</outputDirectory> <resources> <resource> <filtering>true</filtering> <directory>${basedir}/src/main</directory> <include>**/*</include> </resource> </resources> </configuration> </execution> </executions> </plugin> <plugin> <groupId>com.coderplus.maven.plugins</groupId> <artifactId>copy-rename-maven-plugin</artifactId> <version>1.0</version> <executions> <execution> <id>copy-and-rename-file</id> <phase>compile</phase> <goals> <goal>rename</goal> </goals> <configuration> <sourceFile>${project.build.directory}/file-to-rename.txt</sourceFile> <destinationFile>${project.build.directory}/renamed-file.txt</destinationFile> </configuration> </execution> </executions> </plugin> </plugins> </build> </project> |
Building the project
Run a mvn install to build the project if you use command line or Right-Click -> Run As -> Maven install on Eclipse.
After a refresh, below the new project structure :
And the content of renamed-file.txtis :
1 2 3 |
firstname=Radouane lastname=ROUFID date=2016-12-06T10:19:19Z |
What happened ?
The project build is on two step. First maven-resources-plugin is used at the copy-resources lifecycle phase to filter the resources located under /src/main and copy them to the target directory. Next, copy-rename-maven-plugin is used to rename the filtered output file file-to-rename.txt to renamed-file.txt.