使用maven-antrun-plugin和replaceregexp时非法引用组

时间:2013-06-17 14:41:52

标签: regex maven ant illegalargumentexception maven-antrun-plugin

问题:

我正在使用 Maven 2.2.1 来构建模块化网络应用。 除此之外,它还要求 maven-antrun-plugin 在属性文件中执行多次替换。

启动具有目标clean,install的Maven时失败。构建失败,出现 IllegalArgumentException:非法组引用

根据我的理解,String.replace()Matcher的各种方法都会发生此异常,并且它指向其中一个参数的解析错误。

有人能指出这段代码中的缺陷吗?

注意:我不允许为此任务切换到 maven-replacer maven-ressources

替换pom.xml中的任务:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.7</version>
        <executions>
            <execution>
            <id>Properties</id>
            <phase>process-resources</phase>
        <configuration>
            <tasks>
            <echo message="Switching properties"/>
                <property file="${project.build.outputDirectory}/env.properties"/>
                    <replaceregexp file="${project.build.outputDirectory}/properties/log/log4j.properties"          match="(log4j\.appender\.infoAppender\.File.*=).*$"     replace="\1${log4j.appender.infoAppender.File}"     byline="true"/>
                    <replaceregexp file="${project.build.outputDirectory}/properties/log/log4j.properties"          match="(log4j\.appender\.batchAppender\.File.*=).*$"    replace="\1${log4j.appender.batchAppender.File}"    byline="true"/>
                    <replaceregexp file="${project.build.outputDirectory}/properties/log/log4j.properties"          match="(log4j\.appender\.errorAppender\.File.*=).*$"    replace="\1${log4j.appender.errorAppender.File}"    byline="true"/>
                    <replaceregexp file="${project.build.outputDirectory}/properties/log/log4j.properties"          match="(log4j\.appender\.debugAppender\.File.*=).*$"    replace="\1${log4j.appender.debugAppender.File}"    byline="true"/>
                    <replaceregexp file="${project.build.outputDirectory}/properties/log/log4j.properties"          match="(log4j\.appender\.secuAppender\.File.*=).*$"     replace="\1${log4j.appender.secuAppender.File}"     byline="true"/>
                    <replaceregexp file="${project.build.outputDirectory}/properties/log/log4j.properties"          match="(log4j\.appender\.perfAppender\.File.*=).*$"     replace="\1${log4j.appender.perfAppender.File}"     byline="true"/>
                    <replaceregexp file="${project.build.outputDirectory}/properties/log/log4j.properties"          match="(log4j\.category\.error.*=).*$"                  replace="\1${log4j.category.error}"                 byline="true"/>
                    <replaceregexp file="${project.build.outputDirectory}/properties/log/log4j.properties"          match="(log4j\.category\.debug.*=).*$"                  replace="\1${log4j.category.debug}"                 byline="true"/>
                    <replaceregexp file="${project.build.outputDirectory}/properties/log/log4j.properties"          match="(log4j\.rootLogger.*=).*$"                       replace="\1${log4j.rootLogger}"                     byline="true"/>
                    <replaceregexp file="${project.build.outputDirectory}/properties/log/log4j.properties"          match="(log4j\.logger\.org\.hibernate\.SQL.*=).*$"      replace="\1${log4j.logger.org.hibernate.SQL}"       byline="true"/>
                    <replaceregexp file="${project.build.outputDirectory}/properties/projet.properties"             match="(blah\.ihm\.pleinEcran\.actif.*=).*$"                replace="\1${blah.ihm.pleinEcran.actif}"                byline="true"/>
                    <replaceregexp file="${project.build.outputDirectory}/properties/projet.properties"             match="(blah\.version.*=).*$"                           replace="\1${blah.version}"                         byline="true"/>
                    <replaceregexp file="${project.build.outputDirectory}/properties/projet.properties"             match="(blah\.developpement.*=).*$"                     replace="\1${blah.developpement}"                   byline="true"/>
                    <replaceregexp file="${project.build.outputDirectory}/properties/infrastructure.properties"     match="(erreur\.technique\.mode\.prod.*=).*$"           replace="\1${erreur.technique.mode.prod}"           byline="true"/>
                </tasks>
              </configuration>
            <goals>
                    <goal>run</goal>
                </goals>
            </execution>
            <execution>
                <id>Test-properties</id>
                <phase>process-test-resources</phase>
                <configuration>
                    <tasks>
                        <echo message="Replacing in test properties"/>
                            <property file="${project.build.testOutputDirectory}/env.properties"/>
                                <replaceregexp
                                    file="${project.build.testOutputDirectory}/jndi.properties"
                                    match="(org\.osjava\.sj\.root.*=).*$"
                                    replace="\1${org.osjava.sj.root}"
                                    byline="true"/>
                                <copy overwrite="true" file="${project.build.testSourceDirectory}/hibernate.cfg.ic.xml" tofile="${project.build.testOutputDirectory}/hibernate.cfg.test.xml"/>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>ant</groupId>
                        <artifactId>ant-optional</artifactId>
                        <version>1.5.3-1</version>
                    </dependency>
                    <dependency>
                        <groupId>ant</groupId>
                        <artifactId>ant-trax</artifactId>
                        <version>1.6</version>
                    </dependency>
                </dependencies>
            </plugin>

输出:

[WARNING] DEPRECATED [tasks]: Use target instead
[INFO] [antrun:run {execution: Properties}]
[WARNING] Parameter tasks is deprecated, use target instead
[INFO] Executing tasks

main:
     [echo] Switching properties
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] An Ant BuildException has occured: java.lang.IllegalArgumentException: Illegal group reference
around Ant part ...<replaceregexp replace="\1${log4j.appender.infoAppender.File}" byline="true" file="REDACTED\mvntarget\classes/properties/log/log4j.properties" match="(log4j\.appender\.infoAppender\.File.*=).*$"/>... @ 6:232 in REDACTED\mvntarget\antrun\build-main.xml

[INFO] ------------------------------------------------------------------------
[INFO] Trace
org.apache.maven.lifecycle.LifecycleExecutionException: An Ant BuildException has occured: java.lang.IllegalArgumentException: Illegal group reference
around Ant part ...<replaceregexp replace="\1${log4j.appender.infoAppender.File}" byline="true" file="REDACTED\mvntarget\classes/properties/log/log4j.properties" match="(log4j\.appender\.infoAppender\.File.*=).*$"/>... @ 6:232 in REDACTED\mvntarget\antrun\build-main.xml
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:719)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifecycleExecutor.java:556)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:535)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
        at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
        at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
        at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
        at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
Caused by: org.apache.maven.plugin.MojoExecutionException: An Ant BuildException has occured: java.lang.IllegalArgumentException: Illegal group reference
around Ant part ...<replaceregexp replace="\1${log4j.appender.infoAppender.File}" byline="true" file="REDACTED\mvntarget\classes/properties/log/log4j.properties" match="(log4j\.appender\.infoAppender\.File.*=).*$"/>... @ 6:232 in REDACTED\NAEMetier\mvntarget\antrun\build-main.xml
        at org.apache.maven.plugin.antrun.AntRunMojo.execute(AntRunMojo.java:355)
        at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
        ... 17 more
Caused by: REDACTED\mvntarget\antrun\build-main.xml:6: java.lang.IllegalArgumentException: Illegal group reference
        at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:116)
        at org.apache.tools.ant.Task.perform(Task.java:348)
        at org.apache.tools.ant.Target.execute(Target.java:390)
        at org.apache.tools.ant.Target.performTasks(Target.java:411)
        at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
        at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
        at org.apache.maven.plugin.antrun.AntRunMojo.execute(AntRunMojo.java:327)
        ... 19 more
Caused by: java.lang.IllegalArgumentException: Illegal group reference
        at java.util.regex.Matcher.appendReplacement(Matcher.java:713)
        at org.apache.tools.ant.util.regexp.Jdk14RegexpRegexp.substitute(Jdk14RegexpRegexp.java:116)
        at org.apache.tools.ant.taskdefs.optional.ReplaceRegExp.doReplace(ReplaceRegExp.java:289)
        at org.apache.tools.ant.taskdefs.optional.ReplaceRegExp.doReplace(ReplaceRegExp.java:357)
        at org.apache.tools.ant.taskdefs.optional.ReplaceRegExp.execute(ReplaceRegExp.java:491)
        at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
        ... 25 more

1 个答案:

答案 0 :(得分:0)

Matcher源代码看,只有一种方法可以重现错误“非法组引用”:

"test".replaceAll("test", "$a"); // Only 0-9 is a valid group reference

我猜你在env.properties财产log4j.appender.infoAppender.File中有一个美元符号。它需要逃脱。