maven-replacer-plugin抛出[ERROR]非法组引用

时间:2019-02-19 13:27:02

标签: java regex maven maven-replacer-plugin

我使用maven replacer插件替换swagger.json文件中的某些json。此配置可在Java 8中完成的其他项目上工作。 该项目在Java 11中,并且具有相同的正则表达式的相同配置不起作用。正则表达式必须匹配"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>

这是错误日志:


[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 

当我在标记标签中仅使用"paths" : {时,什么也不会被替换。

2 个答案:

答案 0 :(得分:0)

您可以这样替换 regex

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

通过这个:

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

检查正则表达式演示:

有关更多详细信息,请查看文档,以及如何在pom中使用正则表达式:

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

regex demo

答案 1 :(得分:0)

我找到并解决了问题。标签内的正则表达式还可以(至少在我的问题中使用正则表达式为例,解决问题后我还没有尝试其他变体,可能它们都可以工作)。问题是我用来替换的文件中有$符号(在我的示例中tokenEndpointsPaths.json包含$内部)。我只是使用\来转义char $,就解决了。