Difference between revisions of "Ant (tool)"

From Robowiki
Jump to navigation Jump to search
m (moved Ant to Ant (tool): There is an Ant robot, too.)
m
 
(5 intermediate revisions by the same user not shown)
Line 19: Line 19:
 
<taskdef name="yguard" classname="com.yworks.yguard.YGuardTask" />
 
<taskdef name="yguard" classname="com.yworks.yguard.YGuardTask" />
  
<!-- --------------------------------------------- -->
 
 
<!-- Your settings, all you need to change is here -->
 
<!-- Your settings, all you need to change is here -->
 
<description>SkunkWorks Ant Build Script</description>
 
<description>SkunkWorks Ant Build Script</description>
<property name="robot.classpath" value="chase" />
+
<property name="robot.classpath" value="cs" />
<property name="robot.name" value="Seraphim" />
+
<property name="robot.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}/${robot.name}.properties" />
 
<property name="robocode.jar" location="C:/robocode/libs/robocode.jar" />
 
<property name="robocode.jar" location="C:/robocode/libs/robocode.jar" />
 
<!-- Comment this out if you don't want to/cannot use the Eclipse Compiler for Java (ECJ) -->
 
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
 
<!-- --------------------------------------------- -->
 
<!-- --------------------------------------------- -->
 
 
 
<!-- Load version number from robot properties file -->
+
<!-- Things to exclude from your obfuscation -->
<property file="${robot.classpath}/${robot.name}.properties" />
+
<patternset id="obfuscate.exclude">
 +
<include name="ags.**" />
 +
<include name="wiki.**" />
 +
</patternset>
 +
 +
<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
 +
<!-- !!!!!!!!!!!!!!!  NO CONFIGURATION BEYOND THIS POINT  !!!!!!!!!!!!!!!!!!!! -->
 +
<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
 
 
 
<!-- Compile the source java to class files -->
 
<!-- Compile the source java to class files -->
Line 38: Line 52:
 
<!-- Delete all the current files so we can recompile with our compiler. -->
 
<!-- Delete all the current files so we can recompile with our compiler. -->
 
<delete>
 
<delete>
<fileset dir="${robot.classpath}" includes="**/*.class"/>
+
<fileset dir="${basedir}" includes="**/*.class"/>
 
</delete>
 
</delete>
 
 
 
<!-- Compile with no debugging info -->
 
<!-- Compile with no debugging info -->
<javac srcdir="." destdir="." includeAntRuntime="no"
+
<javac srcdir="${srcdir}" destdir="${bindir}" includeAntRuntime="no" encoding="UTF-8"
target="1.5" source="1.5" debug="off">
+
target="${build.version}" source="${build.version}" debug="off">
 
<classpath>
 
<classpath>
 
<pathelement path="${basedir}"/>
 
<pathelement path="${basedir}"/>
Line 57: Line 71:
 
</tstamp>
 
</tstamp>
  
<property name="backup.file" location="${robot.classname}_${robot.version}_src_${LONGSTAMP}.jar" />
+
<!-- Setup directories and filenames -->
<property name="release.file" location="${robot.classname}_${robot.version}.jar" />
+
<mkdir dir="${jardir}"/>
<property name="tmp.file" location="~${robot.version}.jar" />
+
<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 older copies of your files -->
Line 66: Line 82:
  
 
<!-- jar takes all your java and class files and packs them togeather -->
 
<!-- jar takes all your java and class files and packs them togeather -->
<jar duplicate="add" basedir="." destfile="${backup.file}" compress="true">
+
<jar duplicate="add" basedir="${srcdir}" destfile="${backup.file}" compress="true">
<include name="${robot.classpath}/**/*.java" />
+
<include name="**/*.java" />
<include name="${robot.classpath}/**/*.class" />
+
<include name="**/*.properties" />
<include name="${robot.classpath}/**/*.properties" />
+
</jar>
 +
<jar duplicate="add" basedir="${bindir}" destfile="${backup.file}" update="true" compress="true">
 +
<include name="**/*.class" />
 
</jar>
 
</jar>
 
 
Line 76: Line 94:
 
<!-- http://sadun-util.sourceforge.net/pack.html -->
 
<!-- http://sadun-util.sourceforge.net/pack.html -->
 
<pack classes="${robot.classname}" targetjar="${tmp.file}" excludepkg="java,javax,sun,robocode">
 
<pack classes="${robot.classname}" targetjar="${tmp.file}" excludepkg="java,javax,sun,robocode">
<classpath path="." />
+
<classpath path="${bindir}" />
 
<classpath location="${robocode.jar}" />
 
<classpath location="${robocode.jar}" />
 
<!-- A bit of magic so we don't have to type out our property files.
 
<!-- A bit of magic so we don't have to type out our property files.
 
    Bonus, it includes those in the data folder aswell. -->
 
    Bonus, it includes those in the data folder aswell. -->
<additionalfileset dir=".">
+
<additionalfileset dir="${srcdir}">
 
<filename name="**/*.properties" />
 
<filename name="**/*.properties" />
 
</additionalfileset>
 
</additionalfileset>
 
</pack>
 
</pack>
  
<!-- Print codesize-->
+
<antcall target="includeSource" />
<!-- This requires an executable version of codesize, newer robocode versions don't have it.
+
<antcall target="doObfuscate" />
    if you want this, an executable version is here: http://user.cs.tu-berlin.de/~lulli/codesize/index.html -->
+
<antcall target="dontObfuscate" />
<!--
+
<java fork="yes" jar="C:/robocode/libs/codesize.jar">
+
<!-- Cleanup your unneeded tmp jar -->
<arg value="${tmp.file}" />
+
<delete file="${tmp.file}" failonerror="false" />
</java>
+
</target>
-->
+
 
+
<!-- Include source in release jar. -->
<!-- Obfuscate release jar -->
+
<target name="includeSource" if="${addsource}" unless="${obfuscate}">
<!-- http://www.yworks.com/products/yguard/yguard_ant_howto.html -->
+
<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" />
 +
</jar>
 +
</target>
 +
 +
<!-- Skip Obfuscation of release jar. -->
 +
<target name="dontObfuscate" unless="${obfuscate}">
 +
<echo>Skipping Obfuscation</echo>
 +
<move file="${tmp.file}" tofile="${release.file}" />
 +
</target>
 +
 +
<!-- Obfuscate release jar. -->
 +
<!-- http://www.yworks.com/products/yguard/yguard_ant_howto.html -->
 +
<target name="doObfuscate" if="${obfuscate}">
 +
<echo>Obfuscating Jar</echo>
 
<yguard>
 
<yguard>
 
<inoutpair in="${tmp.file}" out="${release.file}" />
 
<inoutpair in="${tmp.file}" out="${release.file}" />
Line 107: Line 141:
 
<keep>
 
<keep>
 
<class name="${robot.classname}" methods="public" />
 
<class name="${robot.classname}" methods="public" />
 
<!-- Put here the classes you don't want to obfuscate -->
 
 
<class>
 
<class>
<patternset>
+
<patternset refid="obfuscate.exclude" />
<include name="ags.utils.*" />
 
<include name="org.csdgn.*" />
 
<include name="wiki.*" />
 
</patternset>
 
 
</class>
 
</class>
 
</keep>
 
</keep>
 
</rename>
 
</rename>
 
</yguard>
 
</yguard>
 
<!-- Cleanup your unneeded tmp jar -->
 
<delete file="${tmp.file}" failonerror="false" />
 
 
</target>
 
</target>
 
</project>
 
</project>
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 01:09, 30 August 2011

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="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}/${robot.name}.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.**" />
	</patternset>
	
	<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
	<!-- !!!!!!!!!!!!!!!  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. -->
		<delete>
			<fileset dir="${basedir}" includes="**/*.class"/>
		</delete>
		
		<!-- Compile with no debugging info -->
		<javac srcdir="${srcdir}" destdir="${bindir}" includeAntRuntime="no" encoding="UTF-8"
		 	target="${build.version}" source="${build.version}" 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>

		<!-- 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>
		<jar duplicate="add" basedir="${bindir}" destfile="${backup.file}" update="true" compress="true">
			<include name="**/*.class" />
		</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="${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" />
			</additionalfileset>
		</pack>

		<antcall target="includeSource" />
		<antcall target="doObfuscate" />
		<antcall target="dontObfuscate" />
		
		<!-- Cleanup your unneeded tmp jar -->
		<delete file="${tmp.file}" failonerror="false" />
	</target>
	
	<!-- 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" />
		</jar>
	</target>
	
	<!-- Skip Obfuscation of release jar. -->
	<target name="dontObfuscate" unless="${obfuscate}">
		<echo>Skipping Obfuscation</echo>
		<move file="${tmp.file}" tofile="${release.file}" />
	</target>
	
	<!-- Obfuscate release jar. -->
	<!-- http://www.yworks.com/products/yguard/yguard_ant_howto.html -->
	<target name="doObfuscate" if="${obfuscate}">
		<echo>Obfuscating Jar</echo>
		<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" />
					<class>
						<patternset refid="obfuscate.exclude" />
					</class>
				</keep>
			</rename>
		</yguard>
	</target>
</project>