Skip to content

Instantly share code, notes, and snippets.

@cris
Last active December 22, 2015 09:58
Show Gist options
  • Save cris/6455175 to your computer and use it in GitHub Desktop.
Save cris/6455175 to your computer and use it in GitHub Desktop.
Simple build.xml for toy java-projects, to have ability to unit-test it via command-line.
<?xml version="1.0" encoding="UTF-8"?>
<!--
build.xml with dead-simple project structure and ability to use JUnit.
This build.xml provides next directories structure:
.
├── build
│   ├── main
│   │   └── SomeClass.class
│   └── test
│   └── SomeClassTest.class
├── build.xml
├── lib
│   ├── junit.jar
│   └── some_other.jar
└── src
├── main
│   └── SomeClass.java
└── test
└── SomeClassTest.java
Put junit.jar and other required jars into lib/ folder.
Run with unit-tests:
$ ant
-->
<project name="NameForProject" basedir="." default="default">
<property name="build.dir" value="build"/>
<property name="src.dir" value="src"/>
<property name="main.dir" value="${src.dir}/main"/>
<property name="main.classes.dir" value="${build.dir}/main"/>
<property name="main.lib.dir" value="lib"/>
<property name="test.dir" value="${src.dir}/test"/>
<property name="test.classes.dir" value="${build.dir}/test"/>
<property name="test.lib.dir" value="lib"/>
<path id="main.classpath">
<pathelement location="${main.classes.dir}"/>
<fileset dir="${main.lib.dir}">
<include name="**/*.jar"/>
<exclude name="**/junit*.jar"/>
</fileset>
</path>
<path id="test.classpath">
<path refid="main.classpath"/>
<pathelement location="${test.classes.dir}"/>
<fileset dir="${test.lib.dir}">
<include name="**/junit*.jar"/>
</fileset>
</path>
<available file="${build.dir}" property="buildDirExists"/>
<target name="clean" description="remove all generated artifacts" if="buildDirExists">
<delete dir="${build.dir}" includeEmptyDirs="true"/>
</target>
<target name="create" description="create the build directories" unless="buildDirExists">
<mkdir dir="${main.classes.dir}"/>
<mkdir dir="${test.classes.dir}"/>
</target>
<target name="compile" description="compile all .java source files" depends="create">
<javac srcdir="${main.dir}" destdir="${main.classes.dir}" debug="on" includeantruntime="no">
<classpath refid="main.classpath"/>
<include name="**/*.java"/>
<exclude name="**/*Test.java"/>
</javac>
<javac srcdir="${test.dir}" destdir="${test.classes.dir}" debug="on" includeantruntime="no">
<classpath refid="test.classpath"/>
<include name="**/*Test.java"/>
</javac>
</target>
<target name="test" description="run all JUnit tests" depends="compile">
<junit printsummary="on" haltonfailure="yes" showoutput="yes">
<classpath refid="test.classpath"/>
<formatter type="brief" usefile="false" />
<batchtest fork="yes">
<fileset dir="${test.dir}">
<include name="**/*Test.java"/>
</fileset>
</batchtest>
</junit>
</target>
<target name="default" depends="test" />
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment