是否有一种方法可以有条件地将输出重定向到属性或标准输出流,如下面的非工作示例所示?
<macrodef name="mytask">
<attribute name="output" default="STDOUT"/>
<sequential>
<exec executable="my.exe" outputproperty="@{output}"/>
</sequential>
</macrodef>
上面的示例默认将输出重定向到属性STDOUT
。相反,我希望它被定向到标准输出流。
我可以创建 mytask_with_stdout 作为上述宏的副本,并删除 exec 输出属性,但这会违反 DRY 原则。
有什么好的方法可以做到这一点吗?
最佳答案
您可以将两个 Ant 功能组合起来以获得您想要的东西。
首先,一个<macrodef>
可以传递任何<element>
你想要的。
第二,一个<redirector>
可用于捕获 <exec>
的输出属性中的命令。
我在 Windows 计算机上运行了以下 Ant 脚本,以便可以使用 cmd.exe 的 echo 命令。将 cmd.exe 替换为您的 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>
关于ant - 有条件地重定向 ant exec 任务中的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9618051/