I have a fairly large data/user-driven web application written in Java (using a lot of the Spring Framework). You can see this site on http://cfreference.net. Today, I'm planning on migrating this to Scala.

The motivation behind this migration is to learn how to use Scala. I use CFR (http://cfreference.net) to learn new technologies and try out new techniques that I might use for my clients. I originally wrote this web app using Ruby on Rails, then re-wrote it in Java.

I've read a lot of great things about Scala (I'm mostly excited about the mixin-style types/quasi-multiple inheritance).

I'm going to document this here for anyone else looking to migrate a similar app.

I should also mention that I'm using Ant to build this project and it's running on Tomcat. I'm doing this on Linux (Ubuntu 9.04).

I'm going to write this kind of log-style. Not a lot of formatting, editing, or forethought. Okay, here we go!

First, download the latest version of Scala from here http://www.scala-lang.org/downloads and install.

Now, figure out how to compile my Java sources using the Scala compiler (scalac) using Ant. This page has some good info: http://www.scala-lang.org/node/98. The first thing I did was change all javac to scalac (I guess this is kind of test-driven development). Then I ran it to be sure it failed... sure did. It complained because it doesn't know what the scalac task is. So let's define it:


<taskdef resource="scala/tools/ant/antlib.xml">
<classpath>
<path refid="compile.classpath"/>
<pathelement location="${scala.home}/lib/scala-compiler.jar"/>
<pathelement location="${scala.home}/lib/scala-library.jar"/>
</classpath>
</taskdef>

Okay, give it a try and find that scalac does not support the debug attribute, so let's get rid of those. Not it compiles real fast i.e. it's not doing anything. I see messages like this:

Compiling 0 scala and 287 java source files to .../target/classes

I'm going to guess it's doing nothing because I don't have any Scala code. I assumed it would compile my Java code for me, but I guess not. After some googling, It looks like if I run scalac, then javac it should work (though I'm not sure how Spring IoC and AOP is going to work with all this...).

I'm also going to convert all my bean classes to .scala. This is easy because I'm generating this code from properties files (I'm not a fan of writing boilerplate getters and setters).

Actually, I ended up converting one of my bean classes to use scala.

In the end, I am now using scala in about 0.01% of my code (which is much better than the previous 0.0%). Some things I found in getting this to work:

* I had to add scala-library.jar to the tomcat runtime lib dir (scala code compiles into plain old java bytecode classes, and the scala classes use classes found in this jar)
* To create a java bean-style object (with getters and setters), I had to write the getXXX and setXXX methods in scala. I was hoping the fancy properties mechanism in scala would handle this, but it does not follow the Java beans convention
* I'm running my code on a JVM. There is an executable name scala. I'm not sure what this is for.

Powered by Drupal, an open source content management system

Navigation