Ant for Scala!

Ant has come out with a “scalac” task which compiles scala code. Since I did not want to rely on Eclipse to compile my scala code and build jars, etc, I decided to write my own Ant script to automate the process.

To begin with, we need to set the required properties:

I used a file to set scala.home value.

<property name="scala-compiler.jar" value="${scala.home}/lib/scala-compiler.jar"/>
<property name="scala-library.jar" value="${scala.home}/lib/scala-library.jar"/>

Then we define the scala classpath and the scala compiler command:

<path id="scala.classpath">
<pathelement location="${scala-compiler.jar}"/>
<pathelement location="${scala-library.jar}"/>

<taskdef resource="scala/tools/ant/antlib.xml">
 <classpath refid="scala.classpath"/>

This is the main scalac target, here since I have some xml dependencies for my code, I copy them over to the build directory.
Then I compile the scala classes using the scalac ant task. Incase we want to mix scala and java code in the same project, I also have a javac target. Eclipse currently wont allow people to mix java and scala code, so this is cool.

<target name="compile" depends="prepare">
 <copy todir="${build.dir}/WEB-INF/classes">
 <fileset dir="${src.dir}">
 <include name="**/*.xml"/>
 <scalac srcdir="${src.dir}" destdir="${build.dir}/WEB-INF/classes" classpathref="build.classpath">
 <include name="**/*.scala"/>
 <include name="**/*.java"/>
 <!-- When we mix Scala and Java... -->
 <javac srcdir="${src.dir}" destdir="${build.dir}/WEB-INF/classes" classpathref="build.classpath">
 <include name="**/*.java"/>

I build a war and deploy this war in the Glassfish server, so I have a target to build the war.

<target name="package" depends="compile">
 <jar jarfile="${deploy.war}">
 <fileset dir="${web.dir}"/>
 <fileset dir="${build.dir}"/>

Figuring all this out from the distributed help online was time consuming and painful, so I’m glad I can share :-).


4 thoughts on “Ant for Scala!

  1. Or you could alternatively just port over Rake/Capistrano from Ruby (if it hasn’t been done already) which would fit in very nicely with the Scala tools as Scala can be written as a script (and with it’s type inference, it would be relatively comparable to the ruby scripts), and then you would have something much more configurable, readable, and simpler than ant..

    Nothing is better at building Scala apps than Scala..

    Ant is so last century.. time to move to a better build utility.. If you reply and tell me that your build team is not comfortable in dealing with DSLs, I will agree that you are probably right but I bet they are far less comfortable in dealing with heavy verbose XML scripts. 🙂


  2. @ilan: Saying Ant is so last century is pretty funny. That’s like saying Make is so last century. Which it is, but is very much still in use as it’s very successful. Building an Ant build file isn’t rocket science.

    vim and ant still is an amazing combination for developing. Did it with Java and now will do it with scala. Thanks for this information.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s