I have a property that contains multiple values, and I want to execute a command with a separate "-j" argument for each value in the property.
E.g. <property name="arguments" value="foo bar hello world"/>
Should execute: mycommand -j foo -j bar -j hello -j world
I'm using Ant 1.7.1, so I can't use the "prefix" attribute (Ant 1.8) on the <arg>
element of an <exec>
task.
One workaround is to insert the "-j" directly into the property by hand and then use the "line" attribute of <arg>
:
<property name="args" value="-j foo -j bar -j hello -j world"/>
<exec executable="mycommand">
<arg line="${args}"/>
</exec>
...But I prefer to have the property be a simple list without the embedded arguments.
Edit: Actually, my arguments are paths within an XML file, so a more accurate argument list would be:
<property name="arguments" value="/foo/bar /hello/world /a/very/long/path"/>
I would like the command to then execute with arguments: "-j /foo/bar -j /hello/world -j /a/very/long/path". Note that the slashes remain forward slashes even under Windows (these are arguments to a command, not filenames).
You can use Ant resource tools for this.
The above will result in property
arguments
having the value-j foo -j bar -j hello -j world
, which can then be used in theexec
arg
line.Alternatively a
pathconvert
task can help in this regard:If you have absolute paths, rather than just strings in the list, then remove the
flattenmapper
.If you have relative paths, replace the
flattenmapper
line with:to prevent the paths being converted to absolute.
In the event that you have UNIX-like paths in the arg_list on a Windows system the default settings for pathconvert won't work - the paths get converted to Windows style. Instead, to process the list use:
Note the
targetos
setting and the revised regexmapper from argument.Not sure your comfort with scripting languages, but you can also embed script to do the parsing/reassembling:
http://ant.apache.org/manual/Tasks/script.html
I will say, I think it's a bit fragile because you have an explicit reference to the ANT project's name, but up to you:
Use some for loop, here's a solution based on Ant Addon Flaka :