ant - using conditions with in a target

C

coltrane

Is it possible to use a condition within a target?

I have a target that contains some steps that only need to be run if a
certain condition exists.
I know how to set a condition property but how can I use an "if" to
control the flow of the steps.

something like this ( I know the if is invalid )

<target name="sometarget">
<delete dir="directoryOne"/>
<if condition>
<delete dir="directoryTwo"/>
</if>
</target>


thanks for your help

john
 
S

SMC

Is it possible to use a condition within a target?

I have a target that contains some steps that only need to be run if a
certain condition exists.
I know how to set a condition property but how can I use an "if" to
control the flow of the steps.

something like this ( I know the if is invalid )

<target name="sometarget">
<delete dir="directoryOne"/>
<if condition>
<delete dir="directoryTwo"/>
</if>
</target>

If the deletion order is not important:

<target name="sometarget" depends="othertarget">
<delete dir="directoryOne"/>
</target>

<!-- Assume deleteDirTwo property is set somewhere else -->
<target name="othertarget" if="deleteDirTwo">
<delete dir="directoryTwo"/>
</target>

Otherwise:

<target name="sometarget">
<delete dir="directoryOne"/>
<antcall target="othertarget"/>
</target>

<!-- Assume deleteDirTwo property is set somewhere else -->
<target name="othertarget" if="deleteDirTwo">
<delete dir="directoryTwo"/>
</target>
 
W

Wibble

SMC said:
If the deletion order is not important:

<target name="sometarget" depends="othertarget">
<delete dir="directoryOne"/>
</target>

<!-- Assume deleteDirTwo property is set somewhere else -->
<target name="othertarget" if="deleteDirTwo">
<delete dir="directoryTwo"/>
</target>

Otherwise:

<target name="sometarget">
<delete dir="directoryOne"/>
<antcall target="othertarget"/>
</target>

<!-- Assume deleteDirTwo property is set somewhere else -->
<target name="othertarget" if="deleteDirTwo">
<delete dir="directoryTwo"/>
</target>
And if you get up with that, use the ant-contrib jar.

It has an an If task exactly like you wanted, and other good stuff.
Ant is just a theoretical exercise without it.

http://ant-contrib.sourceforge.net/ant-contrib/manual/tasks/if.html
 
C

coltrane

Thanks for the idea.

I also think I might perform use an antcall to call a target with an if
condition.

Thanks again for the help

john
 
R

Roedy Green

I also think I might perform use an antcall to call a target with an if
condition.

here is an ant conditional copy. This is why I spit on XML as too
impoverished for programming and too bloated.

<!-- copy screenshot png from website to project dir, if it exists -->
<available property="has.screenshot"
file="${screenshot.dir}/${ant.project.name}.png" />
<antcall target="copy.screenshot" />

<!-- copy screenshot from website to project dir, if there is one. -->
<target name="copy.screenshot" if="has.screenshot">
<copy file="${screenshot.dir}/${ant.project.name}.png"
tofile="${package.dir}/${ant.project.name}screenshot.png"
failonerror="false" overwrite="true" />
</target>


What this says is :

if exists E:\mindprod\screenshot\currcon.png
copy E:\mindprod\screenshot\currcon.png
C:\com\mindprod\currcon\currconscreenshot.png
 
W

Wibble0

Roedy said:
here is an ant conditional copy. This is why I spit on XML as too
impoverished for programming and too bloated.

<!-- copy screenshot png from website to project dir, if it exists -->
<available property="has.screenshot"
file="${screenshot.dir}/${ant.project.name}.png" />
<antcall target="copy.screenshot" />

<!-- copy screenshot from website to project dir, if there is one. -->
<target name="copy.screenshot" if="has.screenshot">
<copy file="${screenshot.dir}/${ant.project.name}.png"
tofile="${package.dir}/${ant.project.name}screenshot.png"
failonerror="false" overwrite="true" />
</target>


What this says is :

if exists E:\mindprod\screenshot\currcon.png
copy E:\mindprod\screenshot\currcon.png
C:\com\mindprod\currcon\currconscreenshot.png

Why doesn't just the copy suffice?

<copy file="${screenshot.dir}/${ant.project.name}.png"
tofile="${package.dir}/${ant.project.name}screenshot.png"
failonerror="false" overwrite="true" />

XML is ugly but not unusable. Ant without If, for, runtarget and other
ant-contrib
tasks is unusable, but thats not because of XML.
 
Joined
Oct 18, 2007
Messages
1
Reaction score
0
How do i put an else to send a message error?

Roedy Green said:
On 31 Mar 2006 04:44:09 -0800, "coltrane" <[email protected]>
wrote, quoted or indirectly quoted someone who said :

>I also think I might perform use an antcall to call a target with an if
>condition.


here is an ant conditional copy. This is why I spit on XML as too
impoverished for programming and too bloated.

<!-- copy screenshot png from website to project dir, if it exists -->
<available property="has.screenshot"
file="${screenshot.dir}/${ant.project.name}.png" />
<antcall target="copy.screenshot" />

<!-- copy screenshot from website to project dir, if there is one. -->
<target name="copy.screenshot" if="has.screenshot">
<copy file="${screenshot.dir}/${ant.project.name}.png"
tofile="${package.dir}/${ant.project.name}screenshot.png"
failonerror="false" overwrite="true" />
</target>


What this says is :

if exists E:\mindprod\screenshot\currcon.png
copy E:\mindprod\screenshot\currcon.png
C:\com\mindprod\currcon\currconscreenshot.png

--

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

From Chile, my english is poor, but i'll give a try...

How do i make an else here?. Let's say to put an error message if the file doesn't exist?.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top