1

I need to unzip multiple jars in my folder and add some entry in Manifest.MF and have to zip the jars again indivudually. I dont need a combined jar. But I need indivudal jars with new Manifest entries. How can i do it using ant script? Is there any way to do it.

For eg. I have two jar files in my location say test1.jar, test2.jar

So if i am unzipping with this code

<jar destfile="test1.jar">
    <fileset dir="build/main/classes"/>
    <zipfileset includes="**/*.class" src="lib/main/test.jar"/>
    <manifest>
    <attribute name="permission" value="all-permissions"/>
    </manifest>
</jar>

and again If I am going to zip it

<zip destfile="test1.jar">
 <zipfileset src="test.jar">
  <exclude name="do/not/include/this/class"/>
 </zipfileset>
</zip>

In this case for the processing of test2.jar, i have to repeat the above script again in my build.xml

But I need to do it generically, so that it can pick up both my jar files and add the manifest property and create two jars in the same name as test1.jar and test2.jar.

Something like:

 <jar destfile="*.jar">
    <fileset dir="build/main/classes"/>
    <zipfileset includes="**/*.class" src="lib/main/test.jar"/>
    <manifest>
    <attribute name="permission" value="all-permissions"/>
    </manifest>
</jar>
googytech
  • 479
  • 4
  • 7
  • 18

3 Answers3

1

It sounds like you could do this with a macro:

<macrodef name="updatemanifest">
    <attribute name="jarfile"/>
    <sequential>
        <local name="manifest"/>
        <tempfile property="manifest" destdir="${java.io.tmpdir}"
            suffix=".mf"/>

        <copy tofile=${manifest}">
            <zipentry zipfile="@{jarfile}" name="META-INF/MANIFEST.MF"/>
        </copy>

        <manifest file="${manifest}" mode="update">
            <attribute name="permission" value="all-permissions"/>
        </manifest>

        <jar destfile="@{jarfile}" manifest="${manifest}" update="true"/>

        <delete file="${manifest}"/>
    </sequential>
</macrodef>

<updatemanifest jarfile="build/test1.jar"/>
<updatemanifest jarfile="build/test2.jar"/>
VGR
  • 40,506
  • 4
  • 48
  • 63
0

Just use ant's Jar task.

Example:

<jar destfile="build/main/checksites.jar">
    <fileset dir="build/main/classes"/>
    <zipfileset includes="**/*.class" src="lib/main/some.jar"/>
    <manifest>
      <attribute name="Main-Class" value="com.acme.checksites.Main"/>
    </manifest>
</jar>

For unzipping, you could use ant's unzip task

Example:

<zip destfile="new.jar">
  <zipfileset src="old.jar">
    <exclude name="do/not/include/this/class"/>
  </zipfileset>
</zip>
R. Oosterholt
  • 7,720
  • 2
  • 53
  • 77
  • Yes this is correct. But in this case I am hardcoding the jar file name. But is it possible for me to do it generic. so that I can unjar all the files, add the properties and then jar it again in the same name without mentioning the actual jar name. . – googytech Oct 25 '13 at 06:20
  • well you could loop specific files and execute tasks on them. Example http://stackoverflow.com/q/1467991/1183010 – R. Oosterholt Oct 25 '13 at 06:23
  • I think you are misunderstanding my problem. I have edited my question again. Im not sure whether your solution will work for it. – googytech Oct 25 '13 at 06:54
0

You could use the AntCall task and foreach, but you have to use ant-contrib:

<target name="myMaintask">
  <foreach target="UnzipChangeZip" param="fileName">
    <fileset dir="${myDirectoryWithJars}" casesensitive="yes">
      <include name="**/*.jar"/>
    </fileset>
  </foreach>
</target>

<target name="UnzipChangeZip">
  <echo message="fileName=${fileName}"/>

  <jar destfile="${fileName}">
    <fileset dir="build/main/classes"/>
    <zipfileset includes="**/*.class" src="lib/main/test.jar"/>
    <manifest>
    <attribute name="permission" value="all-permissions"/>
    </manifest>
 </jar>

 <zip destfile="${fileName}">
   <zipfileset src="test.jar">
     <exclude name="do/not/include/this/class"/>
   </zipfileset>
 </zip>

</target>
thommy
  • 73
  • 6
  • this was helpful.. But again you are hardcoding the jar file in the code as test1 and test2.jar. But i have hundreds of jar file in the folder and i dont want to set all the names manually.. – googytech Oct 25 '13 at 07:20