Computing 2010

pc.jpgToday, I came across an interesting article in the Forbes Magazine entitled “Computing 2010″. The interesting part is that this article was written ten years ago. The author, Kip Crosby, imagined what computers would look like in 2010: optical circuits instead of silicon, with a CPU running at 100 GHz, holographic mass storage offering several TB capacity, 256 GB optoelectronic RAM, biometric authentication, voice control, completely wireless and shaped like a frisbee. Whew! Looks like Kip was just a tad too optimistic. Optoelectronics hasn’t caught on and most computers are still boxy rather than frisbee-ish. In fact, todays’s PC looks pretty much like that of 2000, except that its capacity has increased roughly following Moore’s law. The only accurate prediction is about mass storage capacity, although that didn’t require optical technology.

Personally, I’ve begun the new computing year with a major upgrade, though still far away from Kip’s 2010 vision. I have replaced my 32-bit Windows OS with a 64-bit Linux OS, doubled RAM from 4 GB to 8 GB and added another external USB hard drive for backups. The Ubuntu installation turned out a little difficult, because Linux did not want to cooperate with the BIOS RAID-1 configuration, so I had to switch to SATA mode and wipe out the Windows installation. The rest was easy, however. I used to worry about not being able to make my 3G USB modem work with Linux, but our maid has solved this problem for me. She obliterated the device by putting it into the washing machine. Can’t really blame her for that. I probably shouldn’t have carried the modem in the pockets of my shorts.

Back to the topic. What are the computing trends in 2010? Just off the top of my head: cloud computing is becoming a mainstream technology (or perhaps a mainstream buzz; time will tell). Along with that, virtualisation is now widely used. Supercomputers have broken the petaflop mark and now operate in the range of large clouds (> 1 PFlop). CRT monitors are quickly becoming relics of a past epoch. Single-core CPUs are headed the same way. Functional programming languages are beginning to catch on. 64-bit hardware and software are overtaking 32-bit systems in mainstream IT. Java 7 is announced for 2010. It surely looks like an interesting year.

JSP Nightmares

jsp-nightmare.jpgOnce upon a time as a Java newbie, I thought that Java Server Pages were great. Back then I had done web development in Perl and PHP and I was pleasantly surprised by the similarity of the development process. It's as simple as write and run. No bothersome compiler runs and deployment cycles. Java Server Pages seemed like PHP on steroids. Actually, I still think this is a fairly accurate description. It's what the designers of JSP intended – Java's answer to dynamic web scripting languages. The question is whether the JSP design is sound. After having spent several years in the Java Enterprise world, and having maintained large amounts of JSP code, I am convinced that it isn't. So I am writing this article to tell you the untold nightmares of JSP programming.

Before we are going into that, let's briefly look at the bright side. JSP technology is tempting for several reasons. First, it is mainstream and it bears the official stamp of approval by Sun. Second, it is an integral part of JEE and widely supported by application servers and tools. Third, it it is relatively easy to whip up dynamic content with JSP. So, why not use Java Server Pages? Well, there are several problems, the biggest of which are scriptlets. Scriptlets are embedded Java code. Because JSP allows you to mix Java code with markup, you get hybrid files which tend to become messy and difficult to maintain. Few HTML designers understand what's going on in a JSP file. Few programmers are comfortable with mixing their code with reams of markup.

The typical production process for a JSP-based application looks like this: the UI designers finish their protoypes and hand over the resulting markup to the programmers. The programmers tear apart the HTML to make it easier to insert code, repeat  sequences, and insert display logic. Once the programmers are finished, there is absolutely no chance that the designers will recognise the markup they produced. It is therefore also unlikely that they will ever touch it again. Even if the finished product contains no scriptlets, JSTL, expression language and (God help us) custom tags will confuse the hell out of designers. This means that the entire process has to be repeated,  everytime a change of visuals is requested. This is however not the worst problem.

The biggest drawback is that there is absolutely no way to prevent application logic from creeping into the JSPs which ultimately leads to spaghetti code. You might think, “Oh well, I know how to code my JSPs properly.” At this point you should remember Murphy's law, especially the part that says: “if something can go wrong it will go wrong.” Perhaps you are a disciplined individual who wouldn't even dream about putting application logic into a JSP file. But can you say the same about the other programmers in your team? What if work needs to be completed under time pressure? Can you resist the temptation to solve a problem quick-and-dirty by putting a hack into a JSP file along with a “will fix this later” note? I have seen way too many “will fix this later” notes in JSPs and most of them were several years old.

My project consisted of an application with roughly 1.5 million LOC where about half of the code was implemented in JSPs. That's about 1500-2000 JSP files. Most files were less than 500 lines, but some were in excess of 2000 lines. Once you hit upon an XL-sized JSP, it's a safe bet that some crucial functionality is buried in it. Reading and understanding a 2000 line JSP can take several hours. Modifying and maintaining it is quite another deal. The project did not attempt to separate business logic from display logic in JSPs, thus XML was liberally mixed with scriptlets. While this approach works OK for narrow functionality that can be coded  in a single JSP, it becomes very unwieldy for functionality that spans a larger problem space and, hence, many JSPs.

The “solution” for this was to use scriptlet fragments which are shared by multiple JSPs. This isn't a real solution, however, because it forgoes almost all advantages of using an OOP language, such as abstraction, encapsulation, inheritance, and as a side effect it produces “ultra-tight coupling” between JSPs that use the same fragments. In theory, this can be made slightly less painful by defining inner classes in fragments for shared functionality. These class fragments are then included in the consuming JSPs at compile time. However, inner classes have their own limitations and compile time includes bulk up the resulting byte code. Since inner classes can access variables and methods in the outer scope, they lack proper encapsulation. Tying them to multiple consumers can lead to some weird dependencies and logical errors. Finally, there is no way to produce unit tests for scriptlets. The only way to test scriptlets is by inserting the test code directly into the JSP, which is obviously insane. In summary, there is no way to code scriptlets cleanly, so it's best to avoid them.

But even without scriptlets, there's plenty of trouble. For example, there is the Java standard tag library (JSTL) and the expression language (EL) which are supposed to replace scriptlets as coding instruments. In particular, the EL has been praised as enabling clean coding for MVC applications with Java Server Pages as view component. – Well, I disagree. – JSTL+EL are neither very clean nor very concise. What is worse, they are too powerful for their own good. JSTL+EL are Turing-complete, just like XSLT which they resemble, which means that replacing scriptlets with JSTL+EL is like jumping from the frying pan into the fire. In addition, JSTL provides tags that allow programmers to access a database and execute queries. If you see MVC going out of the window at this point, you have recognised the problem. In summary, JSTL+EL produce the same problems as scriptlets, but with an XML syntax.

In conclusion, Java Server Pages can lead to maintenance nightmares, especially when used in a large project. While it is possible to code JSPs cleanly, it is apparently not a widespread practice, which is probably Murphy's law taking its toll. Hence, if you are building a new Java web application, think twice about using JSP. If you have a legacy application, you might want to replace JSPs by something more appropriate. In most cases, this can be done gradually whereas legacy JSPs can run side by side with an alternative view technology.

Ant script for Scala with Lift

Today I'd like to present another Ant script for Scala development. I promise it's going to be the last one, but I felt this mini series wouldn't be complete without it. This script is for developing a Lift application with Scala. I became interested in Lift in the first half of this year, but unfortunately, I wasn't able to install it when I wanted to give it a try, because of some besetting problems with Maven. Deplorably, the Lift developers have made Maven a requirement for Lift. If you go to the Lift download page, you wont find a Lift package but a Maven package which you must install in order to retrieve Lift from the central repository. Alternatively, you can fetch the latest Lift sources from GitHub, but then you still need Maven to get the examples to work. Since Maven and I have a difficult relationship, I mentioned this to the Lift community earlier this year. The community seemed quite pleased with Maven, however, and had little interest in supporting alternatives. Hence, I've adapted the Ant script from the last article in order to work with Lift and -while we are at it- replaced the Jetty server that is bundled with Lift with Tomcat. I hope this will be useful for people interested in tinkering with Lift who want to avoid the aggravation of having to deal with Maven. As usual, you can download the project sekeleton and Ant build file right here. I have packaged the Lift 1.0 jars as well as its dependencies (commons-fileupload.jar and log4j.ar), so that you can get the helloworld example to run without additional installation steps. If you plan to use this for your work, you should replace the Lift jars with the most recent versions from GitHub. The Ant script has the same target definitions as the one I presented for general Web development including deploy, undeploy and reload. The Lift dependencies are likewise handled in the same way, as the script uses everything it finds in the WEB-INF/lib directory. Your only responsibility is to put the right jars in there. The main difference consists of a different directory tree structure that reflects the layout of a standard Lift project. This means you won't have to rearrange things if you move a project from Maven to Ant. In fact all of the Lift examples should run simply by dropping in the sources (I've tried this for a few but not all examples). So, here's the Ant script:

<project default="build" name="hellolift">

  <!-- root directory of this project -->
  <property name="project.dir" value=".">

  <!-- root directory of Scala installation -->
  <property name="scala.home" value="C:\\Program Files\\Scala">

  <!-- root directory of Tomcat installation -->
  <property name="server.home"
    value="C:\\path\\to\\Tomcat\\installation">

  <!-- URL for Tomcat's manager application -->
  <property name="server.manager.url" value="http://dev:8080/manager">

  <!-- account name for Tomcat's admin account -->
  <property name="server.manager.username" value="admin">

  <!-- password name for Tomcat's admin account -->
  <property name="server.manager.password" value="admin">

  <!-- location of scalatest.jar for unit testing -->
  <property name="scalatest.jar"
     value="C:\\path\\to\\scalatest-1.0\\scalatest-1.0.jar">

  <target name="init">

    <!-- derived path names -->
    <property name="source.dir" value="${project.dir}/src">
    <property name="web.dir" value="${project.dir}/web">
    <property name="classes.dir" value="${web.dir}/WEB-INF/classes">
    <property name="lib.dir" value="${web.dir}/WEB-INF/lib">
    <property name="webapp.dir"
       value="${server.home}/webapps/${ant.project.name}">
    <property name="test.dir" value="${project.dir}/test">

    <!-- scala libraries for classpath definitions -->
    <property name="scala-library.jar"
        value="${scala.home}/lib/scala-library.jar">
    <property name="scala-compiler.jar"
        value="${scala.home}/lib/scala-compiler.jar">

    <!-- classpath for the compiler task definition -->
    <path id="scala.classpath">
      <pathelement location="${scala-compiler.jar}">
      <pathelement location="${scala-library.jar}">
    </pathelement>

    <!-- classpath for project build -->
    <path id="build.classpath">
      <pathelement location="${server.home}/lib/servlet-api.jar">
      <pathelement location="${scala-library.jar}">
      <fileset dir="${lib.dir}">
        <include name="*.jar">
      </include>
      <pathelement location="${classes.dir}">
    </pathelement>

    <!-- classpath for unit test build  -->
    <path id="test.classpath">
      <path refid="build.classpath">
      <pathelement location="${scalatest.jar}">
    </pathelement>

    <!-- definition for the
      "scalac" and "scaladoc" ant tasks -->
    <taskdef resource="scala/tools/ant/antlib.xml">
      <classpath refid="scala.classpath">
    </classpath>

    <!-- definition for the "scalatest" ant task -->
    <taskdef classname="org.scalatest.tools.ScalaTestTask"
      name="scalatest">
      <classpath refid="test.classpath">
    </classpath>

    <!-- definition for the "reload", "deploy"
      and "undeploy" Tomcat tasks -->
    <taskdef classname="org.apache.catalina.ant.DeployTask"
      name="deploy">
      <classpath path="${server.home}/lib/catalina-ant.jar">
    </classpath>
    <taskdef classname="org.apache.catalina.ant.ReloadTask"
      name="reload">
      <classpath path="${server.home}/lib/catalina-ant.jar">
    </classpath>
    <taskdef classname="org.apache.catalina.ant.UndeployTask"
      name="undeploy">
      <classpath path="${server.home}/lib/catalina-ant.jar">
    </classpath>

  </taskdef>

  <!-- compile project -->
  <target depends="init" description="build" name="build">
    <buildnumber>
    <tstamp>
    <mkdir dir="${classes.dir}">
    <mkdir dir="${lib.dir}">
    <copy file="${scala-library.jar}" todir="${lib.dir}">
    <scalac classpathref="build.classpath" deprecation="on"
        destdir="${classes.dir}" force="never" srcdir="${source.dir}">
      <include name="**/*.scala">
    </include>
  </scalac>

  <!-- create a deployable web archive -->
  <target depends="build" description="war" name="war">
    <war basedir="${web.dir}"
      destfile="${project.dir}/${ant.project.name}.war"
       webxml="${web.dir}/WEB-INF/web.xml">
  </war>

  <!-- creates a deployable web archive with
    all classes packed into a single jar file -->
  <target depends="build" description="packedwar"
    name="packedwar">
    <jar basedir="${classes.dir}"
      destfile="${lib.dir}/${ant.project.name}.jar"
      duplicate="preserve">
      <manifest>
        <section name="Program">
          <attribute name="Title" value="${ant.project.name}">
          <attribute name="Build" value="${build.number}">
          <attribute name="Date" value="${TODAY}">
        </attribute>
      </attribute>
    </attribute>
    <delete dir="${classes.dir}">
    <war basedir="${web.dir}"
      destfile="${project.dir}/${ant.project.name}.war"
      webxml="${web.dir}/WEB-INF/web.xml">
    <delete file="${lib.dir}/${ant.project.name}.jar">
  </delete>

  <!-- deploy project on Tomcat server -->
  <target depends="war" description="deploy" name="deploy">
    <mkdir dir="${webapp.dir}">
    <copy todir="${webapp.dir}">
      <fileset dir="${web.dir}">
    </fileset>
    <deploy localwar="${project.dir}/${ant.project.name}.war"
        password="${server.manager.password}"
	path="/${ant.project.name}"
        url="${server.manager.url}"
	username="${server.manager.username}">
  </deploy>

  <!-- update and reload project on Tomcat server -->
  <target depends="build" description="reload" name="reload">
    <copy file="${scala-library.jar}" todir="${lib.dir}">
    <copy todir="${webapp.dir}">
      <fileset dir="${web.dir}">
    </fileset>
    <reload password="${server.manager.password}"
      path="/${ant.project.name}"
      url="${server.manager.url}"
      username="${server.manager.username}">
  </reload>

  <!-- remove project from Tomcat server -->
  <target depends="init" description="undeploy" name="undeploy">
    <undeploy password="${server.manager.password}"
      path="/${ant.project.name}"
      url="${server.manager.url}"
      username="${server.manager.username}">
  </undeploy>

  <!-- build unit tests -->
  <target depends="build" name="buildtest">
    <mkdir dir="${test.dir}/build">
    <scalac classpathref="test.classpath" deprecation="on"
        destdir="${test.dir}/build" force="never" srcdir="${test.dir}">
      <include name="**/*.scala">
    </include>
  </scalac>

  <!-- run unit tests -->
  <target depends="buildtest" description="test" name="test">
    <scalatest runpath="${test.dir}/build">
      <reporter config="YFABRT" type="stdout">
      <membersonly package="suite">
      <!-- <reporter type="graphic" config="YFABRT"/> -->
      <!-- <suite classname="suite.TestSuite"/> -->
    </membersonly>
  </reporter>

  <!-- delete all build files -->
  <target depends="init" description="clean" name="clean">
    <delete dir="${classes.dir}">
    <delete dir="${project.dir}/doc">
    <delete dir="${test.dir}/build">
    <delete file="${lib.dir}/scala-library.jar">
    <delete file="${project.dir}/${ant.project.name}.war">
  </delete>

  <!-- create API documentation in doc folder -->
  <target depends="build" description="scaladoc" name="scaladoc">
    <mkdir dir="${project.dir}/doc">
    <scaladoc classpathref="build.classpath"
      destdir="${project.dir}/doc"
      doctitle="${ant.project.name}" srcdir="${source.dir}"
      windowtitle="${ant.project.name}">
    </scaladoc>
  </target>

</project>

Ant script for Scala Web application

In my last blog entry I introduced an Ant script for building a stand-alone application with Scala. Today I present a script for building a Scala web application. Just as in the last example, you can download the script and the project skeleton layout for a “Hello World” application. The difference is that this application prints the infamous words on a web page rather than on the console. Things get slightly more complicated in a web application, because we need to interact with a web server to run the program. This is reflected by the Ant script below which has additional target definitions for deploying, undeploying and reloading the application on the server. I chose Tomcat for the server, because it's a popular choice for Java web development and because Tomcat is mature, lightweight, and standards-compliant. If you use another web server, you may have to make some small modifications, but the overall structure should be the same. The script disregards web frameworks altogether and the “Hello World” application is simply implemented as a servlet. Since the file structure reflects the standard layout for a Java web application, it should be straightforward to get this to work with any Java web framework.

<project default="build" name="hello">

  <!-- root directory of this project -->
  <property name="project.dir" value=".">

  <!-- root directory of Scala installation -->
  <property name="scala.home" value="C:\\Program Files\\Scala">

  <!-- root directory of Tomcat installation -->
  <property name="server.home"
    value="C:\\path\\to\\Tomcat\\installation">

  <!-- URL for Tomcat's manager application -->
  <property name="server.manager.url"
    value="http://dev:8080/manager">

  <!-- account name for Tomcat's admin account -->
  <property name="server.manager.username" value="admin">

  <!-- password name for Tomcat's admin account -->
  <property name="server.manager.password" value="admin">

  <!-- location of scalatest.jar for unit testing -->
  <property name="scalatest.jar"
     value="C:\\path\\to\\scalatest-1.0\\scalatest-1.0.jar">

  <target name="init">

    <!-- derived path names -->
    <property name="source.dir" value="${project.dir}/src">
    <property name="web.dir" value="${project.dir}/web">
    <property name="classes.dir" value="${web.dir}/WEB-INF/classes">
    <property name="lib.dir" value="${web.dir}/WEB-INF/lib">
    <property name="webapp.dir"
        value="${server.home}/webapps/${ant.project.name}">
    <property name="test.dir" value="${project.dir}/test">

    <!-- scala libraries for classpath definitions -->
    <property name="scala-library.jar"
       value="${scala.home}/lib/scala-library.jar">
    <property name="scala-compiler.jar"
      value="${scala.home}/lib/scala-compiler.jar">

    <!-- classpath for the compiler task definition -->
    <path id="scala.classpath">
      <pathelement location="${scala-compiler.jar}">
      <pathelement location="${scala-library.jar}">
    </pathelement>

    <!-- classpath for project build -->
    <path id="build.classpath">
      <pathelement location="${server.home}/lib/servlet-api.jar">
      <pathelement location="${scala-library.jar}">
      <fileset dir="${lib.dir}">
        <include name="*.jar">
      </include>
      <pathelement location="${classes.dir}">
    </pathelement>

    <!-- classpath for unit test build  -->
    <path id="test.classpath">
      <path refid="build.classpath">
      <pathelement location="${scalatest.jar}">
    </pathelement>

    <!-- definition for the "scalac"
      and "scaladoc" ant tasks -->
    <taskdef resource="scala/tools/ant/antlib.xml">
      <classpath refid="scala.classpath">
    </classpath>

    <!-- definition for the "scalatest" ant task -->
    <taskdef classname="org.scalatest.tools.ScalaTestTask" name="scalatest">
      <classpath refid="test.classpath">
    </classpath>

    <!-- definition for the "reload", "deploy"
      and "undeploy" Tomcat tasks -->
    <taskdef classname="org.apache.catalina.ant.DeployTask"
      name="deploy">
      <classpath path="${server.home}/lib/catalina-ant.jar">
    </classpath>
    <taskdef classname="org.apache.catalina.ant.ReloadTask"
      name="reload">
      <classpath path="${server.home}/lib/catalina-ant.jar">
    </classpath>
    <taskdef classname="org.apache.catalina.ant.UndeployTask"
      name="undeploy">
      <classpath path="${server.home}/lib/catalina-ant.jar">
    </classpath>

  </taskdef>

  <!-- compile project -->
  <target depends="init" description="build" name="build">
    <buildnumber>
    <tstamp>
    <mkdir dir="${classes.dir}">
    <mkdir dir="${lib.dir}">
    <copy file="${scala-library.jar}" todir="${lib.dir}">
    <scalac classpathref="build.classpath" deprecation="on"
       destdir="${classes.dir}" force="never" srcdir="${source.dir}">
      <include name="**/*.scala">
    </include>
  </scalac>

  <!-- create a deployable web archive -->
  <target depends="build" description="war" name="war">
    <war basedir="${web.dir}"
        destfile="${project.dir}/${ant.project.name}.war"
        webxml="${web.dir}/WEB-INF/web.xml">
  </war>

  <!-- creates a deployable web archive with all classes
    packed into a single jar file -->
  <target depends="build" description="packedwar" name="packedwar">
    <jar basedir="${classes.dir}"
        destfile="${lib.dir}/${ant.project.name}.jar" duplicate="preserve">
      <manifest>
        <section name="Program">
          <attribute name="Title" value="${ant.project.name}">
          <attribute name="Build" value="${build.number}">
          <attribute name="Date" value="${TODAY}">
        </attribute>
      </attribute>
    </attribute>
    <delete dir="${classes.dir}">
    <war basedir="${web.dir}"
        destfile="${project.dir}/${ant.project.name}.war"
        webxml="${web.dir}/WEB-INF/web.xml">
    <delete file="${lib.dir}/${ant.project.name}.jar">
  </delete>

  <!-- deploy project on Tomcat server -->
  <target depends="war" description="deploy" name="deploy">
    <mkdir dir="${webapp.dir}">
    <copy todir="${webapp.dir}">
      <fileset dir="${web.dir}">
    </fileset>
    <deploy localwar="${project.dir}/${ant.project.name}.war"
        password="${server.manager.password}"
	path="/${ant.project.name}"
        url="${server.manager.url}"
	username="${server.manager.username}">
  </deploy>

  <!-- update and reload project on Tomcat server -->
  <target depends="build" description="reload" name="reload">
    <copy file="${scala-library.jar}" todir="${lib.dir}">
    <copy todir="${webapp.dir}">
      <fileset dir="${web.dir}">
    </fileset>
    <reload password="${server.manager.password}"
      path="/${ant.project.name}"
      url="${server.manager.url}"
      username="${server.manager.username}">
  </reload>

  <!-- remove project from Tomcat server -->
  <target depends="init" description="undeploy" name="undeploy">
    <undeploy password="${server.manager.password}"
      path="/${ant.project.name}"
      url="${server.manager.url}"
      username="${server.manager.username}">
  </undeploy>

  <!-- build unit tests -->
  <target depends="build" name="buildtest">
    <mkdir dir="${test.dir}/build">
    <scalac classpathref="test.classpath" deprecation="on"
       destdir="${test.dir}/build" force="never" srcdir="${test.dir}">
      <include name="**/*.scala">
    </include>
  </scalac>

  <!-- run unit tests -->
  <target depends="buildtest" description="test" name="test">
    <scalatest runpath="${test.dir}/build">
      <reporter config="YFABRT" type="stdout">
      <membersonly package="suite">
      <!-- <reporter type="graphic" config="YFABRT"/> -->
      <!-- <suite classname="suite.TestSuite"/> -->
    </membersonly>
  </reporter>

  <!-- delete all build files -->
  <target depends="init" description="clean" name="clean">
    <delete dir="${classes.dir}">
    <delete dir="${project.dir}/doc">
    <delete dir="${test.dir}/build">
    <delete file="${lib.dir}/scala-library.jar">
    <delete file="${project.dir}/${ant.project.name}.war">
  </delete>

  <!-- create API documentation in doc folder -->
  <target depends="build" description="scaladoc" name="scaladoc">
    <mkdir dir="${project.dir}/doc">
    <scaladoc classpathref="build.classpath"
        destdir="${project.dir}/doc"
        doctitle="${ant.project.name}" srcdir="${source.dir}"
        windowtitle="${ant.project.name}">
    </scaladoc>
  </target>

</project>

The directory structure differs slightly from that for a standalone application. We have an additional web directory for web content. It contains the WEB-INF directory where all class files and  libraries go. During development, class files are directly copied to the server without packaging. This ensures faster deploy/test cycles. In addition, there are Ant tasks for putting class files into a single jar  and for creating a distributable war file (war, packedwar). I have tried to keep the number of properties that need to be changed down to a minimum. Obviously, you need to set the home directories of your Scala, Scalatest, and Tomcat installations. You might also have to change the admin password for the Tomcat manager application which is used for automated deployment. Here is a summary of the defined targets:

  • build = compile your webapp and put class files into the WEB-INF/classes directory.
  • war = build a deployable web archive.
  • packedwar = build a deployable web archive with all class files packed into a jar.
  • deploy = deploy your webapp on Tomcat server.
  • reload = update project files on server and reload application.
  • undeploy = remove webapp from Tomcat server.
  • test = build and run unit tests (using Scalatest).
  • clean = delete all build files.
  • 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.

Scala With Ant

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.

<project default="run" name="helloworld">

  <!-- root directory of this project -->
  <property name="project.dir" value=".">

  <!-- main class to run -->
  <property name="main.class" value="app.HelloWorld">

  <!-- root directory of Scala installation -->
  <property name="scala.home"
    value="C:\\Program Files\\Scala">

  <!-- location of scalatest.jar for unit testing -->
  <property name="scalatest.jar"
    value="C:\\Program Files\\...\\scalatest-1.0.jar">

  <target name="init">

    <!-- derived path names -->
    <property name="build.dir" value="${project.dir}/build">
    <property name="source.dir" value="${project.dir}/src">
    <property name="test.dir" value="${project.dir}/test">

    <!-- scala libraries for classpath definitions -->
    <property name="scala-library.jar"
      value="${scala.home}/lib/scala-library.jar">
    <property name="scala-compiler.jar"
      value="${scala.home}/lib/scala-compiler.jar">

    <!-- classpath for the compiler task definition -->
    <path id="scala.classpath">
      <pathelement location="${scala-compiler.jar}">
      <pathelement location="${scala-library.jar}">
    </pathelement>

    <!-- classpath for project build -->
    <path id="build.classpath">
      <pathelement location="${scala-library.jar}">
      <fileset dir="${project.dir}/lib">
        <include name="*.jar">
      </include>
      <pathelement location="${build.dir}/classes">
    </pathelement>

    <!-- classpath for unit test build  -->
    <path id="test.classpath">
      <pathelement location="${scala-library.jar}">
      <pathelement location="${scalatest.jar}">
      <pathelement location="${build.dir}/classes">
    </pathelement>

    <!-- definition for the "scalac" and
      "scaladoc" ant tasks -->
    <taskdef resource="scala/tools/ant/antlib.xml">
      <classpath refid="scala.classpath">
    </classpath>

    <!-- definition for the "scalatest" ant task -->
    <taskdef classname="org.scalatest.tools.ScalaTestTask"
      name="scalatest">
      <classpath refid="test.classpath">
    </classpath>

  </taskdef>

  <!-- delete compiled files -->
  <target depends="init" description="clean" name="clean">
    <delete dir="${build.dir}">
    <delete dir="${project.dir}/doc">
    <delete file="${project.dir}/lib/scala-library.jar">
  </delete>

  <!-- compile project -->
  <target depends="init" description="build" name="build">
    <buildnumber>
    <tstamp>
    <mkdir dir="${build.dir}/classes">
    <scalac classpathref="build.classpath" deprecation="on"
       destdir="${build.dir}/classes"
       force="never" srcdir="${source.dir}">
      <include name="**/*.scala">
    </include>
  </scalac>

  <!-- run program -->
  <target depends="build" description="run" name="run">
    <java classname="${main.class}" classpathref="build.classpath">
  </java>

  <!-- build unit tests -->
  <target depends="build" name="buildtest">
    <mkdir dir="${build.dir}/test">
    <scalac classpathref="test.classpath" deprecation="on"
      destdir="${build.dir}/test" force="never" srcdir="${test.dir}">
      <include name="**/*.scala">
    </include>
  </scalac>

  <!-- run unit tests -->
  <target depends="buildtest" description="test" name="test">
    <scalatest runpath="${build.dir}/test">
      <reporter config="YFABRT" type="stdout">
      <membersonly package="suite">
      <!-- <reporter type="graphic" config="YFABRT"/> -->
      <!-- <suite classname="suite.TestSuite"/> -->
    </membersonly>
  </reporter>

  <!-- create a startable *.jar with proper
    classpath dependency definition -->
  <target depends="build" description="jar" name="jar">
    <mkdir dir="${build.dir}/jar">
    <copy file="${scala-library.jar}" todir="${project.dir}/lib">
    <path id="jar.class.path">
       <fileset dir="${project.dir}">
         <include name="lib/**/*.jar">
       </include>
    </fileset>
    <pathconvert dirsep="/" pathsep=" " property="jar.classpath">
      <path refid="jar.class.path"></path>
      <map from="${basedir}${file.separator}lib" to="lib">
    </map></pathconvert>
    <jar basedir="${build.dir}/classes"
      destfile="${build.dir}/jar/${ant.project.name}.jar"
      duplicate="preserve">
      <manifest>
        <attribute name="Main-Class" value="${main.class}">
        <attribute name="Class-Path" value="${jar.classpath}">
        <section name="Program">
          <attribute name="Title" value="${ant.project.name}">
          <attribute name="Build" value="${build.number}">
          <attribute name="Date" value="${TODAY}">
        </attribute>
      </attribute>
    </attribute>
  </section>

  <!-- create API documentation in doc folder -->
  <target depends="build" description="scaladoc"
    name="scaladoc">
    <mkdir dir="${project.dir}/doc">
    <scaladoc classpathref="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 -->
  <target depends="jar, scaladoc"
    description="package" name="package">
    <zip destfile="${build.dir}/${ant.project.name}.zip">
      <zipfileset dir="${build.dir}/jar"
        includes="${ant.project.name}.jar">
      <zipfileset dir="${project.dir}" includes="lib/*"/>
      <zipfileset dir="${project.dir}" includes="doc/*"/>
      <zipfileset dir="${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:

Scala Project Skeleton
  • build – temporary directory for class files and other binaries
  • lib – contains external dependencies (*.jar files)
  • src – contains Scala source code tree
  • test – contains Scala unit tests
  • 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. Ant in Eclipse