Ant条件目标未运行

时间:2014-07-29 20:10:06

标签: ant conditional

我有以下ant文件:

<project name="admin" default="dirCheck" basedir=".">

    <property name="directory" location="node_modules" />

    <target name="run.npm.install" depends="dirCheck" unless="${dirExists}">
        <echo>${directory} does not exist. Running 'npm install'</echo>
        <exec executable="npm">
            <arg value="install" />
        </exec>
    </target>

    <target name="run.npm.update" depends="dirCheck" if="${dirExists}">
        <echo>${directory} exists. Running 'npm update'</echo>
        <exec executable="npm">
            <arg value="update" />
        </exec>
    </target>

    <target name="node_modules.check">
        <condition property="dirExists">
            <available file="${directory}" type="dir"/>
        </condition>
    </target>

    <target name="dirCheck" depends="node_modules.check">
        <echo>Checking for ${directory}. Exists? ${dirExists}</echo>
    </target>

</project>

出于某种原因,无论runNPMInstall目录是否存在,runNPMUpdatenode_modules都不会运行。任何人都知道这可能导致什么?从在线的所有示例中,至少应该运行一个相关任务。

编辑:已更改:

<condition property="dirExists" value="true" else="false">

要:

<condition property="dirExists">

结果相同。其他信息是我使用Ant 1.8.2。感谢任何人的帮助!

编辑2:更新了代码以反映我的最新测试。上面新代码的构建输出是:

Buildfile: /ws_html/app/client/build.xml

node_modules.check:

dirCheck:
     [echo] Checking for /ws_html/app/client/node_modules. Exists? true

BUILD SUCCESSFUL
Total time: 0 seconds
运行ant -debug -f build.xml

编辑3 输出:

jjung-m2:client-dir jjung$ ant -debug -f build.xml
Apache Ant(TM) version 1.8.2 compiled on June 16 2012
Buildfile: /ws_html/client-dir/app/client-dir/build.xml
Adding reference: ant.PropertyHelper
Detected Java version: 1.7 in: /Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre
Detected OS: Mac OS X
Adding reference: ant.ComponentHelper
Setting ro project property: ant.file -> /ws_html/client-dir/app/client-dir/build.xml
Setting ro project property: ant.file.type -> file
Adding reference: ant.projectHelper
Adding reference: ant.parsing.context
Adding reference: ant.targets
parsing buildfile /ws_html/client-dir/app/client-dir/build.xml with URI = file:/ws_html/client-dir/app/client-dir/build.xml
Setting ro project property: ant.project.name -> ClientApp
Adding reference: ClientApp
Setting ro project property: ant.project.default-target -> dirCheck
Setting ro project property: ant.file.ClientApp -> /ws_html/client-dir/app/client-dir/build.xml
Setting ro project property: ant.file.type.ClientApp -> file
Project base dir set to: /ws_html/client-dir/app/client-dir
 +Target: 
 +Target: run.npm.install
 +Target: run.npm.update
 +Target: node_modules.check
 +Target: dirCheck
Adding reference: ant.LocalProperties
parsing buildfile jar:file:/usr/share/ant/lib/ant.jar!/org/apache/tools/ant/antlib.xml with URI = jar:file:/usr/share/ant/lib/ant.jar!/org/apache/tools/ant/antlib.xml from a zip file
Setting project property: directory -> /ws_html/client-dir/app/client-dir/node_modules
Setting ro project property: ant.project.invoked-targets -> dirCheck
Attempting to create object of type org.apache.tools.ant.helper.DefaultExecutor
Adding reference: ant.executor
Build sequence for target(s) `dirCheck' is [node_modules.check, dirCheck]
Complete build sequence is [node_modules.check, dirCheck, run.npm.update, run.npm.install, ]

node_modules.check:
[available] Found directory: node_modules
Condition true; setting dirExists to true
Setting project property: dirExists -> true

dirCheck:
     [echo] Checking for /ws_html/client-dir/app/client-dir/node_modules. Exists? true

BUILD SUCCESSFUL
Total time: 0 seconds

1 个答案:

答案 0 :(得分:3)

您的runNPMInstall目标永远不会运行,因为dirExists属性将始终设置为true或false,因为您使用condition task属性else =“false”。
使用旧语法(Ant Version&lt; 1.8.0)时,仅表示普通属性名if="propertyname",只有在定义属性时才激活目标 - 与其实际值无关。相反,unless="propertyname"表示只有在未定义属性时才激活目标,但您的属性将始终从条件task =&gt;设置。是真是假 从ant 1.8.0开始,您可以使用新语法if="${propertyname}表示仅当属性保持为true时才激活目标是的|分别为unless="${propertyname}"表示仅当属性保持为false时才会激活目标没有|关,see ant manual if / unless

当使用if / unless with $ {propertyname}时,它应该按预期使用Ant&gt; = 1.8.0左右。或者使用不带else属性的条件任务使其使用旧语法:
< / p>

The value to set the property to if the condition evaluates to false. By default the property will remain unset.

也不需要value="true",如下:

The value to set the property to. Defaults to "true".

所以这应该足以使它成为'旧式':

<condition property="dirExists">
 <available file="${directory}" type="dir"/>
</condition>

- 评论后编辑 -

启动ant文件时,它将运行默认目标dir.check,并且不会发生任何其他情况。使用:
<project name="admin" default="main" basedir=".">
并创建一个新目标:
<target name="main" depends="dir.check,runNPMInstall,runNPMUpdate"/>

相关问题