Scala 05 Oct 2008 05:26 pm
IDE and Ant support for Scala
If I intend on writing even a smallish application there are two things I look for in regards to the tool support of the language.
- I hope the language has reasonable IDE support. By reasonable it should offer me a moderate number of code refactorings as a minimum.
- The language should integrate nicely with an automated build system so that i can incorparate the build in to a continuous integration environment.
One of the factors that can make or break the success of a language is the IDE support. Currently Scala’s IDE options are not great. Nothing that i have seen in regards to Scala’s IDE support has rocked my world. The main two I have explored is Intelli’s Scala plugin and the Scala plugin for Eclipse.
Normally my editor of choice would be IntelliJ for authoring Java. When it comes to writing Scala the IntelliJ plugin available is buggy at best and completely unusable on the Mac. I am sure this will improve over time. I am quietly hopeful that by the time IntelliJ 8 is released, many of the issues surrounding the Scala plugin will be resolved.
The Eclipse plugin, while quite limited in features seems less buggy than the IntelliJ equivalent. It is still not great though, so be prepared for some ugliness.
Both of the IDE’s mentioned above offer little more than the most basic refactoring support for Scala.
Fortunately Scala fairs better when it comes to an automated build environment. Scala offers great support for Apache Ant out of the box.
To illustrate an Ant build, lets get a simple application running. Here is a hello world application.
package com.gigantiq.helloworld
object HelloWorld extends Application {
println("Hello world!")
}
To show this application running we can either use the Scala compiler called “scalac” or the Scala compiler daemon called “fsc” to compile the application. For example:
andytrigg$ fsc com/gigantiq/helloworld/HelloWorld.scala
andytrigg$
Now that the application has been compiled to Java byte code, we can run the application by using the scala command:
andytrigg$ scala com.gigantiq.helloworld.HelloWorld
The output looks something like this:
andytrigg$ scala com.gigantiq.helloworld.HelloWorld
Hello world!
andytrigg$
Now that we have proven that the application runs, lets write a simple Ant script to automate this.
<?xml version="1.0" encoding="UTF-8"?>
<project name="hello world" basedir="." default="run">
<property name="scala.home" value="/Users/andytrigg/dev/scala/scala-2.7.1.final"/>
<property name="scala-compiler.jar" value="${scala.home}/lib/scala-compiler.jar"/>
<property name="scala-library.jar" value="${scala.home}/lib/scala-library.jar"/>
<property name="build.dir" value="${basedir}/build"/>
<property name="src.dir" value="${basedir}/src"/>
To make use of the built in Ant targets that Scala offers both “scala-compiler.jar” and “scala-library.jar” need to be on the classpath.
<path id="scala.classpath">
<pathelement location="${scala-compiler.jar}"/>
<pathelement location="${scala-library.jar}"/>
</path>
<path id="build.classpath">
<pathelement location="${scala-library.jar}" />
<pathelement location="${build.dir}" />
</path>
Add the Scala task definitions to this Ant project.
<taskdef resource="scala/tools/ant/antlib.xml">
<classpath refid="scala.classpath"/>
</taskdef>
Use the “scalac” compiler to build the project.
<target name="-compile">
<mkdir dir="${build.dir}"/>
<scalac srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath" force="changed">
<include name="**/*.scala"/>
</scalac>
</target>
Once the project is compiled to Java byte code, the application can be executed as a java application.
<target name="-run">
<java classname="com.gigantiq.helloworld.HelloWorld" classpathref="build.classpath">
</java>
</target>
<target name="--run" depends="-compile, -run"/>
<target name="run" depends="--run"/>
</project>
Now that we have an Ant build we can run the following from the command line:
andytrigg$ ant run
The result looks like this …
Buildfile: build.xml
-compile:
-run:
[java] Hello world!
–run:
run:
BUILD SUCCESSFUL
Total time: 0 seconds
andytrigg$
As you can see it is pretty simple to get a Scala application being built using Ant.

