从现有maven属性生成新的maven属性

时间:2016-06-22 02:27:39

标签: maven

我们需要一个新属性scala.major.version,它是通过删除末尾的次要版本号从现有属性scala.version中提取的。

  

2.11.8 => 2.11

我尝试过的方法是使用build-helper-maven-plugin并使用\\.[\\d]+$应用regexPropertySettings正则表达式模式,如下所示:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.10</version>
    <executions>
        <execution>
            <id>regex-property</id>
            <goals>
                <goal>regex-property</goal>
            </goals>
            <configuration>
     <regexPropertySettings>
        <regexPropertySetting>
                <name>scala.major.version</name>
                <value>${scala.version}</value>
                <regex>\\.[\\d]+$</regex>
                <replacement></replacement>
                <failIfNoMatch>true</failIfNoMatch>
        </regexPropertySetting>
     </regexPropertySettings>
            </configuration>
        </execution>
    </executions>
</plugin>

这是调用

mvn -X -Dscalatest.version=2.2.6 -Dscala.version=2.11.8  
     -Dspark.version=2.0.0-SNAPSHOT -Djava.version=1.8 validate package

然而,似乎这项任务没有执行:我甚至没有看到任何替代发生。

那么实现属性生成的正确工具是什么?

2 个答案:

答案 0 :(得分:1)

您将regex-property目标与regex-properties目标混淆。第一个只替换单个值,而后者可以替换多个值(借助regexPropertySettings参数)。

此外,作为regex参数传递的正则表达式不需要进行Java转义,插件将执行此操作。因此,正确的正则表达式将是:

<regex>\.\d+$</regex>

将选择次要版本号(最后一个点后面的所有数字)。 regex参数将位于capturing group内,以便捕获的内容可以替换为已配置的替换。

使用regex-property

在您的情况下,您只对替换单个值感兴趣,因此regex-property是合适的。配置如下:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.10</version>
    <executions>
        <execution>
            <id>regex-property</id>
            <goals>
                <goal>regex-property</goal>
            </goals>
            <configuration>
                <name>scala.major.version</name>
                <value>${scala.version}</value>
                <regex>\.\d+$</regex>
                <replacement></replacement>
                <failIfNoMatch>true</failIfNoMatch>
            </configuration>
        </execution>
    </executions>
</plugin>

直接使用所需值配置执行。这将正确创建所需属性scala.major.version

使用regex-properties

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.10</version>
    <executions>
        <execution>
            <id>regex-properties</id>
            <goals>
                <goal>regex-properties</goal>
            </goals>
            <configuration>
                <regexPropertySettings>
                    <regexPropertySetting>
                        <name>scala.major.version</name>
                        <value>${scala.version}</value>
                        <regex>\.\d+$</regex>
                        <replacement></replacement>
                        <failIfNoMatch>true</failIfNoMatch>
                    </regexPropertySetting>
                </regexPropertySettings>
            </configuration>
        </execution>
    </executions>
</plugin>

这一次,由于此目标可以定位多个替换,因此需要在regexPropertySetting内定义每个替换。结果将是相同的。

答案 1 :(得分:0)

检查documentation表示您的插件配置不正确

您的命令行可以使用此pom.xml。至少在调试中你可以看到消息:

[DEBUG] define property scala.major.version = "2.11"

这是一个pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>stackoverflow</groupId>
    <artifactId>37957533</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.10</version>
                <executions>
                    <execution>
                        <id>regex-property</id>
                        <goals>
                            <goal>regex-property</goal>
                        </goals>
                        <configuration>
                            <name>scala.major.version</name>
                            <value>${scala.version}</value>
                            <regex>\.[\d]+$</regex>
                            <replacement></replacement>
                            <failIfNoMatch>true</failIfNoMatch>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>