Saturday, 20 August 2011

Liftweb setup in 10 minutes - IDE and project configuration

Scala is one of the coolest programming languages out there and Lift is a revolutionary web framework that really leverages many scala features.

But it could be daunting at the beginning, even just getting your workspace setup. In this article I will go through the basic setup of your IDE, it really takes a few minutes!

Even though I've always been an eclipse person I'm using IDEA IntelliJ with scala cause I think is way superior.
Ok let's begin. You will need IntelliJ 10.5.1, Maven or SBT.

Create the project with maven

First step is to create the project and for that we will use lift maven archetype. For the persistence layer we will use the default lift mapper. Run this command
mvn archetype:generate   -DarchetypeGroupId=net.liftweb   
  -DgroupId=com.playamatch  -DartifactId=web
You can also create a JPA based project running the same command with

You already have a full liftweb project! You can even run it with maven itself using the command

mvn jetty-run

Download and configure IntelliJ

Next open IntelliJ and install the scala plugin. You need to go to File -> Setting. This is where you can access all the configuration options. The equivalent of Window -> Preferences in eclipse.
Now go to the plugins, pick scala right click "Download and Install" and once done restart the IDE.
You should be able to see your code with syntax highlights and navigate through the classes already.

If you want you can also download the SBT plugin for IDEA. In this tutorial I will use the command line.

Another configuration change you might want to do if you are coming from eclipse if the shortcuts. IDEA comes with an eclipse profile that will allow you to keep using all the handy shortcuts you've learned so far.

Configure SBT

I would recommend using SBT for your projects with the scala compiler is very fast ( well as fast as it gets really ;) ) Here I am using SBT 0.10.1

You need to create a build.sbt in the project root folder. This is what mine looks like, it is pretty much the standard dependencies.

name := "liftplay"

scalaVersion := "2.8.1"

seq(webSettings: _*)

// If using JRebel uncomment next line
//jettyScanDirs := Nil

resolvers += " Maven2 Repository" at ""

libraryDependencies ++= {

  val liftVersion = "2.3" // Put the current/latest lift version here


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

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

    "net.liftweb" %% "lift-wizard" % liftVersion % "compile->default")


// Customize any further dependencies as desired

libraryDependencies ++= Seq(

  "org.eclipse.jetty" % "jetty-webapp" % "7.3.0.v20110203" % "jetty", // For Jetty 7

  //"org.mortbay.jetty" % "jetty" % "6.1.22" % "jetty,test", // For Jetty 6, add scope test to make jetty avl. for tests

  "org.scala-tools.testing" % "specs_2.9.0" % "1.6.8" % "test", // For tests

  "junit" % "junit" % "4.8" % "test->default", // For JUnit 4 testing

  "javax.servlet" % "servlet-api" % "2.5" % "provided->default",

  "com.h2database" % "h2" % "1.2.138", // In-process database, useful for development systems

  "ch.qos.logback" % "logback-classic" % "0.9.26" % "compile->default" // Logging


Then create another file under project/plugins (create these folders in your root project directory if you don't have them) called again build.sbt

resolvers ++= Seq(

  "Web plugin repo" at "",

  Resolver.url("Typesafe repository", new""))(Resolver.defaultIvyPatterns)


//Following means libraryDependencies += "com.github.siasia" %% "xsbt-web-plugin" % "0.1.0-""

libraryDependencies <+= sbtVersion(v => "com.github.siasia" %% "xsbt-web-plugin" % ("0.1.0-"+v))

last file is the under the project folder
That's it. All you need to do now is an update to download all the dependencies

Get dependencies and run application

sbt update

and then you can run your new application
sbt ~jetty-run

Enable Debug for your application

If you want to debug your application just add the following to your sbt script
Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005

and then create a remote application debug configuration from Run -> Edit configurations. First check your defaults are like this then click on the plus sign

and name your run configuration!
Happy coding!!!

No comments: