Tuesday Jul 10, 2007

Pack200 and compression through ant

If you are interested in compressing your jar file, you can use Pack200 for compression.

You can also do this through ant using the Pack200Task. (Pack200Task.jar is shipped is shipped with glassFish). Here is a code snippet that does this:


<property name="jarpack-task.jar" value="${glassfish_home}/lib/Pack200Task.jar"/>
   <target name="jarpack-tasks" >
       <taskdef name="pack200"
                classname="com.sun.tools.apache.ant.pack200.Pack200Task"
                classpath="${jarpack-task.jar}" />
       <taskdef name="unpack200"
                classname="com.sun.tools.apache.ant.pack200.Unpack200Task"
                classpath="${jarpack-task.jar}" />
   </target>

     <!-- Target to pack the jars using the Pack200 ant optional task -->
   <target name="jar-pack" depends="jarpack-tasks" description="Applying the pack utility on jars">
       <mkdir dir="${pack.jar.dir}/normalized" />
       <pack200 src="${pack.jar.dir}/${pack.jar.name}" destfile="${pack.jar.dir}/normalized/${pack.jar.name}"
                repack="true" stripdebug="false" deflatehint="keep" unknownattribute="pass" keepfileorder="true" />
   </target>

   <target name="jar-unpack" depends="jarpack-tasks">
       <unpack200 src="${pack.jar.dir}/${pack.jar.name}.pack.gz" dest="${pack.jar.dir}/${pack.jar.name}" />
       <delete file="${pack.jar.dir}/${pack.jar.name}.pack.gz" />
   </target>

     <target name="pack-all">
       <antcall target="jar-pack">
           <param name="pack.jar.dir" value="${build}" />
           <param name="pack.jar.name" value="roller_configurator_${uc_version}.jar" />
       </antcall>
   </target>

In case you are running into Out Of Memory, (especially on windows), you can try changing your JVM heap size. You can do this using ant's ANT_OPTS as below, and then invoke the ant pack-all target.

set ANT_OPTS=-Xmx512m

In case you are interested in signing your compressed file, then you may need to pack, unpack and then repack your jar. This extra step is needed since Pack200 rearranges the contents of the resultant JAR file. The jarsigner hashes the contents of the class file and stores the hash in an encrypted digest in the manifest. When the unpacker runs on a packed packed, the contents of the classes will be rearranged and thus invalidate the signature. Therefore, the JAR file must be normalized first using pack200 and unpack200, and thereafter signed. Here is how this repackaging can be done:


    <pack200 src="${pack.jar.dir}/${pack.jar.name}" destfile="${pack.jar.dir}/normalized/${pack.jar.name}" 
                 repack="true" stripdebug="false" deflatehint="keep" unknownattribute="pass" keepfileorder="true" /> 
    <unpack200 src="${pack.jar.dir}/normalized/${pack.jar.name}.pack.gz" 
                   dest="${pack.jar.dir}/normalized/a-${pack.jar.name}" /> 
    <pack200 src="${pack.jar.dir}/normalized/${pack.jar.name}" 
                 destfile="${pack.jar.dir}/normalized/${pack.jar.name}.pack.gz" 
                 gzipoutput="true" stripdebug="false" deflatehint="keep" 
                 unknownattribute="pass" keepfileorder="true"/>         
    
About

manveen

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today