I wrote a Java Web Application where I replace URLs to static content at build time to add version information, primarely for caching.
For example, href="myapp/css/default.min.css"
is turned into href="myapp-0.2.8/css/default.min.css"
I am using the maven maven-replacer-plugin and things work fine for one single file:
Using the file-Tag for single file replacements.
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.2</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<ignoreMissingFile>false</ignoreMissingFile>
<file>${project.build.directory}/myApp/index.jsp</file>
<replacements>
<replacement>
<token>%PROJECT_VERSION%</token>
<value>${project.version}</value>
</replacement>
</replacements>
</configuration>
</plugin>
Maven Debug Output shows this for the working example.
[DEBUG] Configuring mojo 'com.google.code.maven-replacer-plugin:replacer:1.5.2:replace' with basic configurator -->
[DEBUG] (s) basedir = .
[DEBUG] (s) commentsEnabled = true
[DEBUG] (s) encoding = UTF-8
[DEBUG] (s) file = /Users/phisch/Development/MyApp/Workspace/MyApp-WebApp/target/myApp/index.jsp
[DEBUG] (s) ignoreErrors = false
[DEBUG] (s) ignoreMissingFile = false
[DEBUG] (s) preserveDir = true
[DEBUG] (s) quiet = false
[DEBUG] (s) token = %PROJECT_VERSION%
[DEBUG] (s) value = 0.3
[DEBUG] (s) replacements = [com.google.code.maven_replacer_plugin.Replacement@3bccdcbd]
[DEBUG] (s) skip = false
[DEBUG] (s) unescape = false
[DEBUG] -- end configuration --
[DEBUG] Replacement run on /Users/phisch/Development/MyApp/Workspace/MyApp-WebApp/target/myApp/index.jsp and writing to /Users/phisch/Development/MyApp/Workspace/MyApp-WebApp/target/myApp/index.jsp with encoding UTF-8
[INFO] Replacement run on 1 file.
According to the Usage Guide I should be able to use multiple files with includes:include
But the following pom.xml configurations does nothing (Note the include-Tags startin at line 15)
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.2</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<ignoreMissingFile>false</ignoreMissingFile>
<includes>
<include>${project.build.directory}/myApp/index.jsp</include>
</includes>
<replacements>
<replacement>
<token>%PROJECT_VERSION%</token>
<value>${project.version}</value>
</replacement>
</replacements>
</configuration>
</plugin>
The Debug output is as follows. The file exists.
DEBUG] Configuring mojo 'com.google.code.maven-replacer-plugin:replacer:1.5.2:replace' with basic configurator -->
[DEBUG] (s) basedir = .
[DEBUG] (s) commentsEnabled = true
[DEBUG] (s) encoding = UTF-8
[DEBUG] (s) ignoreErrors = false
[DEBUG] (s) ignoreMissingFile = false
[DEBUG] (s) includes = [/Users/phisch/Development/MyApp/Workspace/MyApp-WebApp/target/myApp/index.jsp]
[DEBUG] (s) preserveDir = true
[DEBUG] (s) quiet = false
[DEBUG] (s) token = %PROJECT_VERSION%
[DEBUG] (s) value = 0.3
[DEBUG] (s) token = %MyApp_PROJECT_VERSION%
[DEBUG] (s) value = 0.3 (Build: 20130301-1130)
[DEBUG] (s) replacements = [com.google.code.maven_replacer_plugin.Replacement@235d4338, com.google.code.maven_replacer_plugin.Replacement@3fe823ab]
[DEBUG] (s) skip = false
[DEBUG] (s) unescape = false
[DEBUG] -- end configuration --
[INFO] Replacement run on 0 file.
How can I replace the same token/value pairs in multiple files?
The includes
tag works with version 1.5.2 as well, you just have to specify the basedir
tag before includes
, and put the filepath (excluding the filename) as the basedir
value and just the filename as the include
tag value. So in your case something like this should work:
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.2</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<basedir>${project.build.directory}/myApp</basedir>
<includes>
<include>index.jsp</include>
</includes>
<replacements>
<replacement>
<token>%PROJECT_VERSION%</token>
<value>${project.version}</value>
</replacement>
</replacements>
</configuration>
</plugin>
This seems to be a bug in the latest 1.5.2 Version.
As soon as I change the version on bugfix level down to 1.5.1, the Not Working Example works just as expected and all tokens are replaced by their values.
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.1</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>${project.build.directory}/myApp/index.jsp</include>
</includes>
<replacements>
<replacement>
<token>%PROJECT_VERSION%</token>
<value>${project.version}</value>
</replacement>
</replacements>
</configuration>
</plugin>
I also removed the ignoreMissingFile as suggested by ben.
From the maven-replacer-plugin doc :
ignoreMissingFile: Set to true to not fail build if the file is not found. First checks if file exists and exits without attempting to replace anything. Only usable with file parameter.
So I suggest to remove this parameter when using the <includes>
EDIT: use maven-replacer-plugin version 1.5.1 since version 1.5.2 seems buggy regarding this feature (thanks to phisch for this precision)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With