Ant Swing - A sample ant build script for a Java Swing application (FPTracker)

Ant Swing build script FAQ: Can you share an example of an Ant build script for Java Swing applications?

Here is another sample Ant build script for a Java application. Again, I hope it can help you "Learn Java by Example", or in this case, “Learn Ant by Example.”

My Ant Swing build script

One interesting note about this Ant build script. At the end of the script you'll see that I'm incorporating a task from a product named yGuard. yGuard supplies an Ant task that you can use to obfuscate the Java code during the compilation process.

<!--
  The main build file for the fptracker program.
-->

  <project name="fptracker" default="obfuscate" basedir="..">

  <!-- IMPORTANT VARIABLE HERE -->
  <property name="build.dir" value="C:/Projects_LocalCVS/DevDaily/FPTracker"/>
  <path id="class.path">
    <fileset dir="lib">
      <include name="**/*.jar"/>
      <include name="**/*.zip"/>
    </fileset>
  </path>

  <target name="init">
     <property name="project_name"   value="fptracker"/>
     <property name="jar"            value="${build.dir}/jar/${project_name}.jar"/>
     <property name="obfjar"         value="${build.dir}/jar/${project_name}_obf.jar"/>
     <property name="obfuscationlog" value="${build.dir}/jar/${project_name}_obflog.xml"/>
     <property name="mainclass"      value="com.devdaily.fptracker.FPTrackerMain"/>
     <property name="sampleDataDir"  value="${build.dir}/sampledata"/>
    <tstamp/>
  </target>

  <target name="create_classes_dir" depends="init">
    <mkdir dir="${build.dir}/classes-ant"/>
  </target>
  

  <!-- CLEAN TARGET -->
  <target name="clean">
    <delete dir="${build.dir}/classes-ant"/>
  </target>

  <!-- COMPILE TARGET -->
  <target name="compile" depends="clean,create_classes_dir">
    <javac destdir="${build.dir}/classes-ant" source="1.4" >
      <src path="src"/>
      <exclude name="**/_*.java"/>
      <classpath refid="class.path"/>
    </javac>
    <copy todir="${build.dir}/classes-ant">
      <fileset dir="${build.dir}/src">
        <include name="**/*.gif"/>
        <include name="**/*.jpg"/>
        <include name="**/*.png"/>
      </fileset>
      <fileset dir="${build.dir}">
        <include name="reports/**/*.*"/>
      </fileset>
    </copy>
    <!-- SPECIAL STUFF HERE TO GET THE REPORTS IN THE RIGHT DIRECTORY -->
    <copy todir="${build.dir}/classes-ant/com/devdaily/fptracker/controller">
      <fileset dir="${build.dir}/reports">
        <include name="**/*"/>
      </fileset>
    </copy>
  </target>


  <!-- MAKE JAR TARGET -->
  <target name="make_jar" depends="compile">
    <jar basedir="${build.dir}/classes-ant" 
         jarfile="${build.dir}/jar/${project_name}.jar" 
         manifest="${build.dir}/build/${project_name}.manifest"
         includes="**/*.*"/>
    <copy todir="${build.dir}/jar">
      <fileset dir="${build.dir}/lib">
        <include name="**/*.jar"/>
        <include name="**/*.zip"/>
      </fileset>
    </copy>
    <mkdir dir="${build.dir}/jar/fpdata"/>
    <copy todir="${build.dir}/jar/fpdata">
      <fileset dir="${sampleDataDir}">
        <include name="**/*"/>
      </fileset>
    </copy>
  </target>


  <!-- MAKE OBFUSCATED JAR -->
  <taskdef name="obfuscate" classname="com.yworks.yguard.ObfuscatorTask"/>
  <target name="obfuscate" depends="make_jar">
    <!-- the following can be adjusted to your needs -->
    <obfuscate mainclass="${mainclass}" logfile="${obfuscationlog}" replaceclassnamestrings="true">
      <property name="error-checking" value="pedantic"/>
      <inoutpair in="${jar}" out="${obfjar}"/>
      <expose>
         <class classes="protected" methods="protected" fields="protected">
               <patternset>
                   <include name="com.devdaily.fptracker.**.*"/>
                   <!-- <exclude name="com.mycompany.publicapi.private.*"/> -->
               </patternset>
           </class>
       </expose>
    </obfuscate>
  </target>

</project>

Ant Swing build script - Discussion

If I were to ever try to sell a Java Swing application, I’d use yGuard, or a product like it, to obfuscate my Java code, and it’s really, really nice that they've created an obfuscate Ant task.

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.