使用build-helper-maven-plugin的目标“regex-property”解析属性时可能出现的错误?

时间:2013-07-07 20:57:40

标签: maven

要求是我必须使用正则表达式解析系统属性,以便从值中删除点。

执行示例是:mvn install -Dsomeversion = 1.3

pom.xml配置是:

<plugin>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>build-helper-maven-plugin</artifactId>
 <version>1.8</version>
 <executions>
  <execution>
   <id>regex-property</id>
   <goals>
    <goal>regex-property</goal>
   </goals>
   <configuration>
    <name>someversion.parsed</name>
    <value>$\{someversion}</value>
    <regex>(.*)[\._](.*)</regex>
    <replacement>$1$2</replacement>
    <failIfNoMatch>false</failIfNoMatch>
   </configuration>
  </execution>
 </executions>
</plugin>

根据插件的文档,在<value>

美元符号后,反斜杠必须在那里

问题是:

  1. 当反斜杠存在时,系统属性不会被解析
  2. 如果我删除反斜杠:
    a)系统属性被成功解析
    b)如果执行“mvn install”,我收到参数'value'的错误    尽管我已配置<failIfNoMatch>false</failIfNoMatch>
  3. ,但遗失或不正确

    任何反馈都将受到高度赞赏

    提前谢谢

1 个答案:

答案 0 :(得分:5)

经过一些测试后,我得出了以下结论:

  • 没有看到文件说'''应该跟'$'符号 (也许你可以指出它),所以我删除它:),无论如何通常通过 - '\'实现的逃避与下一个而不是前一个角色有关,据我所知
  • 您为mvn install看到的错误是配置的值 财产<failIfNoMatch>不相关

<failIfNoMatch>决定,如果系统属性存在但不是预期的格式,我是否会失败?它并不涵盖财产根本不存在的情况。然而,为此目的,在maven中存在所谓的简档,这些可以以不同的方式激活,其中之一是系统属性存在。

以下是我的工作:

<?xml version="1.0" encoding="UTF-8"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0       http://maven.apache.org/maven-v4_0_0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>4.0.0</modelVersion>
    <groupId>sample</groupId>
    <artifactId>sample</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <profiles>
        <profile>
            <activation>
                <property>
                    <name>someversion</name>
                </property>
            </activation>

            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>build-helper-maven-plugin</artifactId>
                        <version>1.8</version>
                        <executions>
                            <execution>
                                <id>regex-property</id>
                                <goals>
                                    <goal>regex-property</goal>
                                </goals>
                                <configuration>
                                    <name>someversion.parsed</name>
                                    <value>${someversion}</value>
                                    <regex>(.*)[\._](.*)</regex>
                                    <!-- <regex>notmatched</regex>-->
                                    <replacement>$1$2</replacement>
                                    <failIfNoMatch>false</failIfNoMatch>
                                    <!--<failIfNoMatch>true</failIfNoMatch>-->
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>1.7</version>
                        <executions>
                            <execution>
                                <phase>validate</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <tasks>
                                        <echo>******** Displaying value of property ********</echo>
                                        <echo>${someversion.parsed}</echo>
                                    </tasks>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>


</project>

请注意,maven-antrun-plugin只是为了显示调试输出。

现在进行一些测试:

mvn install -Dsomeversion=1.3
...
main:
     [echo] ******** Displaying value of property ********
     [echo] 13
[INFO] Executed tasks
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
...

对于没有系统属性版本:

mvn install
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
...

我的pom.xml中没有特殊的配置文件,我会得到以下输出:

mvn install
...
INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] One or more required plugin parameters are invalid/missing for 'build-helper:regex-property'

[0] Inside the definition for plugin 'build-helper-maven-plugin' specify the following:

<configuration>
  ...
  <value>VALUE</value>
</configuration>

-OR-

on the command line, specify: '-Dsomeversion=VALUE'
...

只是为了让事情完整,以防我在我的解决方案中使用(目前已注释掉)<regex>notmatched</regex>以及<failIfNoMatch>true</failIfNoMatch>

mvn install -Dsomeversion=1.3
...
[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - sample:sample:pom:1.0.0-SNAPSHOT
[INFO]    task-segment: [install]
[INFO] ------------------------------------------------------------------------
[INFO] [build-helper:regex-property {execution: regex-property}]
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] No match to regex 'notmatched' found in '1.3'.
[INFO] ------------------------------------------------------------------------
...

请注意,最后2个错误有所不同 - 一个是缺失属性,另一个是非匹配正则表达式。

总而言之,我相信build-helper-maven-plugin可以正常运作。