Part 1: Lifting Off With Scala Aug 22, 2009
Static Scala Website in under 10 Commands
The Purpose
The purpose of this tutorial is to build a Scala web app from scratch. This app will not do much at first but you will be ready to get working with the Lift Framework. I feel the biggest hurdle that JVM based applications face is getting a new project off the ground. In this tutorial I will go through all the pieces needed to get you ready to code some Scala on the web!
On a personal note I oft wonder why Web Applications in Java are so hard to get started. I personally think this leads to Java programmers spending their free time working with other languages. In the 5 years I have been doing Java Development I have not created a single Java Web App from scratch. Even then most people just copy another applications structure and build.xml ( cringe @ ant ), hallow it out, then start to code.
The Language
Scala is a functional / object oriented programming language that runs on the Java Virtual Machine or JVM. As far as speed goes it can keep up with Java in most cases, and will blow away most dynamic languages in most benchmarks. ( ie. Ruby, Python, Groovy )
The Web Framework
My web framework of choice for Scala is the Lift Framework. From their site "Lift stresses the importance of security, maintainability, scalability and performance, while allowing for high levels of developer productivity. Lift open source software licensed under an Apache 2.0 license." Yes, this means it is free.
The Server
For my server I chose Ubuntu Linux 9.04 since I love apt-get and I am a fan of the work being done at Canonical. I also Use Ubuntu 8.10 at work on my desktop. Unfortunately server distributions are a bit harder to work with but you will be happy when your site doesn't get hacked. Unless of course your root password is well... password.
I've recently bought an account over at linode.com to have a computer to "hack" on. For a low monthly fee you basically get your own virtual machine to play with. They just ask you what Operating System Distro you would like to install and then give you root ssh access. Light years ahead of the competitors like godaddy. When setting up your hosting you can choose Ubuntu 9.04 as your OS.
Here are the commands I used to get my server up and serving a Scala Website.
First we need to update the aptitude program.
rhigdon@vanilla-bear:~$ sudo apt-get update
Now we need to download our http server. I choose Apache Http to server traffic on port 80 normally. There is some discussion whether or not to use iptables for port redirection. I personally like using Http and all its modules. It is easily extensible. The package name we are going to look for is apache2.
rhigdon@vanilla-bear:~$ sudo apt-get install apache2
Now traffic on our domain will be hitting our Apache webserver! Currently my site minus9.org is serving up the default index.html of /var/www/index.html. This is the default for Apache Http Server.
Now we need the Java Virtual Machine! Scala runs on the JVM so we need to install a flavor of the JVM. The default-jdk in Ubuntu 9.04 is the IcedTea JDK. This is an open source version of the GPL'd version of the Sun JDK. Certain libraries could not be exported to GPL due to Legal Obligations so RedHat created the IcedTea project to write what was missing.
rhigdon@vanilla-bear:~$ sudo apt-get install default-jdk
rhigdon@vanilla-bear:~$ java -version
java version "1.6.0_0"
OpenJDK Runtime Environment (IcedTea6 1.4.1) (6b14-1.4.1-0ubuntu11)
OpenJDK 64-Bit Server VM (build 14.0-b08, mixed mode)
Now we need Maven2. This will not be in aptitude's packages. I choose the route to download it directly from the Apache Site. For this I needed wget.
rhigdon@vanilla-bear:~$ sudo apt-get install wget
Then from your home directory grab the binaries
rhigdon@vanilla-bear:~$ wget http://www.ip97.com/apache.org/maven/binaries/apache-maven-2.2.1-bin.tar.bz2
This file is now in bzip2 format. We will need to un-archive it using tar:
rhigdon@vanilla-bear:~$ tar -xvpjf apache-maven-2.2.1-bin.tar.bz2 /usr/local/share/
Now you need to add the environment variables for the JDK and Maven2.
rhigdon@vanilla-bear:~$ export JAVA_HOME=/usr/lib/jvm/java-6-openjdk
rhigdon@vanilla-bear:~$ export M2_HOME=/usr/local/share/apache-maven-2.2.1
rhigdon@vanilla-bear:~$ export M2=$M2_HOME/bin
rhigdon@vanilla-bear:~$ export PATH=$M2:$JAVA_HOME:$PATH
Note -- This can be added to your profile with "vim ~/.bashrc"
Now Maven and the JDK should be ready to go.
Now we will need to create the maven from the lift archtype. For those of you not very familiar with Maven, an arch-type is basically a project template.
rhigdon@vanilla-bear:~$ mvn archetype:generate -U \ -DarchetypeGroupId=net.liftweb \ -DarchetypeArtifactId=lift-archetype-blank \ -DarchetypeVersion=1.0 -DremoteRepositories=http://scala-tools.org/repo-releases \ -DgroupId=org.minus9 \ -DartifactId=minus9Site \ -Dversion=1.0-SNAPSHOT
Note that "-DgroupId=org.minus9" needs to be replaced with your package structure and "-DartifactId=minus9Site" is your project name.
Now lets get started! You need to enter the top level directory of your project:
rhigdon@vanilla-bear:~$ cd minus9Site/
rhigdon@vanilla-bear:~$ mvn jetty:run
You now have your very first Scala Web Application! It isn't much but it is a start.
