Although Scala is tool-agnostic, the Scala community seems to have a preference for the Apache Maven build tool. I am not quite sure why. Given that most people who learn Scala come from a Java background, what could be more natural than using Ant for Scala development? As a tried-and-true build solution, Ant avoids the headaches and additional learning curve that comes with an unfamiliar build tool. Although I have spent considerable time with Maven as part of my job, Maven and I haven’t become friends, and we probably never will. I won’t go into the details of the how and why. Instead I’m going to present and explain a few Ant scripts for Scala development. If you are new to Scala, I hope that this will give you a headstart with your new Scala projects. After all, you have a new language to learn and little time to bother with tools.
<!-- classpath for unit test build --> <pathid="test.classpath"> <pathelementlocation="${scala-library.jar}"> <pathelementlocation="${scalatest.jar}"> <pathelementlocation="${build.dir}/classes"> </pathelement>
<!-- definition for the "scalac" and "scaladoc" ant tasks --> <taskdefresource="scala/tools/ant/antlib.xml"> <classpathrefid="scala.classpath"> </classpath>
<!-- definition for the "scalatest" ant task --> <taskdefclassname="org.scalatest.tools.ScalaTestTask" name="scalatest"> <classpathrefid="test.classpath"> </classpath>
<!-- create API documentation in doc folder --> <targetdepends="build"description="scaladoc" name="scaladoc"> <mkdirdir="${project.dir}/doc"> <scaladocclasspathref="build.classpath" destdir="${project.dir}/doc" doctitle="${ant.project.name}"srcdir="${source.dir}" windowtitle="${ant.project.name}"> </scaladoc>
<!-- create a zip file with binaries for distribution --> <targetdepends="jar, scaladoc" description="package"name="package"> <zipdestfile="${build.dir}/${ant.project.name}.zip"> <zipfilesetdir="${build.dir}/jar" includes="${ant.project.name}.jar"> <zipfilesetdir="${project.dir}"includes="lib/*"/> <zipfilesetdir="${project.dir}"includes="doc/*"/> <zipfilesetdir="${project.dir}/txt"includes="*"/> </zip> </target>
</project>
Download "helloworld.scala" with Ant build file. Fortunately, the designers of Scala provided built-in Ant tasks in the compiler library, which makes the integration of Ant straightforward. The XML code above shows the build.xml for a typical Scala standalone application, such as a GUI program or a console application. It is reasonably complete and it provides Ant targets for all common tasks:
build = compile your project and put class files into the ./build directory.
run = run program.
jar = create a startable jar archive from class files.
test = build and run unit tests (using Scalatest).
scaladoc = create API documentation from sources and put it into the in ./doc directory.
package = create a distributable zip archive that contains all dependencies plus Scaladocs.
The directory tree is kept as simple as possible:
skeleton-standalone
|
|--- build
|--- lib
|--- src
| |--- app
|
|--- test
| |--- suite
|
|--- txt
build - temporary directory for class files and other binaries
txt - contains files (such as license text) that are packaged with the distributable zip archive
If your project depends on external jars, all you have to do is to drop these into the ./lib folder. Ant will take care of creating appropriate class paths, entering them into the manifest of the startable jar file, and including them in the distribution package. The Scala library is packed automatically so that the resulting package can be deployed on computers that don’t have Scala installed (only a standard JRE is required). To use this Ant script on your computer, you need to change the values of the scala.home and scalatest.jar properties to match the location of your Scala and Scalatest installations (never mind the latter if you use another unit testing framework).
If you are working in a team, it is good practice to put Ant properties into a separate properties file, so that the build file can be checked into a source code management tool. I have included them here for simplicity. One of the advantages of using Ant is that it has very good integration with IDEs, such as Eclipse. The only twist is that Eclipse does not honour the classpaths defined in build.xml for its own incremental compilation process used to mark syntactical errors. Thus if you import the Ant project into Eclipse, you have to tell Eclipse about the dependencies using the Project/Properties/Java Build Path option.