Sunday 28 December 2008

Scala, Ant

An example of building Scala applications using Ant and the Scala Ant tasks:

In the complete Ant build file given below, the key lines are:


<taskdef resource="scala/tools/ant/antlib.xml">
<classpath>
<pathelement location="${scala.home}/lib/scala-compiler.jar" />
<pathelement location="${scala-library.jar}" />
</classpath>
</taskdef>
Which loads the task definition

scala-library.jar defines Scala tasks for Ant, these are:
  • scalac
  • fsc
  • scaladoc

Note: The Scala library scala-library.jar must be present in the Java class path. The actors and dbc libraries are available as separate JAR files: scala-actors.jarand scala-dbc.jar.


Example build.xml

// LJB 28-12-2008 - basic scala Ant build
<project name="Scala test" default="build" basedir="../">

<property name="base.dir" value="${basedir}" />
<property name="sources.dir" value="${base.dir}/src" />
<property name="build.dir" value="${base.dir}/build//classes.tmp" />

// take scala ant libs from scala installation, assumes SCALA_HOME is set to home of scala in env vars
<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" />

<path id="build.classpath">
<pathelement location="${scala-library.jar}" />
<!--<pathelement location="${your.path}" />-->
<pathelement location="${build.dir}" />
</path>

<taskdef resource="scala/tools/ant/antlib.xml">
<classpath>
<pathelement location="${scala.home}/lib/scala-compiler.jar" />
<pathelement location="${scala-library.jar}" />
</classpath>
</taskdef>
</target>

<target name="build" depends="init">

<mkdir dir="${build.dir}" />

<scalac srcdir="${sources.dir}"
destdir="${build.dir}"
classpathref="build.classpath"
force="changed">

<!-- <src location="src" /> -->
<include name="**/*.scala" />
</scalac>
</target>

<target name="run" depends="build">
<java classname="test.Test"
classpathref="build.classpath">
</java>
</target>
</project>




Project directory structure:

/build
/build/build.xml
/src
/src/test.scala


Example "hello world" Scala test program


package test

object Test {

def main(args : Array[String]) {

println("Hello world")
}
}


If you type ant run in the /build directory, the application is compiled and run, giving "Hello world"...


References:

http://www.scala-lang.org/node/98


.

No comments: