Scala With Ant


Although Scala is tool-agnostic, the Scala community seems to have a preference for the Apache Maven build tool. I am not quite sure why. Given that most people who learn Scala come from a Java background, what could be more natural than using Ant for Scala development? As a tried-and-true build solution, Ant avoids the headaches and additional learning curve that comes with an unfamiliar build tool. Although I have spent considerable time with Maven as part of my job, Maven and I haven’t become friends, and we probably never will. I won’t go into the details of the how and why. Instead I’m going to present and explain a few Ant scripts for Scala development. If you are new to Scala, I hope that this will give you a headstart with your new Scala projects. After all, you have a new language to learn and little time to bother with tools.

<project default="run" name="helloworld">

<!-- root directory of this project -->
<property name="project.dir" value=".">

<!-- main class to run -->
<property name="main.class" value="app.HelloWorld">

<!-- root directory of Scala installation -->
<property name="scala.home"
  value="C:\\Program Files\\Scala">

<!-- location of scalatest.jar for unit testing -->
<property name="scalatest.jar"
  value="C:\\Program Files\\...\\scalatest-1.0.jar">

<target name="init">

  <!-- derived path names -->
  <property name="build.dir" value="${project.dir}/build">
  <property name="source.dir" value="${project.dir}/src">
  <property name="test.dir" value="${project.dir}/test">

  <!-- scala libraries for classpath definitions -->
  <property name="scala-library.jar"
  <property name="scala-compiler.jar"

  <!-- classpath for the compiler task definition -->
  <path id="scala.classpath">
    <pathelement location="${scala-compiler.jar}">
    <pathelement location="${scala-library.jar}">

  <!-- classpath for project build -->
  <path id="build.classpath">
    <pathelement location="${scala-library.jar}">
    <fileset dir="${project.dir}/lib">
      <include name="*.jar">
    <pathelement location="${build.dir}/classes">

  <!-- classpath for unit test build  -->
  <path id="test.classpath">
    <pathelement location="${scala-library.jar}">
    <pathelement location="${scalatest.jar}">
    <pathelement location="${build.dir}/classes">

  <!-- definition for the "scalac" and
    "scaladoc" ant tasks -->
  <taskdef resource="scala/tools/ant/antlib.xml">
    <classpath refid="scala.classpath">

  <!-- definition for the "scalatest" ant task -->
  <taskdef classname=""
    <classpath refid="test.classpath">


<!-- delete compiled files -->
<target depends="init" description="clean" name="clean">
  <delete dir="${build.dir}">
  <delete dir="${project.dir}/doc">
  <delete file="${project.dir}/lib/scala-library.jar">

<!-- compile project -->
<target depends="init" description="build" name="build">
  <mkdir dir="${build.dir}/classes">
  <scalac classpathref="build.classpath" deprecation="on"
    force="never" srcdir="${source.dir}">
    <include name="**/*.scala">

<!-- run program -->
<target depends="build" description="run" name="run">
  <java classname="${main.class}" classpathref="build.classpath">

<!-- build unit tests -->
<target depends="build" name="buildtest">
  <mkdir dir="${build.dir}/test">
  <scalac classpathref="test.classpath" deprecation="on"
    destdir="${build.dir}/test" force="never" srcdir="${test.dir}">
    <include name="**/*.scala">

<!-- run unit tests -->
<target depends="buildtest" description="test" name="test">
  <scalatest runpath="${build.dir}/test">
    <reporter config="YFABRT" type="stdout">
    <membersonly package="suite">
    <!-- <reporter type="graphic" config="YFABRT"/> -->
    <!-- <suite classname="suite.TestSuite"/> -->

<!-- create a startable *.jar with proper
  classpath dependency definition -->
<target depends="build" description="jar" name="jar">
  <mkdir dir="${build.dir}/jar">
  <copy file="${scala-library.jar}" todir="${project.dir}/lib">
  <path id="jar.class.path">
    <fileset dir="${project.dir}">
      <include name="lib/**/*.jar">
  <pathconvert dirsep="/" pathsep=" " property="jar.classpath">
    <path refid="jar.class.path"></path>
    <map from="${basedir}${file.separator}lib" to="lib">
  <jar basedir="${build.dir}/classes"
      <attribute name="Main-Class" value="${main.class}">
      <attribute name="Class-Path" value="${jar.classpath}">
      <section name="Program">
        <attribute name="Title" value="${}">
        <attribute name="Build" value="${build.number}">
        <attribute name="Date" value="${TODAY}">

<!-- create API documentation in doc folder -->
<target depends="build" description="scaladoc"
  <mkdir dir="${project.dir}/doc">
  <scaladoc classpathref="build.classpath"
    doctitle="${}" srcdir="${source.dir}"

<!-- create a zip file with binaries for distribution -->
<target depends="jar, scaladoc"
  description="package" name="package">
  <zip destfile="${build.dir}/${}.zip">
    <zipfileset dir="${build.dir}/jar"
    <zipfileset dir="${project.dir}" includes="lib/*"/>
    <zipfileset dir="${project.dir}" includes="doc/*"/>
    <zipfileset dir="${project.dir}/txt" includes="*"/>


Download "helloworld.scala" with Ant build file. Fortunately, the designers of Scala provided built-in Ant tasks in the compiler library, which makes the integration of Ant straightforward. The XML code above shows the build.xml for a typical Scala standalone application, such as a GUI program or a console application. It is reasonably complete and it provides Ant targets for all common tasks:

  • build = compile your project and put class files into the ./build directory.
  • run = run program.
  • jar = create a startable jar archive from class files.
  • test = build and run unit tests (using Scalatest).
  • scaladoc = create API documentation from sources and put it into the in ./doc directory.
  • package = create a distributable zip archive that contains all dependencies plus Scaladocs.

The directory tree is kept as simple as possible:

|--- build
|--- lib
|--- src
|    |--- app
|--- test
|    |--- suite
|--- txt
  • build - temporary directory for class files and other binaries
  • lib - contains external dependencies (*.jar files)
  • src - contains Scala source code tree
  • test - contains Scala unit tests
  • txt - contains files (such as license text) that are packaged with the distributable zip archive

If your project depends on external jars, all you have to do is to drop these into the ./lib folder. Ant will take care of creating appropriate class paths, entering them into the manifest of the startable jar file, and including them in the distribution package. The Scala library is packed automatically so that the resulting package can be deployed on computers that don’t have Scala installed (only a standard JRE is required). To use this Ant script on your computer, you need to change the values of the scala.home and scalatest.jar properties to match the location of your Scala and Scalatest installations (never mind the latter if you use another unit testing framework).

If you are working in a team, it is good practice to put Ant properties into a separate properties file, so that the build file can be checked into a source code management tool. I have included them here for simplicity. One of the advantages of using Ant is that it has very good integration with IDEs, such as Eclipse. The only twist is that Eclipse does not honour the classpaths defined in build.xml for its own incremental compilation process used to mark syntactical errors. Thus if you import the Ant project into Eclipse, you have to tell Eclipse about the dependencies using the Project/Properties/Java Build Path option.