You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Alexei Alexei <a_...@mail.ru> on 2008/12/28 16:37:51 UTC

Ant- copy file from one folder to many folders

Hi!
I have file "myclass.class" in the next folders:

-myfolder1
-myfolder2
-myfolder2/test
-myfolder2/test/test2
-myfolder2/test3

I need to copy "myclass.class" from "myfolder1" to the folder "myfolder2" and all it's subfolders. How I can do this with Ant




Новый Mail.Ru Агент с Аськой! 
http://r.mail.ru/cln4786/agent.mail.ru/ru/


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


RE: Ant- copy file from one folder to many folders

Posted by "Rebhan, Gilbert" <Gi...@huk-coburg.de>.

-----Original Message-----
From: David Weintraub [mailto:qazwart@gmail.com]
Sent: Monday, December 29, 2008 6:10 AM
To: Ant Users List; Alexei Alexei
Subject: Re: Ant- copy file from one folder to many folders

/*

The standard <copy> task requires a single target name, so you
couldn't do it with the standard Ant tasks. However, there is a
special addition to Ant called the AntContrib tasks
<http://ant-contrib.sourceforge.net/>. These extend Ant to allow for
all sorts of tricks.

There's one special AntContrib task called <foreach>
<http://ant-contrib.sourceforge.net/tasks/tasks/foreach.html>. This
will allow you to go through each directory in a list, and use that as
the "todir" parameter of a copy task. It'll look something like this:

<target name="orig">
    <foreach
        target="copyclass"
        parameter="copy.target">
        <path>
             <dirset dir="myfolder*/**"/>
        </path>
    </foreach>
</target>

<target name="copy">
    <copy todir="${copy.target}"
        file="myclass.class"/>
</target>

READ THE COMPLETE DOCUMENTATION ON THE ANT-CONTRIB PACKAGE! You must
download the AntContrib JAR file, put it in your Ant's "lib"
directory, and add a <taskdef> task to your build.xml file.

*/

Better use <for> instead of <foreach>, as foreach opens a new project scope
for every target execution.

Also with <for> there's no need for an extra target.
So with a given structure like that =

c:\test>tree /A /F
C:.
+---myfolder1
|       myclass.class
|
\---myfolder2
    |   myclass.class
    |
    +---test
    |   |   myclass.class
    |   |
    |   \---test2
    |           myclass.class
    |
    \---test3
            myclass.class

you can use =

<project name="foobar" default="main" basedir=".">

<!-- // Taskdefs -->
<!-- Import AntContrib -->
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<!-- Taskdefs // -->

<target name="main">
 <for param="dir">
  <dirset dir="C:/test" includes="**"/>
   <sequential>
    <copy todir="@{dir}" overwrite="true" verbose="true">
     <fileset dir="C:/test/myfolder1" includes="myclass.class" />
    </copy>
   </sequential>
 </for>
</target>

</project>


btw Alexei =
your given situation was a bit irritating to me, as the file myclass.class is
already existent in all directories, including the one you want to copy from
(myfolder1 in your example).
i understood it like that = a new myclass.class drops in myfolder1 and you
need to spread it recursive in all subdirectories, right !?


Regards, Gilbert

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


Re: Ant- copy file from one folder to many folders

Posted by David Weintraub <qa...@gmail.com>.
The standard <copy> task requires a single target name, so you
couldn't do it with the standard Ant tasks. However, there is a
special addition to Ant called the AntContrib tasks
<http://ant-contrib.sourceforge.net/>. These extend Ant to allow for
all sorts of tricks.

There's one special AntContrib task called <foreach>
<http://ant-contrib.sourceforge.net/tasks/tasks/foreach.html>. This
will allow you to go through each directory in a list, and use that as
the "todir" parameter of a copy task. It'll look something like this:

<target name="orig">
    <foreach
        target="copyclass"
        parameter="copy.target">
        <path>
             <dirset dir="myfolder*/**"/>
        </path>
    </foreach>
</target>

<target name="copy">
    <copy todir="${copy.target}"
        file="myclass.class"/>
</target>

READ THE COMPLETE DOCUMENTATION ON THE ANT-CONTRIB PACKAGE! You must
download the AntContrib JAR file, put it in your Ant's "lib"
directory, and add a <taskdef> task to your build.xml file.

On Sun, Dec 28, 2008 at 10:37 AM, Alexei Alexei <a_...@mail.ru> wrote:
> Hi!
> I have file "myclass.class" in the next folders:
>
> -myfolder1
> -myfolder2
> -myfolder2/test
> -myfolder2/test/test2
> -myfolder2/test3
>
> I need to copy "myclass.class" from "myfolder1" to the folder "myfolder2" and all it's subfolders. How I can do this with Ant
>
>
>
>
> Новый Mail.Ru Агент с Аськой!
> http://r.mail.ru/cln4786/agent.mail.ru/ru/
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>



-- 
--
David Weintraub
qazwart@gmail.com