Saturday, May 12, 2012

Compiling Scala with Ant


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>

By Jakob Jenkov

Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

0 comments: on "Compiling Scala with Ant"

Post a Comment