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

mik*_*e.l 2 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>
Run Code Online (Sandbox Code Playgroud)

根据插件的文档,在美元登录后必须有反斜杠 <value>

问题是:

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

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

先感谢您

Pet*_*vic 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>
Run Code Online (Sandbox Code Playgroud)

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

现在一些测试:

mvn install -Dsomeversion=1.3
...
main:
     [echo] ******** Displaying value of property ********
     [echo] 13
[INFO] Executed tasks
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
...
Run Code Online (Sandbox Code Playgroud)

对于没有系统属性版本:

mvn install
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
...
Run Code Online (Sandbox Code Playgroud)

如果我的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'
...
Run Code Online (Sandbox Code Playgroud)

只是为了使事情完整,以防我在我的解决方案中使用(目前已注释掉)<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] ------------------------------------------------------------------------
...
Run Code Online (Sandbox Code Playgroud)

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

总而言之,我相信这是build-helper-maven-plugin有效的.