Reputation: 1271
Is there a way of conditionally redirecting the output to property or the stdout stream as the non-working example below?
<macrodef name="mytask">
<attribute name="output" default="STDOUT"/>
<sequential>
<exec executable="my.exe" outputproperty="@{output}"/>
</sequential>
</macrodef>
The above example redirects the output by default to a property STDOUT
. Instead I would like it to be directed to the stdout stream.
I could create mytask_with_stdout as a copy of the above macro and remove the exec outputproperty, but that would violate the DRY principle.
Is there some nice way of doing this?
Upvotes: 2
Views: 2688
Reputation: 7051
There are two Ant features you can combine to get what you want.
First, a <macrodef>
can be passed whatever <element>
you want.
Second, a <redirector>
can be used to capture the output of an <exec>
command in a property.
I ran the following Ant script on a Windows machine so I could use cmd.exe's echo command. Replace the cmd.exe with your my.exe:
<project name="exec-redirector-example" default="run">
<macrodef name="mytask">
<attribute name="message"/>
<element name="myredirector" optional="true"/>
<sequential>
<exec executable="cmd.exe">
<arg value="/c"/>
<arg value="echo"/>
<arg value="@{message}"/>
<myredirector/>
</exec>
</sequential>
</macrodef>
<target name="run">
<!-- exec outputs to STDOUT by default -->
<mytask message="To STDOUT">
</mytask>
<!-- exec outputs to a property in this example -->
<mytask message="To property">
<myredirector>
<redirector outputproperty="my.property"/>
</myredirector>
</mytask>
<echo>${my.property}</echo>
</target>
</project>
Upvotes: 2