ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Weintraub <qazw...@gmail.com>
Subject Re: factoring out commonality in sequences of tasks
Date Tue, 23 Jun 2009 16:10:52 GMT
There are several things you can use:

One is the <defmacro> command. If <task1> is actually composed of
multiple tasks, then the Macro function might be the way to go.

The other is to look into the AntContrib package
<http://ant-contrib.sourceforge.net/ant-contrib/manual/#install> and
use the <if><then> tasks in the antContrib package:
<http://ant-contrib.sourceforge.net/ant-contrib/manual/tasks/>

<target name="deploy">
<task1/>
   <if>
       <equal arg1="${deployType}" arg2="copy"/>
       <then>
           <copy-files-2/>
      </then>
      <else>
            <link-files-2/>
       </else>
   </if>
   <task 3/>
   <if>
       <equals arg1="${deployType}" arg2="copy"/>
       <then>
            <copy-files-4/>
       </then>
       <else>
           <link-files-4/>
      </else>
   </if>
   <task-5/>
</target>

Another possibility is to use <antcall> to call the tasks in the order you want:

<target name="deploy-with-copy">
    <antcall target="task1"/>
    <antcall target="copy-files-2"/>
    <antcall target="task3"/>
    <antcall target="copy-files-4/>
</target>

Infact, what is the difference between copy/link task #2 and copy/link
task #5? Do they do the same thing, but maybe with different filesets?
What you might want to do is use defmacro and pass the difference
between the two tasks as parameters.

Then, you could use the <if> task to simply separate out the link vs.
copy stuff. You have  a single macro to do the copy/link, and you
simply pass along the information you need to copy or link to that
macro.

That way, your target looks like this:

<target name="deploy">
    <task1/>
    <copy.macro deployType="${deploy.type}>
         <files-to-copy-or-link>
              <fileset dir="${foo}"/>
         </files-to-copy-or-link/>
    </copy.macro>
    <task3/>
    <copy.macro deployType="${deploy.type}">
        <files-to-copy-or-link>
             <fileset dir="${bar}"/>
        </files-to-copy-or-link>
    </copy.macro>
    <task5/>
</target>


<macrodef name="copy.macro">
     <attribute name="deployType" required="true"/>
     <element name="files-to-copy-or-link" />
    <sequential>
          <if>
             <equals arg1="@{deployType}" arg2="copy"/>
             <then>
                   <copy file tasks/>
             </then>
             <else>
                    <link file tasks/>
              </else>
           </if>
      </sequencial>
</macrodef>

On Mon, Jun 22, 2009 at 4:46 PM, jscripter<pc88mxer@gmail.com> wrote:
>
> Suppose I have an operation which deploys files and I have the option of
> deploying the files by copying them and deploying using symlinks:
>
> <target name="deploy-using-copy">
>  <task1>
>  <copy-files-2>
>  <task3>
>  <copy-files-4>
>  <task5>
> </target>
>
> <target name="deploy-using-link">
>  <task1>
>  <link-files-2>
>  <task3>
>  <link-files-4>
>  <task5>
> </target>
>
> What's the best way to factor out the commonality of these two targets?
>
> Note that <task1>, <task3>, etc. are going to be sequence of tasks and not
> just a single task element.
> (Is it possible to define a task which is a sequence of tasks?)
>
> Thanks,
> ER
>
>
>
> --
> View this message in context: http://www.nabble.com/factoring-out-commonality-in-sequences-of-tasks-tp24155135p24155135.html
> Sent from the Ant - Users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>



-- 
David Weintraub
qazwart@gmail.com

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


Mime
View raw message