Reputation: 45451
I have an ant property which has value of the type 1.0.0.123
I want to extract the value after the last dot, in this case that would be '123'. Which ant task should i use and how?
Upvotes: 7
Views: 28297
Reputation: 5141
If not wanting to use external libs nor scripting, I found in an answer to a similar question the best option (credit him for this answer). Here you'll be using a ReplaceRegex:
<loadresource property="index">
<propertyresource name="build.number"/>
<filterchain>
<tokenfilter>
<filetokenizer/>
<replaceregex pattern=".*\.)" replace="" />
</tokenfilter>
</filterchain>
</loadresource>
(I have used the same variable names as you in your solution. Of course, this is still missing the increment part of your answer, but that was not in your question.)
This script loads in index
the result of deleting the regex .*\.)
from build.number
, that is, if build.number = 1.0.0.123
then index = 123
.
build.xml:
<project name="ParseBuildNumber" default="parse" basedir=".">
<property name="build.number" value="1.0.0.123"/>
<target name="parse">
<loadresource property="index">
<propertyresource name="build.number"/>
<filterchain>
<tokenfilter>
<filetokenizer/>
<replaceregex pattern=".*\." replace="" />
</tokenfilter>
</filterchain>
</loadresource>
<echo message="build.number=${build.number}; index=${index}"/>
</target>
</project>
$ ant
Buildfile: /tmp/build.xml
parse:
[echo] build.number=1.0.0.123; index=123
BUILD SUCCESSFUL
Total time: 0 seconds
Upvotes: 13
Reputation: 581
I would also use the ant-contrib PropertyRegex task. The following snippet only works if the input string follows the convention you used in your description and you can also use it to extract the other numbers by changing the value entered in the select tag.
<propertyregex property="output"
input="input"
regexp="(\d)\.(\d)\.(\d)\.(\d{3})"
select="\4" />
You can also use the issest task on the output string to print out an error message if the input string does not follow the convention, because the output property will not be set.
Upvotes: 1
Reputation: 91
<propertyregex property="xtractedvalue"
input="${foobar}"
regexp="(.*)\.(.*)$"
select="\2" />
Upvotes: 7
Reputation: 10377
Here's a solution using flaka without scripting =
<project xmlns:fl="antlib:it.haefelinger.flaka">
<property name="foobar" value="1.0.0.123"/>
<target name="main">
<!-- simple echo -->
<fl:echo>xtractedvalue => #{split('${foobar}','\.')[3]}</fl:echo>
<!-- create property for further processing.. -->
<fl:let>
xtractedvalue := split('${foobar}','\.')[3]
</fl:let>
<echo>$${xtractedvalue} => ${xtractedvalue}</echo>
</target>
</project>
Upvotes: 1
Reputation: 45451
Ok i have found the answer myself and this is tested. you just gotta use a bit of javascript.
<target name="get build ctr">
<script language="javascript">
<![CDATA[
// getting the value
buildnumber = myproj.getProperty("build.number");
index = buildnumber.lastIndexOf(".");
counter = buildnumber.substring(index+1);
myproj.setProperty("buildctr",counter);
]]>
</script>
</target>
Upvotes: 9
Reputation: 1500155
I suspect the easiest approach may be to use the ant-contrib PropertyRegex task.
Something like this - completely untested:
<propertyregex property="output"
input="input"
regexp=".*([^\.]*)"
select="\1" />
Upvotes: 11