WestCoastProjects
WestCoastProjects

Reputation: 63201

Generate a new maven property from an existing one

We need a new property scala.major.version that is extracted from an existing property scala.version by removing the minor version number at the end E.g.

2.11.8 => 2.11

The approach that I have tried is to use the build-helper-maven-plugin and apply a \\.[\\d]+$ regex pattern using regexPropertySettings as follows:

<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>

Here is the invocation

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

However it seems that this task were not executed: I do not even see any substitution taking place.

So what is the correct tool to achieve the property generation?

Upvotes: 1

Views: 3303

Answers (2)

Yuri G.
Yuri G.

Reputation: 4648

checking the documentation shows that your plugin's configuration isn't correct

Your command line works with this pom.xml. At least in the debug you can see the message:

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

this is a 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>

Upvotes: 0

Tunaki
Tunaki

Reputation: 137219

You are confusing the regex-property goal with the regex-properties goal. The first will only replace a single value whereas the latter can replace multiple values (with the help of theregexPropertySettings parameter).

Also, the regular expression you pass as the regex parameter does not need to be Java-escaped, the plugin will do it. As such, the correct regex to pass will be:

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

which will select the minor version number (all digits after the last dot). The regex parameter will be inside a capturing group so that what is captured can be replaced by the configured replacement.

With regex-property

In your case, you are only interested in replacing a single value so regex-property is appropriate. The configuration would be:

<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>

The execution is directly configured with the wanted values. This will correctly create the wanted property scala.major.version.

With 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>

This time, since this goal can target multiple replacments, it is needed to define each of them inside a regexPropertySetting. The result will be the same.

Upvotes: 1

Related Questions