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="local-environment.properties"/>

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

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


   <macrodef name="scalac" >
       <attribute name="class"
                     default="src/com/jenkov/scala/*.scala"/>
       <attribute name="output"
                     default="build"/>
       <attribute name="sourcepath"
                     default="c:\data\projects\scala\src"/>
       <sequential>
           <exec executable="${scala-home}\bin\scalac.bat">
               <arg value="-d"/>
               <arg value="@{output}"/>
               <arg value="-sourcepath"/>
               <arg value="@{sourcepath}"/>
               <arg value="@{class}"/>
           </exec>
       </sequential>
   </macrodef>


   <macrodef name="scala">
       <attribute name="class" />
       <attribute name="classpath"
                     default="c:\data\projects\scala\build"/>

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

   </macrodef>


   <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>


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


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


</project>

Jakob Jenkov

Featured Videos

Java ConcurrentMap + ConcurrentHashMap

Java Generics

Java ForkJoinPool

P2P Networks Introduction

















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
Previous
Next tutorial in this tutorial trail
Next