ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rebo, Alex" <Alex.R...@acs-inc.com>
Subject RE: Invoking a target (Ant 1.7) in a loop (Ant-Contrib)
Date Wed, 17 Jan 2007 18:44:52 GMT
So many thanks, Peter!

One "smart" Q, however: 
Ability to provide qualified name space,
such as in 
<ac:for ...

</ac:for>,

should be granted by smth like:

xmlns:ac="antlib:net.sf.antcontrib"

Where that "smth" should be specified, provided my reference to antcontrib
is set (per installation instructions):

	<taskdef resource="net/sf/antcontrib/antlib.xml"> 
		<classpath> 
			<pathelement
location="/app/java/ant-contrib/ant-contrib-1.0b3.jar"/>
		</classpath> 
	</taskdef>	
?

-----Original Message-----
From: Peter Reilly [mailto:peter.kitt.reilly@gmail.com]
Sent: Wednesday, January 17, 2007 12:18
To: Ant Users List
Subject: Re: Invoking a target (Ant 1.7) in a loop (Ant-Contrib)


On 1/17/07, Rebo, Alex <Alex.Rebo@acs-inc.com> wrote:
> Thank you, Steve!
>
> The idea was to "unload" the heavy construct such as:
> <target name="selfCheck">
>         ...
>         <if>
>                 <equals arg1="${doNotHaveCheckSums}"    arg2="true"     />
>                 <then>
>                         <for    param="fileToWorkOn">
>                                 <path>
>                                         <fileset refid="extentionLibs" />
>                                 </path>
>                                 <sequential>
>                                         <checksum
> file="@{fileToWorkOn}"  algorithm="MD5" />
>                                         <echo>
>                                                         Created checksum
> file for @{fileToWorkOn}.
>                                         </echo>
>
>                                 </sequential>
>                         </for>
>                 </then>
>                 <else>
>                         <for    param="fileToWorkOn">
>                                 <path>
>                                         <fileset refid="extentionLibs" />
>                                 </path>
>                                 <sequential>
>                                         <checksum
> file="@{fileToWorkOn}"  verifyProperty="doesCheckSumMatch"      />
>                                         <fail   message="Checksum
> verification failed for @{fileToWorkOn}.">
>                                                 <condition>
>                                                         <equals
> arg1="${doesCheckSumMatch}"     arg2="false"    />
>                                                 </condition>
>                                         </fail>
>
>                                 </sequential>
>                         </for>
>                 </else>
>         </if>
>         <echo>
>                 "Self check done."
>         </echo>
> </target>
>
>
> with:
>
> <target name="selfCheck">
>         ...
>         <if>
>                 <equals arg1="${doNotHaveCheckSums}"    arg2="true"     />
>                 <then>
>                         <foreach        target="computeCheckSum"
> param="fileToWorkOn"    inheritall="true">
>                                 <path>
>                                         <fileset refid="extentionLibs" />
>                                 </path>
>                         </foreach>
>                 </then>
>                 <else>
>                         <foreach        target="verifyCheckSum"
> param="fileToWorkOn"    inheritall="true">
>                                 <path>
>                                         <fileset refid="extentionLibs" />
>                                 </path>
>                         </foreach>
>                 </else>
>         </if>
>         <echo>
>                 "Self check done."
>         </echo>
> </target>
>
>
> <target name="computeCheckSum"  if="doNotHaveCheckSums">
>         <checksum       file="${fileToWorkOn}"  algorithm="MD5" />
>                 <echo>
>                         Created checksum file for ${fileToWorkOn}.
>                 </echo>
> </target>
>
> <target name="verifyCheckSum"   unless="doNotHaveCheckSums">
>         <checksum       file="${fileToWorkOn}"
> verifyProperty="doesCheckSumMatch"      />
>         <fail   message="Checksum verification failed for
${fileToWorkOn}.">
>                 <condition>
>                         <equals arg1="${doesCheckSumMatch}"
arg2="false"
> />
>                 </condition>
>         </fail>
> </target>
>
> In my mind the "if-then-else" block looks cleanly this way.
> On top, target contra to macrodef can be executed conditionally
> (if/unless)
>
> You are correct pointing out that if a target can't be invoked all by
itself
> (answer to your Q: "do you ever execute ant using that as a target or is
it
> a utility type thing?"
> is "No") it should be a target.
> Per Dominique's suggestion I replaced "foreach" with "for" thus loosing
the
> ability
> to call a target within loops body.
> If I can figure out how to put macrodefs in place of a target, I might try
> to "unload" the body of the loop.

You can use the ac:runtarget to call a target within the
same project, but you have to be careful to handle
properties carefully.

In my current project, I use something like
this:
  <target name="clean-start" depends="clean">
    <ac:var name="db.auto" value="create-drop"/>
    <ac:runtarget target="unexplode"/>
    <ac:runtarget target="restart"/>
  </target>

In your target:

 <target name="computeCheckSum"  if="doNotHaveCheckSums">
       <checksum       file="${fileToWorkOn}"  algorithm="MD5" />
               <echo>
                       Created checksum file for ${fileToWorkOn}.
               </echo>
</target>

There are two params: doNotHaveCheckSums and fileToWorkOn,
it would make more sense to convert this to a macro

It looks like doNotHaveChecksSums is global and checked before
the target is called and fileToWorkOn is
local so the macro would look something like this:

So I would do:

<macrodef name="computeCheckSum">
    <attribute name="fileToWorkOn"/>
     <sequential>
        <checksum       file="@{fileToWorkOn}"  algorithm="MD5" />
         <echo>
                       Created checksum file for @{fileToWorkOn}.
         </echo>
    </sequential>
</target>

<macrodef name="verifyCheckSum">
   <attribute name="fileToWorkOn"/>
   <sequential>
       <ac:var name="doesCheckSumMatch" unset="yes"/>
       <checksum       file="@{fileToWorkOn}"
                             verifyProperty="doesCheckSumMatch"      />
       <fail   message="Checksum verification failed for @{fileToWorkOn}.">
           <condition>
               <equals arg1="${doesCheckSumMatch}"     arg2="false"
                />
           </condition>
      </fail>
   </sequential>
<macrodef>

So I would do:

 <target name="selfCheck">
    <ac:for param="fileToCheck">
        <fileset refid="extentionLibs" />
        <seqential>
           <ac:if>
               <equals arg1="${doNotHaveCheckSums}"    arg2="true"     />
                <then>
                     <computeCheckSum fileToCheck="@{fileToCheck}"/>
                </then>
                <else>
                     <verifyCheckSum filetoCheck="@{fileToCheck}"/>
                </else>
           </ac:if>
        </sequential>
    </ac:for>
</target>

However as Steve says, most ant tasks are meant to
work on filesets/paths, and normally would have built-in iterator/dependence
checking, so in general and if possible one should see it that
tasks themselves can be used to to the iteration.

Peter
>
> Do you think it's worse the effort, Steve?
>
> PS.: Could you, please, elaborate on "In Ant 1.7, many support resources,
> which provides you
> even more ways to source data."?
>
> -----Original Message-----
> From: Steve Loughran [mailto:stevel@apache.org]
> Sent: Wednesday, January 17, 2007 06:02
> To: Ant Users List
> Subject: Re: Invoking a target (Ant 1.7) in a loop (Ant-Contrib)
>
>
> Rebo, Alex wrote:
> > Hello!
> >
> > In attempt to compute checksums for all files in a directory I wrote
this:
> >
> > <fileset id="myLibs" dir="${extention}">
> >     <include name="*.jar"/>
> > </fileset>
> >
> > .....
> >
> > <target name="thisTargetName">
> >     <foreach target="computeCheckSum" param="fileToWorkOn"
> > inheritall="true">
> >         <path>
> >             <fileset refid="myLibs" />
> >         </path>
> >     </foreach>
> > </target>
> >
> > ....
> >
> > <target name="computeCheckSum">
> >     <checksum file="${fileToWorkOn}" algorithm="MD5" />
> >     <echo>
> >         Created checksum file for ${fileToWorkOn}.
> >     </echo>
> > </target>
>
>
> I want to ask an even sillier question. why not just hand the entire
> fileset to <checksum>?
>
> <checksum algorithm="MD5" >
>         <fileset refid="myLibs" />
> </checksum>
>
> No macros, no iteration; ant does the bulk work with depdnency logic.
> See example 7 and 8 in the checksum page in teh manual.
>
> Remember: most Ant tasks are designed to work in bulk, with filesets or
> paths as params. In Ant 1.7, many support resources, which provides you
> even more ways to source data.
>
> -Steve
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message