需要使用regex解析系统属性,以便从值中删除点。
执行示例是: mvn安装-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>中的美元符号之后出现
这些问题是:
<failIfNoMatch>false</failIfNoMatch>,但参数'value‘仍然丢失或不正确。如有任何反馈,将不胜感激。
提前谢谢你
发布于 2013-07-09 06:34:59
经过几次测试,我得出以下结论:
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] ------------------------------------------------------------------------
...请注意,最后两个错误不同--一个是缺失属性,另一个是不匹配的regexp。
因此,简单地说,我认为build-helper-maven-plugin如预期的那样工作。
https://stackoverflow.com/questions/17516313
复制相似问题