Monday, 26 September 2011

Get thee to sbt

I was struggling to work out all of what the maven prototype gave me, and then struggling with figuring out maven itself.  My gripes against maven aside, I came across this which walked through a "from the ground up" Hello, World application.  It's not the most complete "walk through", but it uses sbt (simple build tool).

New Skeleton Application

After following the instructions, my files look like:

project/build.properites is:

#Project properties
#Mon Sep 19 22:11:42 IST 2011
project.organization=philgra.org
project.name=contactdb
sbt.version=0.7.7
project.version=1.0
build.scala.versions=2.9.1
project.initialize=false

project/build/LiftProject.scala is:
import sbt._
class LiftProject(info: ProjectInfo) extends DefaultWebProject(info) {
val liftVersion = "2.4-M4"
override def libraryDependencies = Set(
"net.liftweb" %% "lift-webkit" % liftVersion % "compile->default",
"net.liftweb" %% "lift-common" % liftVersion % "compile->default",
"net.liftweb" %% "lift-mapper" % liftVersion % "compile->default",
"org.mortbay.jetty" % "jetty" % "6.1.22" % "test->default"
) ++ super.libraryDependencies
}

src/main/scala/bootstrap/liftweb/Boot.scala is:

package bootstrap.liftweb
import net.liftweb.http.LiftRules
import net.liftweb.sitemap.{SiteMap, Menu}
class Boot {
def boot {
LiftRules.addToPackages("org.philgra.contactdb")
LiftRules.setSiteMap(SiteMap(Menu("Home") / "index"))
}
}

src/main/org/philgra/contactdb/snippet/HelloWorld.scala is:

package org.philgra.contactdb.snippet
class HelloWorld {
def howdy = <span>World!! The current date is: {new java.util.Date}</span>
}

src/main/webapp/templates-hidden/default.html is:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:lift='http://liftweb.net'>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <meta name="description" content="" />
    <meta name="keywords" content="" />
    <title>Hello World</title>
</head>
<body>
    <lift:bind name="content" />
    <lift:Menu.builder />    
</body>
</html>

and src/main/webapp/index.html is:

<lift:surround with="default" at="content">
<h1>Hello World Snippet</h1>
Hello <lift:HelloWorld.howdy />
</lift:surround>

I won't go through the explanation given by Sanj, you can follow the link and get his explanation which seemed reasonable enough to me :)

From the sbt console, running the "update" and "jetty-run" commands gave me a nice and happily working "Hello, World" application which I can wrap my head around.  I'm sure that as I move into more complete things my LiftProject.scala will become more complicated, but as there are ample examples of folks using sbt to build their Scala projects I'll have good company.

And I won't have to contend with the farce that is XML-as-a-build-language.

Post Script: this didn't quite work.  I just realized that I ended up with the correct "index.html" but the HelloWorld.howdy piece ("World!! The current time is blah") and the Menu piece (basically the lift:surround piece) don't seem to have worked.  I will update this post when I get to the bottom of it...

Diagnosing the Problem
Looking at the HTML source coming through to the browser, we can sort of see what has gone wrong:

<lift:surround with="default" at="content">
<h1>Hello World Snippet</h1>
Hello <lift:HelloWorld.howdy /> </lift:surround>
Basically, the Lift code results aren't getting injected into the HTML.  Why not?



No comments:

Post a Comment