Usage GuideParameters used in any execution
Selecting which files to perform replacements upon
Note: You must either supply a file, or list of includes (excludes are optional). Parameters for replacing text
Note: Either a token, tokenFile or tokenValueMap is required. An execution should not contain more than one of these. ExamplesComplete examples can be found in the plugin's test utility. http://code.google.com/p/maven-replacer-plugin/source/browse/test-plugin-use/pom.xml Single regex file replacement with output file: <build>
<plugins>
...
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>maven-replacer-plugin</artifactId>
<version>(version)</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<ignoreMissingFile>true</ignoreMissingFile>
<file>target/someapp/jsp/helloWorld.jsp</file>
<outputFile>
target/someapp/jsp/helloWorld-updated.jsp
</outputFile>
<regex>false</regex>
<token>$BUILD_NUMBER$</token>
<value>${buildNumber}</value>
</configuration>
</plugin>
...
</plugins>
</build>Using regular expression flags: <build>
<plugins>
...
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>maven-replacer-plugin</artifactId>
<version>(version)</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>target/someapp/jsp/helloWorld.jsp</file>
<token>ToKeN</token>
<value>value</value>
<regexFlags>
<regexFlag>CASE_INSENSITIVE</regexFlag>
<regexFlag>MULTILINE</regexFlag>
</regexFlags>
</configuration>
</plugin>
...
</plugins>
</build>Multiple file replacement: <build>
<plugins>
...
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>maven-replacer-plugin</artifactId>
<version>(version)</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>target/**/replace-me.*</include>
</includes>
<excludes>
<exclude>target/**/do-not-replace-me.*</exclude>
</excludes>
<token>TOKEN</token>
<value>VALUE</value>
</configuration>
</plugin>
...
</plugins>
</build>Single file replacement using a tokenValueMap: <build>
<plugins>
...
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>maven-replacer-plugin</artifactId>
<version>(version)</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>target/classes/database.properties</file>
<tokenValueMap>etc/${environment}/database.conf</tokenValueMap>
</configuration>
</plugin>
...
</plugins>
</build>Multiple token/value replacements per file: <build>
<plugins>
...
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>maven-replacer-plugin</artifactId>
<version>(version)</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>target/classes/database.properties</file>
<replacements>
<replacement>
<token>token1</token>
<value>value1</value>
</replacement>
<replacement>
<token>token2</token>
<value>value2</value>
</replacement>
</replacements>
</configuration>
</plugin>
...
</plugins>
</build>Notes on some difficulties integrating maven-replacer-plugin into your build: The phase and time of execution depends largely on the desired use and the type of packaging. There can be issues with WAR packaging and replacing content at the right time to be included up by the WAR (See Usage with other plugins). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
How would you configure the plugin for multiple token/values per file. Where we have multiple files?
Hi Martin,
do have a solution to use multiple files?
Hi Martin and Kai,
There are two ways to select multiple files for replacement. Either use, filesToInclude or includes:include.
Then combine this with either as many replacements:replacment elements as you need (see last example) or use a tokenValueMap in your configuration.
hi,
Trying to understand the role of this plugin. What does it add to standard maven filtering? http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
I feel this plugin has some interesting things (like regexp filtering) but that it should better have been contributed to maven-resources-plugin than creating a new one, since it confuses the filtering process for users. What do you think?
Cheers
The maven-replacer-plugin is suited to niche (or more advanced) replacement logic which is not offered in resources plugin. Adding some of the features in this plugin to resources plugin would only confuse the use resources.
Looks like a great plugin - wondering if/how it would work for our purposes (sorry, I'm not very experienced with Maven). At build time, we'd like to have a configurable list of common CSS/JS files concatenated into one CSS/JS file (i.e. common.css/js). All HTML references to files contained within the common file would need to be removed and one of them replaced with a link to the new common file.
We'd then like to be able to search the rest of the HTML page for any non-common CSS/JS files and concat all of those into another CSS/JS file. Again, this would require replacing existing links to a link to only one file. Does Maven, along with this maven-replacer-plugin, support searching/concatenating files based on search results/replacing file references in HTML? If not, any ideas for build-time tools that may help? Our goal is to reduce the number and size of files that need to be fetched when the app is deployed to vastly improve performance.
Thanks very much, Gaurav, Sakai Project
Hi Gaurav,
The maven-replacer-plugin does not concatenate files.
If you are wanting to combine css/js files, I would suggest the yuicompressor plugin's aggregation use: http://alchim.sourceforge.net/yuicompressor-maven-plugin/ex_aggregation.html
As for replacing all adhoc css/scripts in various files, it might be better practice to alter the files before building them so that they reference your compressed files created by yuicompressor.
If there are a lot of these files, you might want to write an ant script which finds all script and style tags and write their contents to some file which is then used by the yuicompressor. Again, I would suggest only doing this as a one time modification to the source, rather than as some magic that takes place during the build.
I hope this helps, Steven
Hi
I've been trying without success to set up a configuration whereby I can process multiple input files (all with the same replacement) and save the processed files into some target directory. Is this supported at all? I'm thinking of something like:
<configuration>
</configuration>Hi Maffeis,
I assume that the include should have contained an asterix before .sql? Currently there is no support for a target directory (outputDir).
This does sound like a good idea, so I will submit an Enhancement Issue for this feature. In the meantime, you can feel free to grab a copy of the source and make the modification yourself.
Thanks, Steven
Hi Maffeis,
Your enhancement has been completed and is available in version 1.3.3. Please try it out :-)
Thanks, Steven
Nice plugin. Now I finally have an alternative to using antrun plugin and replacing whole files.
To bmathus's comment. It is not always desireable to have maven variables in your files. I have a project that uses eclipse for development and maven for production builds. Eclipse wouldn't know what to do with maven variables inside of files.
Just need to execute the following commands: mvn com.google.code.maven-replacer-plugin:maven-replacer-plugin:replace
:)
It seems not possible in a multimodule project to read or write file in the parent project. I have the following structure :
In the child-web pom.xml, im trying to create the result file in ../target. I have tried the ../ path in the outputFile and outputBasedir without success. The plugin try to write in the child project in ${baseDir}\..\myfile.txt which dont exist. It seems impossible to have access in read/write of files in the parent project
Any suggestion ?
Try adding: <basedir>../${basedir}</basedir> to your configuration.
I am using this plugin, works fine BUT :) I just wonder why all the examples use hardcoded "target" directory instead of ${project.build.directory}. Well, I tried. And it appears that ${project.build.directory} is somehow not resolved when used in an include. Once changed back to "target" it works again. Any ideas? Thanks
zyka.jan - I suspect the basedir is having an impact. Try setting <basedir>/</basedir> into your configuration.
That was my first idea. Didn't help. Second was <basedir />. Didn't help either. The solution actually was to set the ${project.build.directory} as basedir: <basedir>${project.build.directory}</basedir>. I think this is generally better than hardcode 'target' everywhere and might be considered to get into the examples :) Of course only where appropriate. There are probably situations when the default <basedir>.</basedir> is just fine.
I ran into the same problem and set it to target, which works fine. I used ${project.build.directory) also, but after some debuggin I noticed that the plugin also adds ${basedir}, so you'll get a wrong path to where your files are and no replacements are done. But setting basedir to project.build.directory indeed seems like a better option. I'll try that.
I'm trying to replace default config values in a web.xml file and cannot find a way to replace a multiline token (which contains tokens).
I'd like to replace:
<context-param>
</context-param>With:
<context-param>
</context-param>I cannot just tokenize the param-value because of our current build and was hoping to be able to replace the entire <context-param> block.
Try the following configuration:
<token><param-name>requiressl</param-name>.*${line.separator}<param-value>true</param-value></token> <value><param-name>requiressl</param-name>${line.separator}<param-value>false</param-value></value>Can this one be published in the Maven repo please? I don't want to explain to my clients how to install it manually.
Rustam, Are you referring to this plugin being in maven central repo? If so, it already is.
I have a problem with the phases and the plugin: a) Using prepare-package I get an "file doesnt exist" error (because the target folder doesnt exist when it tries to replace it). b) Using package phase, the plugin replaces the file but only in the target folder (and not in the war)
How can I solve this problem?
<plugin>
Check out the UsageWithOtherPlugins page.
It would be great if you could support "back references" when doing regex replacements.
Pop a enhancement issue in if you really want it :-).
Hello great tool, is it possible to replace HTML with it I would like to replace
with something like: <script type="text/javascript" charset="utf-8" src="Scripts/both-min.js"></script>
How can you escape special characters in the pom ?
Found a solution to my problem with regexp. I modified original html to:
and inserted following configuration in the pom:
Hope it helps
Hi
<token>file:/${basedir}/src/main/resources</token> ${basedir} is changed to file:/D:\Project\test\....
Because of backslashesh like \P ,etc , i am getting error. java.util.regex.PatternSyntaxException?: Unknown character property name {r} near index 10 file:/D:\Project\
I want replace backslash to forward shlashes( \ to /) or ignore escape characters.
I tried using <unescape>true</unescape> ,but not successful. Please add your valuable inputs to resolve this.
Try: <regex>false</regex>
Hi,
How do I replace <entry key="buildCount">6</entry> with <entry key="buildCount">$build.number}</entry>
Hi Sabharwa,
The cleanest way would be to change the 6 to something like: @BUILD_COUNT@ in your source and use: <token>@BUILD_COUNT@</token> and <value>${build.number}<value>. This will keep the surrounding entry element and only change the token.
It would be great if you could provide pseudo-variables for the file name and line number in the replacement text.
Hi grkuntzmd, Would you liken this to Issue28 ?
Hi, I have a question: is it possible to reuse regexp groups in here?
Example: I want to replace image names by its html objects ie. IMG=someName.jpg -> <img src="someName.jpg"/> it means, I would use regex group:
<token>IMG=(.+)\.jpg</token> <value><img src=\"(0).jpg\"></value>
This do not work for me :(
Marcel
Try the value: <value><img src=\"$1.jpg\"></value>
That's it! Thank you really much ;)
I use java servlets and it would be interesting concatenate all strings, that are separated by "+" in one. It allow to write nice formatted html code inside method parameter.
Hi nikolay, I dont quite understand what you're trying to do. Do you mean you have some text in a java class which has +'s in it you wish to remove at build time?
Hi, how can I use BACK-REFERENCES?
I got it - $1, not \1.
Yes, that's right. This plugin uses Java's backreference syntax.
Hi, I'm having some issues when trying to use the regex functionality. Whenever I have <regex>true</regex> I get an weird error : "No group 7". Has anyone else stumbled into this ?
Thanks!
Hi geana.adrian, That means your pattern (or token) does not match a 7th group. Can you tell me what your token and value are? You might have more regex references than groups in your token.
Hi, thanks for the quick response! I'm not sure what you mean by 'groups'. I'm trying something similar to what a user commented earlier on this page. I managed to implement my behavior without using regex (using tokenFile and valueFile) but it would be much easier if I manage to get arround this error.
Here's the configuration I'm trying :
Thanks !
Looks like your token has regex characters ("[" and "]"). Either escape them or replace them with dots (".").
I am trying to change the <version> element in multiple pom.xml files in my project. I want to append "-SNAPSHOT" to the existing version.
I want to use xpath since I only want to change the <version> for the pom itself, not any of the <dependency> versions or any <parent> versions.
I have tried the following, but I get XML errors about content before the prolog. For the <xpath> I have tried /project/version, /project/version/text(). Any help is appreciated.
<configuration>
</configuration>