Ant conditional if within a macrodef

爷,独闯天下 提交于 2019-12-19 05:22:07

问题


Within ant, I have a macrodef.

Assuming I have to use this macrodef, and there is a item inside said macrodef that I want to run if the property special.property exists and is true, what do I do?

I currently have

<macrodef name="someName">
    <sequential>
        <someMacroDefThatSetsTheProerty  />
        <some:thingHereThatDependsOn if="special.property" />
    <sequential>
</macrodef>

Which doesn't work - the some:thingHereThatDependsOn doesnt have an "if" attribute, and I cannot add one to it.

antcontrib is not available.

With a target I can give the target an "if", what can I do with a macrodef?


回答1:


In Ant 1.9.1 and higher, there is now a new implementation of if and unless attributes. This might be what you're thinking of.

First, you need to put them into your namespace. Add them to your <project> header:

<project name="myproject" basedir="." default="package"
    xmlns:if="ant:if"
    xmlns:unless="ant:unless">

Now, you can add them to almost any Ant task or sub entity:

<!-- Copy over files from special directory, but only if it exists -->
<available property="special.dir.available"
    file="${special.dir} type="dir"/>

<copy todir="${target.dir}>
    <fileset dir="${special.dir}" if:true="special.dir.available"/>
    <fileset dir="${other.dir}"/>
</copy>

<!-- FTP files over to host, but only if it's on line-->
<condition property="ftp.available">
    <isreachable host="${ftp.host}"/>
</condition>

<ftp server="${ftp.host}" 
    userid="${userid}"
    passowrd="${password}"
    if:true="ftp.available">
    <fileset dir=".../>
</ftp>



回答2:


This is only possible if the ANT "thingHereThatDependsOn" task supports an "if" attribute.

As stated above, conditional execution in ANT, normally, only applies to targets.

<target name="doSomething" if="allowed.to.do.something">
   ..
   ..
</target>

<target name="doSomethingElse" unless="allowed.to.do.something">
   ..
   ..
</target>

<target name="go" depends="doSomething,doSomethingElse"/>


来源:https://stackoverflow.com/questions/18206124/ant-conditional-if-within-a-macrodef

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!