Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

ant - using conditions with in a target

Thread view: 
coltrane - 30 Mar 2006 20:57 GMT
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
SMC - 31 Mar 2006 02:29 GMT
> Is it possible to use a condition within a target?
>
[quoted text clipped - 11 lines]
> </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>

Signature

Sean

If you want it real bad, you can get it *real bad*.

Wibble - 31 Mar 2006 03:05 GMT
>>Is it possible to use a condition within a target?
>>
[quoted text clipped - 34 lines]
>     <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
coltrane - 31 Mar 2006 13:44 GMT
Thanks,

I might give this a try.

john
coltrane - 31 Mar 2006 14:19 GMT
wow, I what a great package!

thanks again
coltrane - 31 Mar 2006 13:44 GMT
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
Roedy Green - 31 Mar 2006 19:56 GMT
>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

Signature

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

Wibble0@gmail.com - 31 Mar 2006 20:56 GMT
> >I also think I might perform use an antcall to call a target with an if
> >condition.
[quoted text clipped - 23 lines]
> Canadian Mind Products, Roedy Green.
> http://mindprod.com Java custom programming, consulting and coaching.

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.
Roedy Green - 31 Mar 2006 21:16 GMT
>Why doesn't just the copy suffice?

because it generates bogus error messages if the file does not exist.
Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.