Manas Saxena
Manas Saxena

Reputation: 2365

Passing mutliple params in ant foreach task

I have an ant task which i want call from a foreach loop as follows:

<target name="iteratorTarget">
    <foreach target="copynow" param="some.location">
            <path refid="some.classpath"/>
    </foreach>
</target>


<target name="copynow">
    <!-- Do some regex related operations -->
    <propertyregex property="from.directory" input="${some.location}" select="\1"/>
    <copy file="${from.directory}" todir="${dest.dir}"/>
</target>

As you can see in the above code one parameter I passed from foreach is some.location. Now I want to pass another argument dest.dir which i have used in second last line of the above code.

So, how do I pass multiple parameters (some.location and dest.dir in my case) , to copynow target from the foreach I have defined in my above code.

EDIT

Here are some more details of my question . First some premise:

Basically, I have a classpath file which has list of jar location defined.example -

direc1/subdir1/somejar1.jar
direc2/subdir2/somejar2.jar
direc3/subdir3/somejar3.jar

From above information , I want to ensure that whenever a jar is copied to a given dest. , its equivalent license is also copied to a given dest.

The licenses are stored as follows-

direc1/subdir1/license/license*.txt
direc2/subdir2/license/license*.txt
direc3/subdir3/license/license*.txt

What I am trying to achieve here is that I want to iterate over classpath entries , then using regex, obtain directory till subdir . example 'direc1/subdir1' , then I append license to the obtained regex and copy all files present in that folder and then i copy the jar to the required destination.

The code I have given above achieves the same.

Only thing that remains is that copy destination directory is not constant and will vary . Thus, I want to somehow send copy destination directory as parameter to copynow target.Help!

Upvotes: 1

Views: 1129

Answers (2)

Viktor Brešan
Viktor Brešan

Reputation: 5522

foreach supports additional param tags, so your code could look like the following:

<target name="iteratorTarget">
    <foreach target="copynow" param="some.location">
            <path refid="some.classpath"/>
            <param name="dest.dir" value="${dest.dir}" />
    </foreach>
</target>

Code of the copynow task can stay the same.

Upvotes: 0

Mark O&#39;Connor
Mark O&#39;Connor

Reputation: 77961

I use ivy to manage my build dependencies. This tool can be adapted to turn a local directory into a repository. Although this solution appears to be more complex (and it is) it uses artifact patterns to decouple the source files from where they are ultimately copied.

When properly configured, the ivy retrieve task can be used place them within the build workspace. The following saves files within a "target" subdirectory:

  <target name="build" depends="resolve">
    <ivy:retrieve pattern="target/[organisation]/[artifact][revision].[ext]" conf="compile"/>
    <ivy:retrieve pattern="target/[organisation]/[artifact]" conf="licence"/>
  </target>

Example

├── build.xml
├── ivysettings.xml
├── ivy.xml
├── repo
│   ├── direc1
│   │   └── subdir1
│   │       ├── licence
│   │       │   └── licenceOne.txt
│   │       └── somejar1.jar
│   ├── direc2
│   │   └── subdir2
│   │       ├── licence
│   │       │   └── licenceTwo.txt
│   │       └── somejar2.jar
│   └── direc3
│       └── subdir3
│           ├── licence
│           │   └── licenceThree.txt
│           └── somejar3.jar
└── target
    ├── myOrg1
    │   ├── licenceOne.txt
    │   └── somejar1.jar
    ├── myOrg2
    │   ├── licenceTwo.txt
    │   └── somejar2.jar
    └── myOrg3
        ├── licenceThree.txt
        └── somejar3.jar

build.xml

<project name="demo" default="build" xmlns:ivy="antlib:org.apache.ivy.ant">

  <available classname="org.apache.ivy.Main" property="ivy.installed"/> 

  <target name="resolve" depends="install-ivy">
    <ivy:resolve/>
  </target>

  <target name="build" depends="resolve">
    <ivy:retrieve pattern="target/[organisation]/[artifact][revision].[ext]" conf="compile"/>
    <ivy:retrieve pattern="target/[organisation]/[artifact]" conf="licence"/>
  </target>

  <target name="clean">
    <delete dir="target"/>
    <ivy:cleancache/>
  </target>

  <target name="install-ivy" description="Install ivy" unless="ivy.installed">
    <mkdir dir="${user.home}/.ant/lib"/>
    <get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.4.0/ivy-2.4.0.jar"/>
    <fail message="Ivy has been installed. Run the build again"/>
  </target>

</project>

Notes:

  • Special install-ivy target to install the ivy jar.

ivysettings.xml

<ivysettings>
  <settings defaultResolver="central"/>
  <resolvers>
    <ibiblio name="central" m2compatible="true"/>
    <filesystem name="custom">
      <artifact pattern="${ivy.settings.dir}/repo/[maindir]/[subdir]/[artifact][revision].[ext]" />
      <artifact pattern="${ivy.settings.dir}/repo/[maindir]/[subdir]/licence/[artifact]" />
    </filesystem>
  </resolvers>
  <modules>
    <module organisation="myOrg1" resolver="custom"/>
    <module organisation="myOrg2" resolver="custom"/>
    <module organisation="myOrg3" resolver="custom"/>
  </modules>
</ivysettings>

Notes:

  • The "myOrg1", "myOrg2" and "MyOrg3" dependencies are resolved from the local file system underneath the "repo" directory
  • Each pattern in the filesystem resolver is designed to find the files
  • Special non-standard "maindir" and "subdir" attributes specificed (see ivy file)

ivy.xml

<ivy-module version="2.0" xmlns:e="http://ant.apache.org/ivy/extra">
  <info organisation="com.myspotontheweb" module="demo"/>

  <configurations>
    <conf name="compile" description="Jar files"/>
    <conf name="licence" description="Licence files"/>
  </configurations>

  <dependencies>
    <dependency org="myOrg1" name="somejar"        rev="1" e:maindir="direc1" e:subdir="subdir1" conf="compile->default"/>
    <dependency org="myOrg1" name="licenceOne.txt" rev="1" e:maindir="direc1" e:subdir="subdir1" conf="licence->default"/>

    <dependency org="myOrg2" name="somejar"        rev="2" e:maindir="direc2" e:subdir="subdir2" conf="compile->default"/>
    <dependency org="myOrg2" name="licenceTwo.txt" rev="2" e:maindir="direc2" e:subdir="subdir2" conf="licence->default"/>

    <dependency org="myOrg3" name="somejar"          rev="3" e:maindir="direc3" e:subdir="subdir3" conf="compile->default"/>
    <dependency org="myOrg3" name="licenceThree.txt" rev="3" e:maindir="direc3" e:subdir="subdir3" conf="licence->default"/>
  </dependencies>

</ivy-module>

Note:

  • This is the file that lists the build dependencies.
  • Note the extra "maindir" and "subdir" attributes. Used by filesystem resolver to find the files

Upvotes: 0

Related Questions