Maven substitute: replacement cost containing dollar signs

I am dealing with a Maven script where I need to change the contents of a file. I am currently using the replacer plugin, which gives me problems when the replacement value contains dollar signs.

My replacement problems are relatively simple: in my log4j.xml, replace the line <param name="File" value="wat.log" /> with <param name="File" value="${FOO_BAR}/wat.log" />

I know that it is written that Maven interprets ${FOO_BAR} as a property. I looked at the solution and tried. When I just use ${FOO}

 <properties> <dollar>$</dollar> <foo>{FOO_BAR}</foo> <dollar.foo>${dollar}${foo}</dollar.foo> </properties> <build> <plugins> <plugin> <groupId>com.google.code.maven-replacer-plugin</groupId> <artifactId>replacer</artifactId> <version>1.5.3</version> <executions> <execution> <id>configure-logging</id> <phase>prepare-package</phase> <goals> <goal>replace</goal> </goals> <configuration> <includes> <include>${my.configDir}/log4j.xml</include> </includes> <replacements> <replacement> <token>value="wat.log"</token> <value>value="${dollar.foo}/wat.log"</value> </replacement> </replacements> </configuration> </execution> </executions> </plugin> </plugins> </build> 

As a result, a named capturing group is missing trailing '}' error occurs. As I understand it, the plugin uses the usual replacement of regular Java code, which interprets dollar signs and curls in the replacement text to capture groups in the regular expression.

I tried several other things, and it seems that the specific error in this case is due to the underscore. If I change the foo property to {FOOBAR} , the error will change: No group with name {FOOBAR} .

I tried other things too:

  • changing the foo property to {foo} , I do not get an error message, but the replacement falls to $ , ie I get value="{foo}/wat.log" - the second substitution of foo with FOO_BAR works, but I'm still missing the dollar sign.
  • changing the property to {dollar} gives me an Illegal group reference error
  • avoiding the dollar, snapping and / or underlining in several different ways (for example, $$ , \$ , \\$ ) did not lead me further, did not use unicode or $ for the dollar sign.

Is there any way to solve this with Maven? I would be glad to use more properties, more replacements or a completely different plugin.

Update: I am working on Windows - not sure what / how this affects the results.

+6
source share
2 answers

I myself found the answer, looking for another backslash problem: Add <regex>false</regex> to the replacer plugin configuration, then the placeholder will simply replace the plain text that it sees, and the trick ${dollar.foo} works by destination:

 <properties> <dollar>$</dollar> <foo>{FOO_BAR}</foo> <dollar.foo>${dollar}${foo}</dollar.foo> </properties> ... <configuration> <includes> <include>${my.configDir}/log4j.xml</include> </includes> <!-- don't treat token/value as regular expressions --> <regex>false</regex> <replacements> <replacement> <token>value="wat.log"</token> <value>value="${dollar.foo}/wat.log"</value> </replacement> </replacements> </configuration> ... 
+1
source

You are very close. Just add the $ character with an escape slash:

 ... <replacements> <replacement> <token>value="wat.log"</token> <value>value="\${dollar.foo}/wat.log"</value> </replacement> </replacements> ... 

log4j.xml, up to:

 <param name="File" value="wat.log" /> 

log4j.xml, after:

 <param name="File" value="${FOO_BAR}/wat.log" /> 

It worked for me.

+7
source

All Articles