User:Nat/Ant Build File

From RoboWiki
Jump to: navigation, search

Just start using ant recently. I now come up with my own ant build file base on two example files on the old wiki. Have fun!


Directory structure
  • /bin - this will store all of classes files
  • /src - this will store all of source files
  • /resource - contain all the resources you want to include in .jar in the same structure
  • /backups - contain each and every build of your code.
  • basedir - in <project> tag - must set to your working directory thatcontains the structure
  • In task "init"
    • - robot classname
    • robot.package - robot package name
    • robot.propertiesfile - path to property file to created
    • ...


  • Automatically packed only required file
  • Deploy to robots folder automatically
  • Automatically backup every builds.
  • Obfuscate the JAR file so no one can take your code by decompiler.
  • Very easy configuration (I think)
  • Automatically pack resources if any.

Note that this build file create the closed-source robot, if you are open sources, you may have to edit it a bit to use the backup files instead (the backup file will include everything in your project). But for someone want to publish their source code but want to obfuscate the jar file too like me, you can use this build file and publish the source code through other context.

XML File

Because color make you memorization better by 72%, so... (but I can't disabled the auto line break TT) <?xml version="1.0" encoding="utf-8" ?>
<!-- ======================================================

                      ANT build file
                       Nat Pavasant

 Directory structure:
  - /bin - this will store all of classes files
  - /src - this will store all of source files
  - /resource - contain all the resources you want to include 
                in .jar in the same structure
  - /backups - contain each and every build of your code.
  - Pack task (
  - yGuard Obfuscator (
  - CodeSize version 1.0 (version 1.1 come with robocode now doesn't have valid entry point)
  - basedir - in <project> tag - must set to your working directory that
                                 contains the structure
  - In task 'init'
    - - robot classname
    - robot.package - robot package name
    - robot.propertiesfile - path to property file to created
    - ...
   - David Alves
   - Hexkid

======================================================= -->

<project name="Robot" default="build" basedir=".">
        Build File

    <!-- define a task use in this build file -->
    <taskdef name="yguard" classname="com.yworks.yguard.YGuardTask" classpath="./yguard.jar" />
    <taskdef name="pack" classname="org.sadun.util.ant.Pack" classpath="./pack.jar" />

    <!-- Initialize the config, all properties must be here -->
    <target name="init" description="Initialize properties to build">
            <!-- configure time string use for backup version -->
            <format property="timestamp" pattern="yyyyMMdHHmmss" locale="en,US" timezone="UTC" />

        <!-- Start configuration setting -->
        <property name="" value="Your Robot Name" />
        <property name="robot.package" value="Your Package Name" />

        <!-- Set the path to your properties file inside .jar, please make sure 
             that the specific directory is available under ./resource/ directory -->

        <property name="robot.propertiesfile" value="Your package name/${}.properties" />

        <!-- Create the property file that describes this robot -->
        <propertyfile file="resource/${robot.propertiesfile}">
            <entry key="robot.description" value="" />
            <entry key="" value="false" />
            <entry key="robocode.version" value="" />
            <entry key="robot.version" value="0.1" />
            <entry key="" value="Your Name" />
            <entry key="robot.classname" value="${robot.package}.${}" />

        <property file="resource/${robot.propertiesfile}" description="Use the entries from the properties file within this script." />
        <property name="robocode.home" value="C:\robocode" />
        <property name="robocode.jar" location="${robocode.home}/libs/robocode.jar" />
        <property name="codesize.jar" location="./codesize.jar" />
        <property name="class.path" value="./bin;${robocode.jar}" />

        <property name="COMPILECLASSNAME" value="" />
        <property name="COMPILE_WITH_DEBUG" value="off" />
        <property name="VERBOSE_LEVEL" value="1" />

        <property name="backup.file" location="target/${}_${robot.version}_${timestamp}.jar" />
        <property name="release.file" location="target/${robot.classname}_${robot.version}.jar" />
        <property name="temp.file" location="target/${}_temp.jar" />

    <!-- Clean up the build -->
    <target name="clean" depends="init" description="Remove the directories containing the classes and the jar file">
        <delete dir="bin" />
        <delete dir="target" />

    <target name="prepare" depends="init" description="Create the generated directories">
        <mkdir dir="bin" />
        <mkdir dir="target" />

    <target name="compile" depends="prepare" description="Compile the Java classes">
        <javac srcdir="src" destdir="bin" classpath="${class.path}" debug="${COMPILE_WITH_DEBUG}" includes="**/*.java" />

    <target name="buildjar" depends="compile" description="Make .jar file">
        <delete file="${backup.file}" failonerror="false" />
        <delete file="${release.file}" failonerror="false" />
        <delete file="${temp.file}" failonerror="false" />

        <jar duplicate="add" destfile="${backup.file}" compress="true">
            <fileset dir="bin" includes="**/*.class" />
            <fileset dir="src" includes="**/*.java" />
            <fileset dir="resource" includes="**/*" />

        <pack classes="${robot.classname}" targetjar="${temp.file}" excludepkg="java,javax,sun,robocode">
            <classpath path="./bin" />
            <classpath location="${robocode.jar}" />
            <additionalfileset dir="resource" includes="**/*" />

        <!-- Print codesize-->
        <java fork="yes" jar="${codesize.jar}">
            <arg value="${temp.file}" />

        <!-- Obfuscate release jar -->
            <inoutpair in="${temp.file}" out="${release.file}" />

                <pathelement location="${robocode.jar}" />

            <rename logfile="obfuscate.log" replaceclassnamestrings="true">

                <property name="overload-enabled" value="false" />
                <property name="naming-scheme" value="mix" />
                <!-- You may set this to 'compatible' instead -->
                <property name="language-conformity" value="illegal" />

                    <class name="${robot.classname}" methods="public" fields="none" />

    <target name="deploy" description="Deploy .jar file to robocode directory and make a backup" depends="buildjar">
        <copy file="${release.file}" overwrite="true" todir="${robocode.home}/robots" />
        <copy file="${backup.file}" overwrite="true" todir="./backups" />

    <target name="cleanup" description="Cleanup .jar in target directory">
        <delete file="${backup.file}" failonerror="false" />
        <delete file="${release.file}" failonerror="false" />
        <delete file="${temp.file}" failonerror="false" />

    <target name="build" depends="clean,deploy" description="Automatically build file and cleanup"></target>

Personal tools