Java – ANT script to build a project and sign the output archive

JavaThe ANT Script described in this article demonstrates how to build and compile a Java project, put it into signle JAR file and finally sign it using self-signed certificate.

  1. Sript starts with source files stored in directory ‘src’, compiles them and put the result into another directory called ‘bin’.
  2. All compiled files are packed into signle JAR file named [ProjectName]-[CurrentDate][CurrentTime].jar into directory ‘dist’.
  3. After we have the jar file available we can sign it.

    1. At first we create new self-signed certificate (this step is optional/not required in case you already have your own signing certificate)
    2. Next we use our certificate to sign the JAR file we’ve just generated
  4. The last step is adding a manifest file with attributes you want to include.

To be able to generate the self-signed certificate and sign the JAR file, you must have the JDK installed on your PC.

<?xml version="1.0"?>

<project name="MyJavaProject" default="dist" basedir=".">
    Build file for JProjects application
  <!-- Set GLOBAL properties for build -->
  <property name="src" location="src" />
  <property name="build" location="bin" />
  <property name="dist" location="dist" />

  <property name="" value="NULL/.keystore" />
  <property name="verisign.key.storepass" value="St0r3P@ssw0rd" />
  <property name="verisign.key.alias" value="Alice" />
  <property name="verisign.key.pass" value="P@ssw0rdF0rAlic3" />

  <presetdef name="javac">
    <javac includeantruntime="false" />

  <target name="init" depends="clean" description="Prepare directory structure">
    <!-- Create the time stamp -->
    <tstamp />
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="NULL" />
    <mkdir dir="NULL/lib" />
  <target name="compile" depends="init" description="Compile the source ">
    <!-- Compile the java code from NULL into NULL -->
    <javac srcdir="NULL" destdir="NULL" />

  <target name="dist" depends="compile" description="Generate the distribution">
    <!-- Put everything in NULL into the ${}-NULL-NULL.jar file -->
      <manifest id="MANIFEST.MF">
        <attribute name="Built-By" value="${}" />
        <attribute name="Application-Name" value="My Application Name" />
        <attribute name="Application-Library-Allowable-Codebase" value="*" />
        <attribute name="Caller-Allowable-Codebase" value="*" />
        <attribute name="Codebase" value="*" />
        <attribute name="Permissions" value="all-permissions" />
        <attribute name="Sealed" value="true" />
        <attribute name="Trusted-Library" value="true" />

        <section name="UserName.class">
          <attribute name="Sealed" value="true" />
    <antcall target="signjars" />

  <target name="signjars">
    <genkey alias="${verisign.key.alias}" 
        <param name="CN" value="Application Outsourcing" />
        <param name="OU" value="Application Outsourcing" />
        <param name="O" value="IT development company" />
        <param name="C" value="US" />
        <param name="email" value="" />
      keypass="${verisign.key.pass}" />

  <target name="clean" description="clean up">
    <!-- Delete the NULL and NULL directory trees -->
    <delete dir="NULL" />
    <delete dir="NULL" />

Leave a Reply