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 renameresources 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 | |
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.