Franz Kafka
Franz Kafka

Reputation: 10831

Ant publish stuff to different locations

I have a java project and an ANT script to build and then distribute the project to other projects with a simple copy command.

I would like to only name the place where to copy to files to once in the header of the ant script, and not have an explicit copy-task for every project that is dependent on this project.

I can't find anything like arrays in ANT, so what would be the cleanest way of distributing something to multiple directories?

Upvotes: 1

Views: 507

Answers (3)

Dante WWWW
Dante WWWW

Reputation: 2739

According to what I commented under Martin's answer, I'd like to post my version of solution as another choice. And I am using property names from Martin's answer to make it clear.

<target name="deploy" >
    <property name="dest.dirs" value="/dir/one,/dir/two,/dir/thr/ee" />
    <for list="${dest.dirs}" param="dest.dir" parallel="true" delimiter="," >
        <sequential>
            <copy todir="@{dest.dir}" >
                <fileset dir="${srd.dir}" />
            </copy>
        </sequential>
    </for>
</target>

Please note that "for" is an Ant-Contrib task, and it's using Macrodef in the back so you should use @{} to refer to "dest.dir"; the "dest.dirs" will be splited into a list (maybe String[]) by delimiter. Here we use comma to split it (and the default value to delimiter is comma). I also added "parallel" to make it copy files to all the "dest.dirs" at same time, however, if the project to copy is large, you should delete "parallel".

Please check http://ant-contrib.sourceforge.net/tasks/tasks/for.html and http://ant-contrib.sourceforge.net/tasks/tasks/foreach.html for more information.

Upvotes: 1

martin clayton
martin clayton

Reputation: 78115

You might consider using a scriptmapper in your copy task with enablemultiplemappings true.

First, list the target directories in a property and create a filelist from it. (You could use a dirset, but the API for filelist is simpler.) Then run the copy, with the scriptmapper setting up the multiple destinations.

<property name="dest.dirs" value="/dir/one,/dir/two,/dir/thr/ee" />    
<filelist id="dests" dir="/" files="${dest.dirs}" />

<copy todir="/" enablemultiplemappings="yes">
    <fileset dir="${srd.dir}" />
    <scriptmapper language="javascript">
        <![CDATA[
            // Obtain a reference to the filelist
            var filelist = project.getReference( "dests" );

            var dests = filelist.getFiles( project );
            for ( var i = 0; i < dests.length; i++ )
            {
                self.addMappedName( dests[i] + "/" + source );
            }
        ]]>
    </scriptmapper>
</copy>

Upvotes: 1

Dave Newton
Dave Newton

Reputation: 160191

I don't believe you have many viable options: the copy task accepts only a single directory.

  1. Create your own copy task that takes a list of directories.
  2. Exec a script/program that does the copying.
  3. Have the subprojects do a pull.

I'm really hesitant about having a project push to other projects, because that makes the assumption that those projects will work with the newly-pushed code. IMO the "sub-"projects should be making the decision if they want the new version or not.

To me this sounds more like a dependency management issue, better handled with Ivy/Maven/Gradle (or other Maven-alike).

All that said, it sounds like you'd want to do option 1, create a custom Ant task that accepts a list of destination directories; it might be pretty easy to just extend the existing copy task to get all its functionality--just add a "todirs" property.

Upvotes: 1

Related Questions