Is there a way to pull a substring from an Ant property and place that substring into it's own property?
-
Can you be more specific about what you're trying to do? Why wouldn't it make more sense to define a property to hold the value you're depending on and use it in multiple places? If your Ant properties are changing often enough that you need to programatically react to them, you may be doing something else wrong. – PanCrit Jun 03 '09 at 16:42
6 Answers
I use scriptdef to create a javascript tag to substring, for exemple:
<project>
<scriptdef name="substring" language="javascript">
<attribute name="text" />
<attribute name="start" />
<attribute name="end" />
<attribute name="property" />
<![CDATA[
var text = attributes.get("text");
var start = attributes.get("start");
var end = attributes.get("end") || text.length();
project.setProperty(attributes.get("property"), text.substring(start, end));
]]>
</scriptdef>
........
<target ...>
<substring text="asdfasdfasdf" start="2" end="10" property="subtext" />
<echo message="subtext = ${subtext}" />
</target>
</project>
You could try using PropertyRegex from Ant-Contrib.
<propertyregex property="destinationProperty"
input="${sourceProperty}"
regexp="regexToMatchSubstring"
select="\1"
casesensitive="false" />

- 54,264
- 27
- 148
- 161

- 14,825
- 5
- 34
- 41
-
Additional note: In case this is done dynamically in a loop, use `override="true"` to override any previous value. – robinst Oct 09 '13 at 15:08
Since I prefer to use vanilla Ant, I use a temporary file. Works everywhere and you can leverage replaceregex to get rid of the part of the string you don't want. Example for munging Git messages:
<exec executable="git" output="${git.describe.file}" errorproperty="git.error" failonerror="true">
<arg value="describe"/>
<arg value="--tags" />
<arg value="--abbrev=0" />
</exec>
<loadfile srcfile="${git.describe.file}" property="git.workspace.specification.version">
<filterchain>
<headfilter lines="1" skip="0"/>
<tokenfilter>
<replaceregex pattern="\.[0-9]+$" replace="" flags="gi"/>
</tokenfilter>
<striplinebreaks/>
</filterchain>
</loadfile>

- 101
- 1
- 2
-
5If you're dealing with a property to start with, you can eschew the file by doing something like
– Scott Jul 02 '13 at 15:49${property.here} -
I guess an easy vanilla way to do this is:
<loadresource property="destinationProperty">
<concat>${sourceProperty}</concat>
<filterchain>
<replaceregex pattern="regexToMatchSubstring" replace="\1" />
</filterchain>
</loadresource>

- 380
- 3
- 13
I would go with the brute force and write a custom Ant task:
public class SubstringTask extends Task {
public void execute() throws BuildException {
String input = getProject().getProperty("oldproperty");
String output = process(input);
getProject().setProperty("newproperty", output);
}
}
What's left it to implement the String process(String)
and add a couple of setters (e.g. for the oldproperty
and newproperty
values)

- 6,853
- 2
- 26
- 25
i would use script task for that purpose, i prefer ruby, example cut off the first 3 chars =
<project>
<property name="mystring" value="foobarfoobaz"/>
<target name="main">
<script language="ruby">
$project.setProperty 'mystring', $mystring[3..-1]
</script>
<echo>$${mystring} == ${mystring}</echo>
</target>
</project>
output =
main:
[echo] ${mystring} == barfoobaz
using the ant api with method project.setProperty() on an existing property will overwrite it, that way you can work around standard ant behaviour, means properties once set are immutable

- 10,307
- 2
- 38
- 66
-
Hi, i got this error: Unable to create javax script engine for ruby – Jarod Law Ding Yong Aug 09 '11 at 09:02
-
Jarod, you need bsf.jar (http://jakarta.apache.org/bsf) and jruby.jar (http://www.jruby.org), see also http://ant.apache.org/manual/install.html#librarydependencies, note that the link to jruby is wrong on that page. – Rebse Aug 14 '11 at 18:05