18.18. Building a Scala Project with Ant

Problem

You want to use Ant to build your Scala project.

Solution

Assuming you have a Maven- and SBT-like project directory structure as described in Recipe 18.1, create the following Ant build.xml file in the root directory of your project:

<project name="AntCompileTest" default="compile" basedir=".">

  <!-- mostly from: http://www.scala-lang.org/node/98 -->

  <property name="sources.dir" value="src" />
  <property name="scala-source.dir" value="main/scala" />
  <property name="scala-test.dir" value="main/test" />
  <property name="build.dir" value="classes" />

  <!-- set scala.home -->
  <property environment="env" />
  <property name="scala.home" value="${env.SCALA_HOME}" />

  <target name="init">
    <property name="scala-library.jar"
              value="${scala.home}/lib/scala-library.jar" />
    <property name="scala-compiler.jar"
              value="${scala.home}/lib/scala-compiler.jar" />
    <property name="scala.reflect"
              value="${scala.home}/lib/scala-reflect.jar"/>
    <path id="build.classpath">
      <pathelement location="${scala-library.jar}" />
      <pathelement location="${build.dir}" />
    </path>
    <taskdef resource="scala/tools/ant/antlib.xml">
      <classpath>
        <pathelement location="${scala-compiler.jar}" />
        <pathelement location="${scala-library.jar}" />
        <pathelement location="${scala.reflect}"/>
      </classpath>
    </taskdef>
  </target>

  <target name="compile" depends="init">
    <mkdir dir="${build.dir}" />
    <scalac srcdir="${sources.dir}"
            destdir="${build.dir}"
            classpathref="build.classpath"
            deprecation="on">

Get Scala Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.