Dave A
Dave A

Reputation: 2830

"llegal group reference" when running Google maven replacer plugin

I’m using Maven 3.2.3. I have this plugin in my pom.xml file …

                    <!-- creates a test database script from the properties file -->
                    <plugin>
                            <groupId>com.google.code.maven-replacer-plugin</groupId>
                            <artifactId>replacer</artifactId>
                            <version>1.5.3</version>
                            <executions>
                                    <execution>
                                            <phase>compile</phase>
                                            <goals>
                                                    <goal>replace</goal>
                                            </goals>
                                    </execution>
                            </executions>
                            <configuration>
                                    <file>src/test/resources/META-INF/db-test-data.sql.templ</file>
                                    <outputFile>target/test-classes/db-test-data.sql</outputFile>
                                    <tokenValueMap>src/test/resources/test.properties</tokenValueMap>
                            </configuration>
                    </plugin>

I notice that when my “test.properties” file contains a property like so

test.sample.school2.name=Middle $ample Elementary #2

I get the below error when the plugin executes …

[ERROR] Failed to execute goal com.google.code.maven-replacer-plugin:replacer:1.5.3:replace (default) on project core: Illegal group reference -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.

The problem is with the “$” in the replaced value. If I remove it, everything is fine. However, sometimes the replaced value will have a “$”. Is there a way to configure the plugin to accept the “$”, or barring that, is there an equivalent plugin I can use that will achieve the same thing as above?

Upvotes: 1

Views: 1361

Answers (1)

mystarrocks
mystarrocks

Reputation: 4088

You should escape the $ token (and other such regex symbols):

test.sample.school2.name=Middle \$ample Elementary #2

Just so you know, the $ character is used to denote the matching groups in during the replacement in regex (hence the error message).

This has nothing to do with the escaping required on the keys in a property file when loading of course, but this plugin probably does a replaceAll for regex support.

Quoting its javadoc,

Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher.replaceAll. Use Matcher.quoteReplacement(java.lang.String) to suppress the special meaning of these characters, if desired.

but apparently, the replacer plugin's code doesn't use the quoteReplacement to escape an arbitrary replacement string (in this case, it's the string: "Middle $ample Elementary #2"), so you must escape it yourself.


If you don't intend to use regex matching/replacements at all, you could set the regex flag to false in the plugin configuration:

<configuration>
  <file>src/test/resources/META-INF/db-test-data.sql.templ</file>
  <outputFile>target/test-classes/db-test-data.sql</outputFile>
  <tokenValueMap>src/test/resources/test.properties</tokenValueMap>
  <regex>false</regex>
</configuration>

Upvotes: 1

Related Questions