3

我正在尝试使用以下内容检查 ant 中是否存在属性:

<target name="test">
    <property name="testproperty" value="1234567890"/>
    <if>
        <isset property="testproperty"/>
        <then>
            <echo message="testproperty exists"/>
        </then>
        <else>
            <echo message="testproperty does not exist"/>
        </else>
    </if>
</target>

结果是消息失败:

build.xml:536: Problem: failed to create task or type if
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.

我一定对isset做错了,因为以下运行顺利:

<target name="test">
    <property name="testproperty" value="1234567890"/>
    <echo message="'${testproperty}'"/>
</target>

请指教 :)

4

3 回答 3

9

if 任务不是标准的 ANT 任务。这就是目标的条件执行的工作原理

<project name="demo" default="test">

  <target name="property-exists" if="testproperty">
    <echo message="testproperty exists"/>
  </target>

  <target name="property-no-exists" unless="testproperty">
    <echo message="testproperty does not exist"/>
  </target>

  <target name="test" depends="property-exists,property-no-exists">
  </target>

</project>
于 2013-06-08T20:41:50.040 回答
7

显然我错过了可以在这里找到的 ant-contrib jar 文件:http:
//ant-contrib.sourceforge.net/

于 2013-06-08T11:04:49.557 回答
3

您已经找到了丢失的 jar,现在您需要定义使用该 jar 的任务。我会将 jar 放在antlib/antcontrib项目内的目录下。这样,下载您的项目的其他人将拥有所需的 jar。

<taskdef resource="net/sf/antcontrib/antlib.xml">
    <classpath>
        <fileset dir="${basedir}/antlib/antcontrib"/>
    </classpath>
</taskdef>

如果你使用很多可选的 jar 文件,你可能想要使用命名空间:

 <project name="..." basedir="."  default="...."
    xmlns:ac="antlib://net/sf/antcontrib">

    <taskdef resource="net/sf/antcontrib/antlib.xml"
         uri="antlib://net/sf/antcontrib">
        <classpath>
            <fileset dir="${basedir}/antlib/antcontrib"/>
        </classpath>
    </taskdef>

现在,当你使用 antcontrib 任务时,你必须在它前面加上ac:. 这允许在没有命名空间冲突的情况下使用可选的 jar。

<target name="test">
    <property name="testproperty" value="1234567890"/>
    <ac:if>
        <isset property="testproperty"/>
        <then>
            <echo message="testproperty exists"/>
        </then>
        <else>
            <echo message="testproperty does not exist"/>
        </else>
    </ac:if>
</target>
于 2013-06-09T03:34:03.153 回答