User:Nat/Ant Build File

From Robowiki
Jump to navigation Jump to 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!

Documentation

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.
Dependancies
Configuration
  • basedir - in <project> tag - must set to your working directory thatcontains the structure
  • In task "init"
    • robot.name - robot classname
    • robot.package - robot package name
    • robot.propertiesfile - path to property file to created
    • ...
Credit

Features

  • 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.
  
 Dependancies:
  - Pack task (http://sadun-util.sourceforge.net/pack.html)
  - yGuard Obfuscator (http://www.yworks.com/en/products_yguard_about.htm)
  - CodeSize version 1.0 (version 1.1 come with robocode now doesn't have valid entry point)
 
 Configuration:
  - basedir - in <project> tag - must set to your working directory that
                                 contains the structure
  - In task 'init'
    - robot.name - robot classname
    - robot.package - robot package name
    - robot.propertiesfile - path to property file to created
    - ...
  
  Credit:
   - David Alves
   - Hexkid

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

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

    <!-- 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">
        <tstamp>
            <!-- configure time string use for backup version -->
            <format property="timestamp" pattern="yyyyMMdHHmmss" locale="en,US" timezone="UTC" />
        </tstamp>

        <!-- Start configuration setting -->
        <property name="robot.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/${robot.name}.properties" />

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

        <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.name}_${robot.version}_${timestamp}.jar" />
        <property name="release.file" location="target/${robot.classname}_${robot.version}.jar" />
        <property name="temp.file" location="target/${robot.name}_temp.jar" />
    </target>

    <!-- 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>

    <target name="prepare" depends="init" description="Create the generated directories">
        <mkdir dir="bin" />
        <mkdir dir="target" />
    </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>

    <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="**/*" />
        </jar>

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

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

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

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

            <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" />

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

    <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>

    <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>

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