Compiling Scala with Ant

Jakob Jenkov
Last update: 2014-05-25

You can use Apache Ant to compile your Scala code. In this text I will show you how I have done it. It is probably not the only way to do it, but it is at least one way.

Here is the full Ant script. It consists of 4 targets:

  1. prepare
  2. build
  3. run
  4. buid-run

It should be pretty self evident what these targets do. I am not going to explain the script in detail. You need to know Apache Ant reasonably to understand it, but then I also think the script will explain itself pretty much.

<?xml version="1.0"?>

<project name="scala-tests" default="build-run" basedir=".">

    <!-- ======================================================== -->
    <!-- Initialization of all property settings                  -->
    <!-- ======================================================== -->
        <property file=""/>

        <property name="name"            value="tutorials"/>

   <target name="prepare">
       <mkdir dir="build"/>

   <macrodef name="scalac" >
       <attribute name="class"
       <attribute name="output"
       <attribute name="sourcepath"
           <exec executable="${scala-home}\bin\scalac.bat">
               <arg value="-d"/>
               <arg value="@{output}"/>
               <arg value="-sourcepath"/>
               <arg value="@{sourcepath}"/>
               <arg value="@{class}"/>

   <macrodef name="scala">
       <attribute name="class" />
       <attribute name="classpath"

           <exec executable="${scala-home}\bin\scala.bat">
             <arg value="-classpath"/>
             <arg value="@{classpath}"/>
             <arg value="@{class}"/>
             <!--<arg value="-classpath build "/>-->
             <!--<arg value="@{class}"/>-->


   <target name="build" >
       <antcall target="prepare"/>

       <-- you need 1 line (below) per scala source directory,
              including one for each package directory. -->
       <scalac class="src/com/jenkov/scala/*.scala"/>
       <scalac class="src/com/jenkov/scala/sub/*.scala"/>

   <target name="run">
       <!--<scala class="HelloWorld"/>-->
       <scala class="com.jenkov.scala.Main"/>

   <target name="build-run">
       <antcall target="build"/>
       <antcall target="run"/>


Jakob Jenkov

Featured Videos

Java ForkJoinPool

P2P Networks Introduction


Java Persistence
Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Next tutorial in this tutorial trail