Discussion:
Ant: file exists
Douglas Kramer
2005-01-29 20:40:21 UTC
Permalink
Is there a way to test whether a file exists?

I googled ant.apache.org for "file exists", looked at
conditional, etc to no avail.

-Doug
Erik Hatcher
2005-01-29 20:49:15 UTC
Permalink
Post by Douglas Kramer
Is there a way to test whether a file exists?
<available>
Douglas Kramer
2005-01-29 21:38:35 UTC
Permalink
Thanks. Okay, great so far...

<available file="${WORKDIR}/jar" property="jar.symlink.exists" />

but I also need to excute a statement if the property is true, something
like this:

if [ -f filename ]; then
echo "File exists";
fi

-Doug
Post by Erik Hatcher
Post by Douglas Kramer
Is there a way to test whether a file exists?
<available>
Douglas Kramer
2005-01-29 22:41:40 UTC
Permalink
For the curious, this is it. When calling the run task, a symlink
is created if it does not exist.


<target name="run" depends="makesymlink" />
<echo message="do stuff with symlink" />
</target>

<target name="makesymlink" depends="jar.symlink.test" if="jar.symlink.notexist">
<symlink link="${WORKDIR}/jar" resource="./build/jar" />
</target>

<!-- Set jar.symlink.notexist to true if ${WORKDIR}/jar symlink does not exist -->
<target name="jar.symlink.test">
<condition property="jar.symlink.notexist">
<not>
<available file="${WORKDIR}/jar" property="jar.symlink.exists" />
</not>
</condition>
</target>
Post by Douglas Kramer
Thanks. Okay, great so far...
<available file="${WORKDIR}/jar" property="jar.symlink.exists" />
but I also need to excute a statement if the property is true, something
if [ -f filename ]; then
echo "File exists";
fi
-Doug
Post by Erik Hatcher
Post by Douglas Kramer
Is there a way to test whether a file exists?
<available>
Douglas Kramer
2005-01-29 23:54:22 UTC
Permalink
Okay, try again.

Need to create a file (actually a symlink) if none exists.

Is there a way to set a property true if a resource does *not* exist?


DETAIL --------------------------------------------

The dosomething task will do something only if the file
exists.

<target name="test">
<available file="${MYFILE}" property="file.exists" value="true" />
</target>


<target name="dosomething" depends="test" if="file.exists">
<echo message="Do something here">
</target>

I tried using <not>, but it won't work, because the "not"
operation of "is not set" is not true.

<target name="test">
<condition property="file.notexists">
<not>
<available file="${MYFILE}" property="file.exists" value="true" />
</not>
</condition>
</target>

Ideas? I don't see a NotAvailable task.

-Doug
Douglas Kramer
2005-01-30 00:26:06 UTC
Permalink
Found "unless" attribute of target.

This simpler version works if ${MYFILE} resource is a file or directory,
but fails if a symlink (which is what I devised this for).

<target name="test">
<available file="${MYFILE}" property="file.exists" value="true" />
</target>

<target name="dosomething" depends="test" unless="file.exists">
<echo message="Do something here">
</target>

Am bummed out.

-Doug
Post by Douglas Kramer
Okay, try again.
Need to create a file (actually a symlink) if none exists.
Is there a way to set a property true if a resource does *not* exist?
DETAIL --------------------------------------------
The dosomething task will do something only if the file
exists.
<target name="test">
<available file="${MYFILE}" property="file.exists" value="true" />
</target>
<target name="dosomething" depends="test" if="file.exists">
<echo message="Do something here">
</target>
I tried using <not>, but it won't work, because the "not"
operation of "is not set" is not true.
<target name="test">
<condition property="file.notexists">
<not>
<available file="${MYFILE}" property="file.exists" value="true" />
</not>
</condition>
</target>
Ideas? I don't see a NotAvailable task.
-Doug
---------------------------------------------------------------------
Ninju Bohra
2005-01-30 01:31:41 UTC
Permalink
You can also look at the <if> task (part of the
ant-contrib project in SourceForge) which along with
the standard condition tasks will allow you to do the
following

<if>
<not>
<isset property="some.thing">
</not>
<then>
<things to do when some.thing is not set>
</then>
<else>
<optional else block.../>
</else>
</if>
Post by Douglas Kramer
Found "unless" attribute of target.
This simpler version works if ${MYFILE} resource is
a file or directory,
but fails if a symlink (which is what I devised this
for).
<target name="test">
<available file="${MYFILE}"
property="file.exists" value="true" />
</target>
<target name="dosomething" depends="test"
unless="file.exists">
<echo message="Do something here">
</target>
Am bummed out.
-Doug
Post by Douglas Kramer
Okay, try again.
Need to create a file (actually a symlink) if none
exists.
Post by Douglas Kramer
Is there a way to set a property true if a
resource does *not* exist?
Post by Douglas Kramer
DETAIL
--------------------------------------------
Post by Douglas Kramer
The dosomething task will do something only if the
file
Post by Douglas Kramer
exists.
<target name="test">
<available file="${MYFILE}"
property="file.exists" value="true" />
Post by Douglas Kramer
</target>
<target name="dosomething" depends="test"
if="file.exists">
Post by Douglas Kramer
<echo message="Do something here">
</target>
I tried using <not>, but it won't work, because
the "not"
Post by Douglas Kramer
operation of "is not set" is not true.
<target name="test">
<condition property="file.notexists">
<not>
<available file="${MYFILE}"
property="file.exists"
Post by Douglas Kramer
value="true" />
</not>
</condition>
</target>
Ideas? I don't see a NotAvailable task.
-Doug
---------------------------------------------------------------------
---------------------------------------------------------------------
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

Loading...