Skip to content

Instantly share code, notes, and snippets.

@Mark1626
Created November 14, 2018 18:30
Show Gist options
  • Save Mark1626/1aed392ee252aabf5530b409483fea0d to your computer and use it in GitHub Desktop.
Save Mark1626/1aed392ee252aabf5530b409483fea0d to your computer and use it in GitHub Desktop.
Basic Ant with ivy, junit, junit-report and jar creation
<project name="ProjectName"
xmlns:ivy="antlib:org.apache.ivy.ant"
default="build"
basedir=".">
<description>
Sample code
</description>
<!-- Ivy targets taken from http://ant.apache.org/ivy/history/latest-milestone/install.html -->
<property name="ivy.install.version" value="2.1.0-rc2"/>
<condition property="ivy.home" value="${env.IVY_HOME}">
<isset property="env.IVY_HOME"/>
</condition>
<property name="ivy.home" value="${user.home}/.ant"/>
<property name="ivy.jar.dir" value="${ivy.home}/lib"/>
<property name="ivy.jar.file" value="${ivy.jar.dir}/ivy.jar"/>
<target name="download-ivy" unless="offline">
<mkdir dir="${ivy.jar.dir}"/>
<!-- download Ivy from web site so that it can be used even without any special installation -->
<get src="https://repo1.maven.org/maven2/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar"
dest="${ivy.jar.file}" usetimestamp="true"/>
</target>
<target name="init-ivy" depends="download-ivy">
<!-- try to load Ivy here from Ivy home, in case the user has not already dropped
it into Ant's lib dir (note that the latter copy will always take precedence).
We will not fail as long as local lib dir exists (it may be empty) and
Ivy is in at least one of Ant's lib dir or the local lib dir. -->
<path id="ivy.lib.path">
<fileset dir="${ivy.jar.dir}" includes="*.jar"/>
</path>
<taskdef resource="org/apache/ivy/ant/antlib.xml"
uri="antlib:org.apache.ivy.ant" classpathref="ivy.lib.path"/>
</target>
<target name="resolve"
description="--> retrieve dependencies">
<ivy:retrieve/>
</target>
<path id="classpath.base" />
<property name="build.dir" value="build/" />
<property name="src.dir" value="src/" />
<property name="src.build.dir" value="build/src" />
<property name="test.dir" value="test/" />
<property name="test.build.dir" value="build/test"/>
<property name="dist.dir" value="dist/" />
<property name="report.dir" value="report" />
<property name="lib.dir" value="lib/" />
<path id="classpath.test">
<pathelement location="${lib.dir}/junit-4.12.jar" />
<pathelement location="${lib.dir}/hamcrest-core-1.3.jar" />
<pathelement location="${src.build.dir}"/>
<path refid="classpath.base" />
</path>
<target name="compile"
description="---> Compile your project"
depends="clean, resolve">
<mkdir dir="${src.build.dir}" />
<mkdir dir="${test.build.dir}" />
<javac srcdir="${src.dir}" destdir="${src.build.dir}" includeantruntime="false" />
<javac srcdir="${test.dir}" destdir="${test.build.dir}"
includeantruntime="false">
<classpath refid="classpath.test"/>
</javac>
</target>
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${report.dir}" />
<delete dir="${dist.dir}" />
</target>
<target name="build"
description="---> Build your project"
depends="compile">
<property name="main.path" value="class.path.to.main" />
<mkdir dir="${dist.dir}" />
<jar jarfile="${dist.dir}/ProjectName.jar" basedir="${build.dir}">
<manifest>
<attribute name="Manifest-Version" value="1.0" />
<attribute name="Main-Class" value="${main.path}" />
</manifest>
</jar>
</target>
<target name="test"
description="---> Test your project"
depends="compile">
<mkdir dir="${report.dir}" />
<junit haltonfailure="true">
<classpath>
<path refid="classpath.test" />
<pathelement location="${test.build.dir}" />
</classpath>
<formatter type="xml" />
<batchtest fork="true" todir="${report.dir}">
<fileset dir="${test.dir}" includes="**/*Test*.java" />
</batchtest>
</junit>
<junitreport todir="${report.dir}">
<fileset dir="${report.dir}">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${report.dir}/html" />
</junitreport>
</target>
</project>
@Mark1626
Copy link
Author

Usage

Find and replace the following

  • ProjectName - Name of your project
  • class.path.to.main - Path to your main function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment