You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

maven-replacer-plugin在Java11中抛出Illegal group reference错误求助

Maven Replacer Plugin在Java 11中替换Swagger.json失败:Illegal group reference

我之前也踩过类似的坑,Java 8下跑的好好的replacer配置,到Java 11直接报错,结合你的情况,咱们一步步拆解问题和解决方案:

问题重现

你在Java 8项目中用maven-replacer-plugin替换swagger.json里的"paths" : {内容完全正常,但迁移到Java 11后,出现Illegal group reference错误;如果直接用"paths" : {作为token,又完全不生效。

你的插件配置:

<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
...
Caused by: java.lang.IllegalArgumentException: Illegal group reference
    at java.base/java.util.regex.Matcher.appendExpandedReplacement(Matcher.java:1068)

问题原因

  1. 正则表达式错误:你原来的token"paths"+\ \:\ \{逻辑有问题,+是匹配前面的字符(这里是双引号)多次,会匹配类似"pathssss..."的内容,根本没法准确命中"paths" : {;而直接用"paths" : {不生效,是因为XML里的特殊字符(比如冒号、大括号)没转义,正则引擎识别不了。
  2. Java 11正则替换逻辑更严格:Java 11的Matcher对替换内容里的$处理更苛刻,如果你的tokenEndpointsPaths.json里有$符号(比如Swagger路径参数/$token),会被当成正则分组引用,但如果$后面不是合法的分组编号(比如$1),就会抛出Illegal group reference异常。

解决方案

修正正则表达式,同时开启替换内容的转义:

<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>
        <!-- 修正后的正则:匹配"paths",允许任意空白字符,然后是冒号、任意空白字符、左大括号 -->
        <token>"paths"\\s*:\\s*\\{</token>
        <valueFile>${project.basedir}/src/main/resources/swagger/tokenEndpointsPaths.json</valueFile>
        <!-- 开启转义,自动把替换内容里的$转成$$,避免分组引用错误 -->
        <escape>true</escape>
      </replacement>
    </configuration>
</plugin>

关键说明:

  • 正则修正:\\s*匹配任意数量的空白字符(空格、制表符等),不管"paths"和:之间、:和{之间有多少空格,都能准确匹配目标内容;XML里需要把反斜杠写成\\,所以正则里的\s要转成\\s。
  • 开启转义:<escape>true</escape>会让插件自动处理替换内容里的$,把单个$转成$$,这样Java 11的Matcher就不会把它当成非法的分组引用了。

如果还是有问题,检查下tokenEndpointsPaths.json里的内容,有没有其他特殊正则字符(比如\),确保内容格式正确。

内容的提问来源于stack exchange,提问作者user9458184

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 03:52:30