build.xml

<project name="myviewer" default="sign">

  <!-- Directories for the src, jarfiles and compiled classfiles -->
  <property name="srcdir" value="src"/>
  <property name="libdir" value="lib"/>
  <property name="destdir" value="build"/>

  <!-- The Jar file we're trying to create -->
  <property name="jar" value="myviewer.jar"/>

  <!-- The keystore path, alias and password (for signing the Jar) -->
  <property name="ks.path" value="keystore.jks"/>
  <property name="ks.alias" value="myalias"/>
  <property name="ks.password" value="secret"/>

  <property name="build.sysclasspath" value="ignore"/>
  <property name="bfopdfjar" value="${libdir}/bfopdf.jar"/>
  <property name="servicedir" value="META-INF/services" />
  <property name="servicefile" value="org.faceless.pdf2.viewer2.ViewerFeature" />
  <path id="buildclasspath">
    <pathelement location="${bfopdfjar}"/>
  </path>


  <!--
    -  Clean - do the compile and create the unsigned Jar
   -->
  <target name="clean">
     <delete dir="${destdir}"/>
     <delete file="${jar}"/>
     <delete file="${jar}.pack.gz"/>
  </target>


  <!--
    -  Build - do the compile and create the unsigned Jar
   -->
  <target name="build">
    <mkdir dir="${destdir}/${servicedir}" />
    <javac encoding="utf-8" srcdir="${srcdir}" destdir="${destdir}" classpathref="buildclasspath" />
    <copy todir="${destdir}">
      <fileset dir="${srcdir}" includes="**/resources/**" />
    </copy>
    <!--
      -  This next stage combines the service file from the existing
      -  "bfopdf.jar" with the service file from the
      -  src/META-INF/services path. This merges the new features from
      -  this example with the existing ones into the finished jar.
     -->
    <concat destfile="${destdir}/${servicedir}/${servicefile}">
      <zipfileset src="${bfopdfjar}">
        <include name="${servicedir}/${servicefile}"/>
      </zipfileset>
      <fileset dir="src">
        <include name="${servicedir}/${servicefile}"/>
      </fileset>
    </concat>
    <!-- 
      -  Finally build the new Jar, combining the existing "bfopdf.jar"
      -  with the class files we've compiled at the start of this target.
      -  Remember to exclude the service file from the original jar -
      -  we want to use our own replacement for that
     -->
    <jar destfile="${jar}">
      <zipfileset src="${bfopdfjar}">
        <exclude name="${servicedir}/${servicefile}"/>
        <exclude name="META-INF/*.RSA"/>
        <exclude name="META-INF/*.SF"/>
      </zipfileset>
      <fileset dir="build"/>
    </jar>
  </target>


  <!--
    -  Sign - digitally sign the jar and create a "pack200" compressed
    -  version of it as well. This uses the "pack200" task from
    -  http://java.net/projects/java-pack200-ant-task/. You need to
    -  set the keystore path, alias and password properties at the
    -  start of this file.
   -->
  <target name="sign" depends="build">
    <taskdef name="pack200" classname="com.sun.tools.apache.ant.pack200.Pack200Task" classpath="${libdir}/Pack200Task.jar"/>
    <pack200 src="${jar}" destfile="${jar}.tmp.jar" repack="true"/>
    <move file="${jar}.tmp.jar" tofile="${jar}"/>
    <signjar jar="${jar}" alias="${ks.alias}" storepass="${ks.password}" keystore="${ks.path}" />
    <pack200 src="${jar}" destfile="${jar}.pack.gz" gzipoutput="true"/>
  </target>

</project>