Create a LiftWeb project with Simple Build Tool

If you are like me an eternal beginner on the Lift Web Framework, after starting a few attempts you probably think that perhaps maven lift archetypes are sometimes really slow process to start a liftweb project, and also that you need something more intuitive as Rails or Grails. Most of the time, when I am using maven, I need to look on the documentation again and again. So I have decided that I would like to use other option such as the Simple Build Tool.

First, we are going to install sbt. I have already made a small tutorial of how to install sbt for Mac OS X Leopard users, which is located here

Once the SBT tool has finally been installed, we proceed to create the lift project structure, there are 2 ways to create a Lift Project, the first one to create a new project by the traditional means using maven:

  mvn archetype:generate \
           -DarchetypeGroupId=net.liftweb \
           -DarchetypeArtifactId=lift-archetype-basic_2.8.0 \
           -DarchetypeVersion=2.1                           \
           -DarchetypeRepository=http://scala-tools.org/repo-snapshots \
           -DremoteRepositories=http://scala-tools.org/repo-snapshots \
           -DgroupId=org.activoricordi.cloudconsole \
           -DartifactId=dbcloudconsole \
           -Dversion=1.0

Then use sbt to manage the rest. We later build the sbt project. However, this way of working is precisely what I want to avoid.

The other option is to use git and download the lift-sbt-prototype from the LiftWeb Prototype Site. If you have not downloaded git, I would suggest two options or either find at Google a good tutorial of how to do it or the other option is to wait for new tutorial where we explain how we install it in Eclipse.

We clone the project repository using the following command:

git clone git://github.com/lift/lift_sbt_prototype.git

But there is a better option, which is using Lifty. Lifty is a SBT processor for generating files associated with the Lift framework. It is a open source project created by Mads Hartmann, which is based on a sbt framework called Lifty engine, which we will describe another day.

Lifty site

Lifty site

mkdir lifty_test
cd lifty_test
sbt

Once we enter the sbt command the following message will appear:

Project does not exist, create new project? (y/N) : y

SBT Project Creation Line


If we select the option Y and we then to enter commands to fill the parameters appear:

  • Name:liftytest
  • Organization:org.lifty
  • Version [1.0]:1
  • Scala version [2.7.7]: 2.8.0
  • sbt version [0.7.4]: 0.7.4
  • Here we can see an screenshot of the process:

    sbt_project_creation_process

    Screenshot from sbt project creation process


    This means that we have created a normal scala project, where we have filled the necessary fields
    We then enter the following command at the sbt prompt which are located on this page: http://lifty.github.com/Lifty/:

    *lifty is org.lifty lifty 1.3
    

    This command download the lifty processor.

    To test it, we are going to enter the following command.

    lifty help
    

    The lifty processor is quite simple, supporting the following commands:

    help: This will list all of the available commands
    templates: This will list all of the available templates
    create: This will attempt to create on of the templates

    lifty create project
    

    This command creates all the structure needed to execute the project. Even it creates the command.

    import sbt._
     
    class LiftProject(info: ProjectInfo) extends DefaultWebProject(info) {
      val mavenLocal = "Local Maven Repository" at
      "file://"+Path.userHome+"/.m2/repository"
     
       val scalatoolsSnapshot = "Scala Tools Snapshot" at
      "http://scala-tools.org/repo-snapshots/"
     
      val scalatoolsRelease = "Scala Tools Snapshot" at
      "http://scala-tools.org/repo-releases/"
     
      val liftVersion = "2.1-SNAPSHOT"
     
      override def libraryDependencies = Set(
        "net.liftweb" %% "lift-webkit" % liftVersion % "compile->default",
        "net.liftweb" %% "lift-testkit" % liftVersion % "compile->default",
        "net.liftweb" %% "lift-wizard" % liftVersion % "compile->default",
        "net.liftweb" %% "lift-mapper" % liftVersion % "compile->default",
        "com.h2database" % "h2" % "1.2.138",
        "org.mortbay.jetty" % "jetty" % "6.1.22" % "test->default",
        "junit" % "junit" % "4.5" % "test->default",
        "org.scala-tools.testing" % "specs" % "1.6.1" % "test->default"
      ) ++ super.libraryDependencies
    }

    As a tip, I would recommend adding are going to add the following dependencies which I consider very valuable.

    "net.liftweb" %% "lift-widgets" % liftVersion % "compile->default",

    Once we add it we have to save the file and use the sbt reload command again.
    To summarize:
    a) We create the folder with the name of the project we want to create
    b) We access to the folder and we then use the sbt command
    c) Instead of filling it

Install the Simple Build Tool for LiftWeb Framework

Install the Simple Build Tool (SBT) for LiftWeb Framework

Best way to install SBT for Mac OS X is described on the following tutorial called Getting Started with Scala using SBT.
However, in this case since I do not have wget installed, we have downloaded the sbt-launcher.jar file from the official Simple Build Tool and place it on our Downloads folder. Using the following commands, we can install the sbt launcher and make it accessible from the command tools :

cd ~
sudo mv ~/Downloads/sbt-launch-0.7.4.jar /usr/local/bin/sbt-launcher.jar
echo "java -Xmx512M -jar /usr/local/bin/sbt-launcher.jar \"\$@\"" | sudo tee /usr/local/bin/sbt
sudo chmod +x /usr/local/bin/sbt

Thank’s to Chris Moos script, the SBT jar is installed and it creates a script called sbt that will allow us to run the sbt jar. To test it just type sbt and press enter, and you now have access to SBT.

Create a Lift Project with Maven

Install Lift 2.1 in MacOSX Leopard > (4) Create a Lift Project with Maven:

The Apache Maven has become a standard, a standard that is already installed in Mac OS X Leopard by default, and there is no need to installed again. It is also possible to update it.

Once the maven is installed, we can use it to create a Lift Project, you could start the mvn command, and create a project with the following structure.

mvn archetype:generate \
 -DarchetypeGroupId=net.liftweb \
 -DarchetypeArtifactId=lift-archetype-basic_2.8.0 \
 -DarchetypeVersion=2.1 \
 -DarchetypeRepository=http://scala-tools.org/repo-releases \
 -DremoteRepositories=http://scala-tools.org/repo-releases \
 -DgroupId=org.activoricordi.app \
 -DartifactId=lift_app_test \
 -Dversion=1.0

We are going to analyze each of the terms on this archetype:

  • archetype:create -U: It indicates the type of archetype we are going to use, there could be others which we will use to build the application or deployed
  • -DarchetypeArtifactId can have one of the three values
    • - lift-archetype-blank: To create a blank project without any database or ORM.
    • - lift-archetype-basic:To create a liftweb project with ORM and derby database, it is good for prototyping a project and help to start with beginners
    • - lift-archetype-jpa-basic To create a liftweb project with ORM and jpa database

    We modify the following parameters to adjust it to our project:

    -DgroupId=org.activoricordi.app \
     -DartifactId=lift_app_test \
     -Dversion=1.0

    Once the execution of the Maven is completed, we execute the following commands:

Install Apache Maven in Mac OS X Leopard

Install Lift 2.1 in MacOSX Leopard > (3) Install Maven in MacOSX Leopard:

As we said before, Lift project uses Apache Maven as its default build system, luckily Maven is already installed on the Mac OS X Leopard computers, and we can check the version using the following command:

mvn --version

It is will appear the following message:

Apache Maven 2.2.0 (r788681; 2009-06-26 15:04:01+0200)
Java version: 1.6.0_20
Java home: /System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home
Default locale: en_US, platform encoding: MacRoman
OS name: "mac os x" version: "10.6.4" arch: "x86_64" Family: "mac"

If you don’t already have Maven or in case it does not have the version you are looking for, then, the first thing to do is to download and install it on your Mac OS X Leopard. The process of installing Maven typically involves four simple steps, which are as follows:

1) To download a release build archive appropriate for your platform, in this case MacOS X from http://maven.apache.org/download.html. You may want to get the zip file on a Windows machine and tarred zip or bz2 file on a Linux, Unix or Mac OSX machine.

2) Expand the archive file in a preferred folder within your file system.

3) Create or update a few environment variables, namely:

JAVA_HOME – point it to your JDK folder. I assume JDK is already installed. If not, go get it before you go further.
M2_HOME – point it to the folder where Maven is installed. It’s the same that was created when you extracted the Maven archive.
M2 – point it to %M2_HOME%\bin on windows and $M2_HOME/bin on Linux/Unix/Mac OSX
PATH – update the PATH environment variable. Add M2 to it.
MAVEN_OPTS – Add -Xms256m -Xmx512m to this environment variable.

Install Lift 2.1 in MacOSX Leopard (II) – Install the Scala Language

2) Install the Scala Language:

I open the finder and choose the option Go to Folder select /usr/local/, then we rename the folder to Scala and we copied the folder there.We could also move it by Terminal using the following command:

$ sudo mv ~/Downloads/scala-2.8.0.final /usr/local
$ sudo mv ~/Downloads/scala /usr/local

if we have already changed its file name.We edit the local PATH file. We open the Terminal application, we edit the .profile file (we can use vim editor or the TextEdit, or directly use Textmate if we have it installed using the mate ~/.profile).

open -a TextEdit .profile
SCALA_HOME=/usr/local/scala;
export $SCALA_HOME; export SCALA="$SCALA_HOME/bin/scala"
PATH=$SCALA_HOME/bin:$PATH; export PATH
source ~/.profile

To test it we can insert the following command on the Terminal application

scala -version

And the following sentence will appear:

Scala code runner version 2.8.0.final -- Copyright 2002-2010, LAMP/EPFL

Install Lift 2.1 in MacOSX Leopard (I) – Download the Scala Language

1) Download the Scala Language:

We download the Scala language file from the scala-lang site. We are going to install it following the instructions given by the following sites Installing Scala on Mac OS X Leopard and How to Install Scala on Mac. I have done a quick summary of the great tips:

  • We go the official Scala Web Site, http://www.scala-lang.org/downloads.
  • Download the installer for your environment and follow the instructions on the downloads page.
  • I download the scala-2.8.0.final.tgz, I extract it in the Downloads folder.