0

I have a directory with jars in it. I do not have their names, I have a target that takes name of the jar and its location and does operation on it.

<target name="addAttributes">
    <mkdir dir="${folderName}/Temp"/>
    <unzip src="${jarNamewtPath}" dest="${folderName}/Temp"/>
    <delete file="${jarNamewtPath}"/>
    <jar destfile="${jarNamewtPath}">
        <manifest>
            <attribute name="Application-Name" value="someValue"/>
            <attribute name="Trusted-Only" value="true"/>
            <attribute name="Codebase" value="*"/>
            <attribute name="Permissions" value="all-permissions"/>
        </manifest>
        <fileset dir="${folderName}/Temp" />
    </jar>
    <delete dir="${folderName}/Temp"/>
</target>

How can I get the names of the files and individually pass them over to this target.

<target name="getJars">
    <fileset id="getJars" dir="${someDir}/Jars">
        <include name="*.jar" />
    </fileset>
    ..... get list of jars
    <antcall target="addAttributes">
            <param name="folderName" value="${path}"/>
            <param name="jarNamewtPath" value="${path}/name.jar"/>
    </antcall> 
</target>

Any help/clue will be appreciated.

  • 1
    Related: http://stackoverflow.com/a/1468063/738746 – Bhesh Gurung Nov 19 '13 at 22:44
  • 1
    as @BheshGurung points out, they are related... I just wanted to point out Jeffery Frederick's excellent advice http://stackoverflow.com/a/1478159/505191 – thekbb Nov 19 '13 at 22:50
  • Are these internal jars you control? you're going to be way better off manipulating the manifest when you jar them up. – thekbb Nov 19 '13 at 22:50
  • @BheshGurung : Is there a possibility to send two(more) parameters to the target at same time. eg: sends jarName just jarname. Can I send "jarName" and "xyz" at once – user2281527 Nov 19 '13 at 23:44
  • @thekbb : Thanks a lot. its actually jars that are created by some other system and we have to add these attributes to make them work without warnings with java 7 – user2281527 Nov 19 '13 at 23:46

1 Answers1

1

Preferably, a <target> should only be called once per build. For shared functionality that can be used repeatedly in a build, consider a <macrodef> instead.

Here's an example of using the third-party Ant-Contrib's <for> task to repeatedly call the addAttributes <macrodef>:

<project name="ant-for-macrodef" default="getJars">
    <taskdef resource="net/sf/antcontrib/antlib.xml" />

    <macrodef name="addAttributes">
        <attribute name="jar-path"/>
        <sequential>
            <echo>jar-path: @{jar-path}</echo>

            <!-- Make temp.dir locally scoped to this macrodef. -->
            <local name="temp.dir"/>
            <property name="temp.dir" value="myTempDir"/>

            <echo>Perform Jar operations here.</echo>
        </sequential>
    </macrodef>

    <target name="getJars">
        <fileset id="getJars" dir="${someDir}/Jars">
            <include name="*.jar" />
        </fileset>

        <for param="jar.file">
            <fileset refid="getJars"/>
            <sequential>
                <addAttributes jar-path="@{jar.file}"/>
            </sequential>
        </for>
    </target>
</project>
Chad Nouis
  • 6,861
  • 1
  • 27
  • 28