Scala SBT

SBT (Simple Build Tool)

– It is a build tool for Scala projects
– Just like Ant and Maven for Java Projects
– Used to handle both Managed and Unmanaged dependencies

File: build.sbt

– Used to define project dependencies and other project settings
– Settings are specified using key value pairs
– It’s not needed for very simple projects

SBT Commands

SBT Version:

$ sbt version -> Gives the Project Version
$ sbt sbt-version -> Gives the SBT Version
$ sbt sbtVersion -> Gives the SBT Version (Windows only)
$ sbt about -> Gives the SBT, Scala and Plugin info

Create Directories:

[Refer: Shell script from GitHub Gist -> https://gist.github.com/alvinj/3194379 -> sbtmkdirs.sh]
$ sbtmkdirs
Project Name: HelloWorld

Alternate Approach:

Linux:
$ mkdir HelloWorld
$ cd HelloWorld
$ mkdir -p src/main/java
$ mkdir -p src/main/scala
$ mkdir -p src/main/resources
$ mkdir -p src/test/java
$ mkdir -p src/test/scala
$ mkdir -p src/test/resources
$ tree
$ rm -R HelloWorld

Windows:
C:\> mkdir HelloWorld
C:\> cd HelloWorld
C:\> mkdir src\main\java
C:\> mkdir src\main\scala
C:\> mkdir src\main\resources
C:\> mkdir src\test\java
C:\> mkdir src\test\scala
C:\> mkdir src\test\
C:\> tree
C:\> rmdir /S HelloWorld

View Directory Structure:

$ cd HelloWorld
$ tree

Compile Application:

$ sbt compile

Run Application:

$ sbt run

Create JAR file:

$ sbt package

Run the Scala Jar:

$ scala target/scala-2.10/helloworld_2.10-1.0.jar

SBT Shell:

$ cd HelloWorld
$ sbt
> run
> exit

$ sbt
> update
> console
scala> // REPL
scala> sys.exit
> exit

Several Commands:

$ sbt clean run

Create Uber JAR file:

$ sbt assembly

Samples

build.sbt

name := “HelloWorld”

version := “1.0”

scalaVersion := “2.10.3”

resolvers ++= Seq(
“Typesafe Repository” at “http://repo.typesafe.com/typesafe/releases/”,
“oracle driver repo” at “http://mirrors.ibiblio.org/maven2/”
)

libraryDependencies ++= Seq(
“com.typesafe.akka” %% “akka-actor” % “2.1.2”,
“com.typesafe” % “config” % “1.2.1”
)

scalacOptions += “-deprecation”

unmanagedJars in Compile += file(“lib/ojdbc6-11.1.0.7.0.jar”)

mainClass in assembly := some(“nag.arvind.gudiseva.app.HelloWorld”)
assemblyJarName := “nag_arvind_gudiseva.jar”

val meta = “””META.INF(.)*”””.r
assemblyMergeStrategy in assembly := {
case PathList(“javax”, “servlet”, xs @ _*) => MergeStrategy.first
case PathList(ps @ _*) if ps.last endsWith “.html” => MergeStrategy.first
case n if n.startsWith(“reference.conf”) => MergeStrategy.concat
case n if n.endsWith(“.conf”) => MergeStrategy.concat
case meta(_) => MergeStrategy.discard
case x => MergeStrategy.first
}

// put all libs in the lib_managed directory, that way we can distribute eclipse project files
retrieveManaged := true

HelloWorld.scala

Object HelloWorld extends App {
println(“Hello, World!”)
}

plugins.sbt

logLevel := Level.Warn
addSbtPlugin(“com.eed3si9n” % “sbt-assembly” % “0.12.0”)

References:

http://scala-sbt.org
http://alvinalexander.com/scala/simple-build-tool-sbt-video-tutorial

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s