Ant (tool)

From Robowiki
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:
	     put it in your ANT_HOME/lib
	<taskdef name="pack" classname="org.sadun.util.ant.Pack" />
	     Find it here:
	     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="" value="Nene" />
	<property name="obfuscate" value="false" />
	<property name="addsource" value="true" />
	<property name="build.version" value="1.5" />
	<property name="jardir" value="." />
	<property name="srcdir" value="." />
	<property name="bindir" value="." />
	<!-- Comment this out if you don't want to use the Eclipse Compiler for Java -->
	<!-- <property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/> -->
	<!-- Load version number from robot properties file -->
	<property file="${srcdir}/${robot.classpath}/${}.properties" />
	<property name="robocode.jar" location="C:/robocode/libs/robocode.jar" />
	<!-- Things to exclude from your obfuscation -->
	<patternset id="obfuscate.exclude">
		<include name="ags.**" />
		<include name="wiki.**" />
	<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
	<!-- !!!!!!!!!!!!!!!  NO CONFIGURATION BEYOND THIS POINT  !!!!!!!!!!!!!!!!!!!! -->
	<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
	<!-- Compile the source java to class files -->
	<target name="compile">
		<!-- Delete all the current files so we can recompile with our compiler. -->
			<fileset dir="${basedir}" includes="**/*.class"/>
		<!-- Compile with no debugging info -->
		<javac srcdir="${srcdir}" destdir="${bindir}" includeAntRuntime="no" encoding="UTF-8"
		 	target="${build.version}" source="${build.version}" debug="off">
				<pathelement path="${basedir}"/>
				<pathelement location="${robocode.jar}"/>

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

		<!-- Setup directories and filenames -->
		<mkdir dir="${jardir}"/>
		<property name="backup.file" location="${jardir}/${robot.classname}_${robot.version}_${LONGSTAMP}.jar" />
		<property name="release.file" location="${jardir}/${robot.classname}_${robot.version}.jar" />
		<property name="tmp.file" location="${jardir}/~${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="${srcdir}" destfile="${backup.file}" compress="true">
			<include name="**/*.java" />
			<include name="**/*.properties" />
		<jar duplicate="add" basedir="${bindir}" destfile="${backup.file}" update="true" compress="true">
			<include name="**/*.class" />
		<!-- "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 -->
		<!-- -->
		<pack classes="${robot.classname}" targetjar="${tmp.file}" excludepkg="java,javax,sun,robocode">
			<classpath path="${bindir}" />
			<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="${srcdir}">
				<filename name="**/*.properties" />

		<antcall target="includeSource" />
		<antcall target="doObfuscate" />
		<antcall target="dontObfuscate" />
		<!-- Cleanup your unneeded tmp jar -->
		<delete file="${tmp.file}" failonerror="false" />
	<!-- Include source in release jar. -->
	<target name="includeSource" if="${addsource}" unless="${obfuscate}">
		<echo>Adding source to output jar.</echo>
		<jar duplicate="add" basedir="${srcdir}" destfile="${tmp.file}" update="true" compress="true">
			<include name="**/*.java" />
			<include name="**/*.properties" />
	<!-- Skip Obfuscation of release jar. -->
	<target name="dontObfuscate" unless="${obfuscate}">
		<echo>Skipping Obfuscation</echo>
		<move file="${tmp.file}" tofile="${release.file}" />
	<!-- Obfuscate release jar. -->
	<!-- -->
	<target name="doObfuscate" if="${obfuscate}">
		<echo>Obfuscating Jar</echo>
			<inoutpair in="${tmp.file}" out="${release.file}" />
				<pathelement location="${robocode.jar}"/>
				<property name="naming-scheme" value="small" />
				<property name="language-conformity" value="illegal" />
				<property name="overload-enabled" value="true" />
					<class name="${robot.classname}" methods="public" />
						<patternset refid="obfuscate.exclude" />