I want to rename static files when building my WAR file using maven-war-plugin, with version number. For intance, in my project I have a file:
src/main/webapp/css/my.css
I want it to appear in the WAR file as:
css/my-versionNumber.css
The same question was already asked (see Rename static files on maven war build), but the accepted answer is to rename a directory. I do not want to rename a directory, I want to rename the actual file.
Is this possible?
You need define
After that call it by EL css/${css.version}.css
OK, I found a way, the principles come from: Automatic update of generated css files via m2e.
Renaming files
I chose to use Maven AntRun Plugin, because it allows to rename several files using a replacement pattern, see https://stackoverflow.com/a/16092997/1768736 for details.
Alternative solutions were to use:
maven-assembly-plugin
: but it doesn't support renaming of filesets, only renaming of individual files (see file assembly descriptor and comments of this answer: https://stackoverflow.com/a/4019057/1768736)maven-resources-plugin
: it allows to copy resources, not to rename them (see copy resources mojo and comments of this question: Renaming resources in Maven)Make renamed files available for
maven-war-plugin
Copy files: the idea is to copy the renamed files into a temp directory, to be added by
maven-war-plugin
to the WAR file as a web-resource.maven-war-plugin
builds the WAR during thepackaging
phase, so we will need to copy renamed files before that.Prevent
maven-war-plugin
to manage the files to be renamed bymaven-antrun-plugin
: this is done by using the parameterwarSourceExcludes
.Make it work from within Eclipse with m2e-wtp
Change lifecycle mapping: the problem is that
m2e
by default doesn't execute all lifecycles defined in the POM file (to see the lifecycles executed/ignored, from Eclipse, go to your project properties, thenMaven
>Lifecycle Mapping
). So you need to use the fake pluginorg.eclipse.m2e.lifecycle-mapping
to add themaven-antrun-plugin
lifecycle, see life cycle mapping documentation.Change
maven-antrun-plugin
output directory: the problem is thatm2e-wtp
acquires its web-resources before any lifecycle can be launched, so, beforemaven-antrun-plugin
can rename the files. As a workaround, we create a profile, activated only when the project is built bym2e
, to change the property used to setmaven-antrun-plugin
output directory, to write directly intom2e-wtp
web-resources.That's it! POM snippet: