Ant (tool)

From Robowiki
Revision as of 09:10, 5 August 2010 by Chase-san (talk | contribs) (Adding a short descripting from the old wiki and a new (better) example script.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Apache Ant is a Java-based build tool, similar to a make file. It allows you to automate the procedure of compiling and packaging your Java code. It is an open source tool and is available at Ant Home Page and is produced by Apache Software Foundation who develop a whole collection of top-class open-source projects.

Example Script

Example ant script for robocode: If you are Ant saavy enough you can change the other settings to your liking.

<?xml version="1.0" encoding="utf-8" ?>
<project name="SkunkWorks" default="package" basedir="." >
	<!--
	     Find it here: http://sourceforge.net/projects/sadun-util/
	     put it in your ANT_HOME/lib
	 -->
	<taskdef name="pack" classname="org.sadun.util.ant.Pack" />
	
	<!--
	     Find it here: http://www.yworks.com/en/products_yguard_about.html
	     put it in your ANT_HOME/lib
	 -->
	<taskdef name="yguard" classname="com.yworks.yguard.YGuardTask" />

	<!-- --------------------------------------------- -->
	<!-- Your settings, all you need to change is here -->
	<description>SkunkWorks Ant Build Script</description>
	<property name="robot.classpath" value="cs" />
	<property name="robot.name" value="Kitsune" />
	<property name="robocode.jar" location="C:/robocode/libs/robocode.jar" />

	<!-- Comment this out if you don't want to use the Eclipse Compiler for Java (ECJ) -->
	<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
	<!-- --------------------------------------------- -->
	<!-- --------------------------------------------- -->
	
	<!-- Load version number from robot properties file -->
	<property file="${robot.classpath}/${robot.name}.properties" />
	
	<!-- Compile the source java to class files -->
	<target name="compile">
		<!-- Delete all the current files so we can recompile with our compiler. -->
		<delete>
			<fileset dir="${robot.classpath}" includes="**/*.class"/>
		</delete>
		
		<!-- Compile with no debugging info -->
		<javac srcdir="." destdir="." includeAntRuntime="no"
		 target="1.5" source="1.5" debug="off">
			<classpath>
				<pathelement path="${basedir}"/>
				<pathelement location="${robocode.jar}"/>
			</classpath>
		</javac>
	</target>

	<!-- Package the robot for upload. -->
	<target name="package" depends="compile">
		<tstamp>
			<format property="LONGSTAMP" pattern="yyyyMMdd-HHmm" locale="en"/>
		</tstamp>

		<property name="backup.file" location="${robot.classname}_${robot.version}_src_${LONGSTAMP}.jar" />
		<property name="release.file" location="${robot.classname}_${robot.version}.jar" />
		<property name="tmp.file" location="~${robot.version}.jar" />

		<!-- delete older copies of your files -->
		<delete file="${backup.file}" failonerror="false" />
		<delete file="${release.file}" failonerror="false" />

		<!-- jar takes all your java and class files and packs them togeather -->
		<jar duplicate="add" basedir="." destfile="${backup.file}" compress="true">
			<include name="${robot.classpath}/**/*.java" />
			<include name="${robot.classpath}/**/*.class" />
			<include name="${robot.classpath}/**/*.properties" />
		</jar>
		
		<!-- "pack" task selects only the minimal set of classes needed to run the bot. 
			 This can also be done with the "classfileset" task from ant-contrib -->
		<!-- http://sadun-util.sourceforge.net/pack.html -->
		<pack classes="${robot.classname}" targetjar="${tmp.file}" excludepkg="java,javax,sun,robocode">
			<classpath path="." />
			<classpath location="${robocode.jar}" />
			<!-- A bit of magic so we don't have to type out our property files.
			     Bonus, it includes those in the data folder aswell. -->
			<additionalfileset dir=".">
				<filename name="**/*.properties" />
			</additionalfileset>
		</pack>

		<!-- Print codesize-->
		<!-- This requires an executable version of codesize, newer robocode versions don't have it.
		     if you want this, an executable version is here: http://user.cs.tu-berlin.de/~lulli/codesize/index.html -->
	<!--
		<java fork="yes" jar="C:/robocode/libs/codesize.jar">
			<arg value="${tmp.file}" />
		</java>
	-->

		<!-- Obfuscate release jar -->
		<!-- http://www.yworks.com/products/yguard/yguard_ant_howto.html -->
		<yguard>
			<inoutpair in="${tmp.file}" out="${release.file}" />
			<externalclasses>
				<pathelement location="${robocode.jar}"/>
			</externalclasses>
			<rename>
				<property name="naming-scheme" value="small" />
				<property name="language-conformity" value="illegal" />
				<property name="overload-enabled" value="true" />
				<keep>
					<class name="${robot.classname}" methods="public" />
					
					<!-- Put here the classes you don't want to obfuscate -->
					<class>
						<patternset>
							<include name="ags.utils.*" />
							<include name="org.csdgn.*" />
							<include name="wiki.*" />
						</patternset>
					</class>
				</keep>
			</rename>
		</yguard>
		
		<!-- Cleanup your unneeded tmp jar -->
		<delete file="${tmp.file}" failonerror="false" />
	</target>
</project>