问题:
我正在使用Maven 2.2.1来构建模块化Web应用程序。除此之外,它还调用 maven-antrun-plugin 来执行属性文件中的多次替换。
当启动具有目标的Maven时,这将失败 clean,install
.生成失败,并显示非法参数异常:非法组引用。
据我了解,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="REDACTEDmvntargetclasses/properties/log/log4j.properties" match="(log4j.appender.infoAppender.File.*=).*$"/>... @ 6:232 in REDACTEDmvntargetantrunbuild-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="REDACTEDmvntargetclasses/properties/log/log4j.properties" match="(log4j.appender.infoAppender.File.*=).*$"/>... @ 6:232 in REDACTEDmvntargetantrunbuild-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="REDACTEDmvntargetclasses/properties/log/log4j.properties" match="(log4j.appender.infoAppender.File.*=).*$"/>... @ 6:232 in REDACTEDNAEMetiermvntargetantrunbuild-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: REDACTEDmvntargetantrunbuild-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
从Matcher
源代码来看,只有一种方法可以重现错误"非法组引用":
"test".replaceAll("test", "$a"); // Only 0-9 is a valid group reference
我猜你在env.properties
里有一个美元符号,用于财产log4j.appender.infoAppender.File
.它需要被转义。