首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当解析属性时可能会出现错误,其目标是构建帮助器-maven-plugin的目标"regex-property“?

当解析属性时可能会出现错误,其目标是构建帮助器-maven-plugin的目标"regex-property“?
EN

Stack Overflow用户
提问于 2013-07-07 20:57:39
回答 1查看 7.5K关注 0票数 3

需要使用regex解析系统属性,以便从值中删除点。

执行示例是: mvn安装-Dsomeversion=1.3

pom.xml配置是:

代码语言:javascript
复制
<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“,尽管我已经配置了<failIfNoMatch>false</failIfNoMatch>,但参数'value‘仍然丢失或不正确。

如有任何反馈,将不胜感激。

提前谢谢你

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-09 06:34:59

经过几次测试,我得出以下结论:

  • 没有看到说明'\‘应该跟在'$’符号后面的文档(也许您可以指给我看),所以我删除了它:),据我所知,无论如何,- '\‘通常是与下一个字符相关的,而不是以前的字符。
  • 对于您看到的mvn install的错误,是<failIfNoMatch>无关的配置属性的值

<failIfNoMatch>决定,如果系统属性存在但没有预期的格式,那么我应该失败吗?它没有涵盖财产根本不存在的情况。然而,出于这个目的,在maven中存在所谓的配置文件,可以以不同的方式激活它们,其中之一就是系统属性的存在。

因此,以下是我的工作:

代码语言:javascript
复制
<?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的存在只是为了显示调试输出。

现在有一些测试:

代码语言:javascript
复制
mvn install -Dsomeversion=1.3
...
main:
     [echo] ******** Displaying value of property ********
     [echo] 13
[INFO] Executed tasks
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
...

对于无系统属性版本:

代码语言:javascript
复制
mvn install
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
...

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

代码语言:javascript
复制
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>中使用

代码语言:javascript
复制
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] ------------------------------------------------------------------------
...

请注意,最后两个错误不同--一个是缺失属性,另一个是不匹配的regexp。

因此,简单地说,我认为build-helper-maven-plugin如预期的那样工作。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17516313

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档