maven-replacer-plugin throws [ERROR] Illegal group reference

1k Views Asked by At

I use maven replacer plugin to replace some of the json in swagger.json file. This configuration works on other project, done in java 8. This project is in java 11 and the same configuration, with same regex, doesn't work. Regex needs to match "paths" : {.

<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>${project.build.directory}/${project.artifactId}-${project.version}/public/docs/swagger.json</file>
                <replacements>
                    <replacement>
                        <token>"paths"+\ \:\ \{</token>
                        <valueFile>${project.basedir}/src/main/resources/swagger/tokenEndpointsPaths.json
                        </valueFile>
                    </replacement>
            </configuration>
        </plugin>

This is error log:


[ERROR] Failed to execute goal com.google.code.maven-replacer-plugin:replacer:1.5.3:replace (default) on project emisiawire-fire-spring-webmvc: Illegal group reference -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal com.google.code.maven-replacer-plugin:replacer:1.5.3:replace (default) on project emisiawire-fire-spring-webmvc: Illegal group reference
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
        at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException: Illegal group reference
        at com.google.code.maven_replacer_plugin.ReplacerMojo.execute(ReplacerMojo.java:401)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
        ... 20 more
Caused by: java.lang.IllegalArgumentException: Illegal group reference
        at java.base/java.util.regex.Matcher.appendExpandedReplacement(Matcher.java:1068)
        at java.base/java.util.regex.Matcher.appendReplacement(Matcher.java:998)
        at java.base/java.util.regex.Matcher.replaceAll(Matcher.java:1182)
        at com.google.code.maven_replacer_plugin.TokenReplacer.replaceRegex(TokenReplacer.java:24)
        at com.google.code.maven_replacer_plugin.TokenReplacer.replace(TokenReplacer.java:11)
        at com.google.code.maven_replacer_plugin.ReplacementProcessor.replaceContent(ReplacementProcessor.java:35)
        at com.google.code.maven_replacer_plugin.ReplacementProcessor.replace(ReplacementProcessor.java:23)
        at com.google.code.maven_replacer_plugin.ReplacerMojo.replaceContents(ReplacerMojo.java:454)
        at com.google.code.maven_replacer_plugin.ReplacerMojo.execute(ReplacerMojo.java:391)
        ... 22 more 

When I use only "paths" : { inside token tabs, then nothing gets replaced.

2

There are 2 best solutions below

0
AudioBubble On BEST ANSWER

I found and solved the problem. Regex inside tags was okay (at least regex in example from my question, I haven't tried the other variations after I fixed the problem, probably all of them works). The problem was $ sign inside file I was using for replacement (in my example tokenEndpointsPaths.json contained $ inside). I just used \ to escape char $ and that solved it.

3
guest On

you can replace the regex like so :

<token>"paths"+\ \:\ \{</token>
              ^  ^   ^             // you have anecassary characters here

By this one :

<token>%regex["paths" : \{]</token>

check the regex demo :

for more details check the documentation, how to use regex in pom :

  1. https://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

regex demo