[ACCEPTED]-In Maven, how can I dynamically build a property value at runtime?-properties
Mojo's Build-Helper Maven Plugin can help 13 you out here.
There are a number of goals 12 that can be used to help transform properties.
There 11 is
Probably regex-property is the one you want, but if 10 your version numbers conform to the "standards" the 9 other two might save you.
To use the regex-property goal 8 you would do something like
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>regex-property</id>
<goals>
<goal>regex-property</goal>
</goals>
<configuration>
<name>tag.version</name>
<value>${project.version}</value>
<regex>^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.(-SNAPSHOT)?$</regex>
<replacement>V$1_$2_$3_P$4</replacement>
<failIfNoMatch>true</failIfNoMatch>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
Note: my regex 7 might be slightly off so you should test 6 the above.
Note: The property value will 5 only be available for executions after the 4 phase that this execution is bound to. The 3 default phase that it is bound to is validate
but 2 if you are on a different lifecycle (e.g. the 1 site lifecycle) the value will not be available.
You can use maven build-helper plugin, in 4 particular its regex-property mojo. Take a look at usage examples (scroll 3 to Set a property by applying a regex replacement to a value section).
Basically you want something 2 like that in your pom to get myVersionTag
property inferred 1 from myValue
:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>regex-property</id>
<goals>
<goal>regex-property</goal>
</goals>
<configuration>
<name>myVersionTag</name>
<value>$\{myValue}</value>
<regex>(\d+)\.(\d+)\.(\d+)\.(\d+)</regex>
<replacement>V_$1_$2_$3_P$4</replacement>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
Ihor Kaharlichenko's answer is basically 10 correct except that it copies an error from 9 the Codehaus documentation. There should be no '\' between the 8 '$' and the '{'. The mojo works without it and doesn't work with it. Truly, with a basic 7 understanding of regex and Maven, I couldn't 6 see what the backslash was supposed to do 5 and indeed it's wrong.
Stephen Connolly's 4 answer correctly omits the backslash. Be 3 careful.
This error has proliferated throughout 2 SO and with Codehaus out of business will 1 probably never get fixed.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.