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.
<!-- classpath for unit test build --> <pathid="test.classpath"> <pathrefid="build.classpath"> <pathelementlocation="${scalatest.jar}"> </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>
<!-- definition for the "reload", "deploy" and "undeploy" Tomcat tasks --> <taskdefclassname="org.apache.catalina.ant.DeployTask" name="deploy"> <classpathpath="${server.home}/lib/catalina-ant.jar"> </classpath> <taskdefclassname="org.apache.catalina.ant.ReloadTask" name="reload"> <classpathpath="${server.home}/lib/catalina-ant.jar"> </classpath> <taskdefclassname="org.apache.catalina.ant.UndeployTask" name="undeploy"> <classpathpath="${server.home}/lib/catalina-ant.jar"> </classpath>
<!-- create a deployable web archive --> <targetdepends="build"description="war"name="war"> <warbasedir="${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 --> <targetdepends="build"description="packedwar"name="packedwar"> <jarbasedir="${classes.dir}" destfile="${lib.dir}/${ant.project.name}.jar"duplicate="preserve"> <manifest> <sectionname="Program"> <attributename="Title"value="${ant.project.name}"> <attributename="Build"value="${build.number}"> <attributename="Date"value="${TODAY}"> </attribute> </attribute> </attribute> <deletedir="${classes.dir}"> <warbasedir="${web.dir}" destfile="${project.dir}/${ant.project.name}.war" webxml="${web.dir}/WEB-INF/web.xml"> <deletefile="${lib.dir}/${ant.project.name}.jar"> </delete>
<!-- deploy project on Tomcat server --> <targetdepends="war"description="deploy"name="deploy"> <mkdirdir="${webapp.dir}"> <copytodir="${webapp.dir}"> <filesetdir="${web.dir}"> </fileset> <deploylocalwar="${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 --> <targetdepends="build"description="reload"name="reload"> <copyfile="${scala-library.jar}"todir="${lib.dir}"> <copytodir="${webapp.dir}"> <filesetdir="${web.dir}"> </fileset> <reloadpassword="${server.manager.password}" path="/${ant.project.name}" url="${server.manager.url}" username="${server.manager.username}"> </reload>
<!-- remove project from Tomcat server --> <targetdepends="init"description="undeploy"name="undeploy"> <undeploypassword="${server.manager.password}" path="/${ant.project.name}" url="${server.manager.url}" username="${server.manager.username}"> </undeploy>
<!-- 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> </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.