Reputation: 979
I have the following ant file:
<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>
For some reason, neither runNPMInstall
nor runNPMUpdate
are ever run, regardless of whether the node_modules
directory exists or not. Anyone have any idea what might be causing this? From all examples online at least one of the dependent tasks should be run.
EDIT: Changed:
<condition property="dirExists" value="true" else="false">
To:
<condition property="dirExists">
With the same result. Additional information is that I am using Ant 1.8.2
. Thanks for anyone's help!
EDIT 2: Updated code to reflect my latest tests. Output from the build for new code above is:
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
Edit 3: Output when running ant -debug -f build.xml
:
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
Upvotes: 0
Views: 2405
Reputation: 10377
Your runNPMInstall target will never run, because the dirExists property will always be set either true or false, because you used condition task with attribute else="false".
When using the old syntax (Ant Version < 1.8.0) , means just the plain propertyname if="propertyname"
the target is only activated if the property is defined - independent of its actual value. On the contrary unless="propertyname"
means the target is only activated if the property is not defined, but your property will always be set from condition task => true or false.
Since ant 1.8.0 you may use the new syntax if="${propertyname}
means the target is only activated if the property holds true | yes | on, respectively unless="${propertyname}"
means the target will only be activated if the property hold false | no | off, see ant manual if / unless
When using if / unless with ${propertyname} it should work as expected with Ant >= 1.8.0
Alternatively use the condition task without the else attribute to make it work with the old syntax :
The value to set the property to if the condition evaluates to false. By default the property will remain unset.
Also value="true"
is not needed, as :
The value to set the property to. Defaults to "true".
So that should be sufficient to make it work 'old style' :
<condition property="dirExists">
<available file="${directory}" type="dir"/>
</condition>
-- EDIT after comment --
When starting your ant file it will run the default target dir.check and nothing else happens. Use :<project name="admin" default="main" basedir=".">
and create a new target :<target name="main" depends="dir.check,runNPMInstall,runNPMUpdate"/>
Upvotes: 3