SBT (Simple Build Tool) is a build environment
for Scala (and Java) applications. It features Maven-style dependency management,
a build definition language, pluggable architecture and an interactive shell.
Roughly akin to Maven for Java and Rake for Ruby, SBT can be used to build, run
and deploy apps and can work alongside both text editors and full IDE’s.

In this tutorial I’ll demonstrate how to create a new Scala project (“HelloSBT”) from scratch
with SBT, and an extra section on how to load the generated project into Intellij.
This tutorial has been tested with SBT 0.12.2 and Scala 2.10.0

You should see the output after SBT initialized and compiles your app:

Hello SBT

To finish this basic SBT project you need a build definition file, stored
in build.sbt at the root of the project. The .sbt file format is a
domain specific language (DSL) based on but not exactly like Scala
and contains mainly name-value pairs and library dependency
management.

After running package, exit the shell and check out your new project’s artifact:

target/scala-2.10/hellosbt_2.10-1.0.jar

Good news, you’ve created an SBT project!

Part 3 - Using External Libraries

Better news, since Scala is a JVM language you have access to the full (and massive) collection
of open source Java libraries. The main distribution mechanism for Java libraries
is Maven artifact repositories, and SBT has full
support for adding them to your project. In this section we’ll add the popular
Joda-Time
Date/Time management library to the project and update the application to
demonstrate it.

First open the build.sbt file created in Part 2 and add the following lines:

libraryDependencies += "joda-time" % "joda-time" % "1.6.2"

Note: SBT requires that settings in build.sbt must be separate by at least one blank line.

Now edit the Hi.scala file to make use of this new library, then we can try it out.

1234

objectHiextendsApp{valnow=neworg.joda.time.DateTime()println("Hi SBT, the time is "+now.toString("hh:mm aa"))}

1

$ sbt run

When you hit SBT will download the joda-time library from
a public Maven repository and cache it in a local repository.
You can find the library in ~/.ivy/cache since SBT uses the
Java-based Apache Ivy project for library management.

After the library is downloaded and your project is compiled, SBT will
display the new greeting with the current time.

Extra Credit: Open your SBT project in Intellij IDEA

I’m not going to argue the benefits of IDE’s vs Text Editors here,
and will gamely sidestep the Eclipse vs Intellij IDE battle
since this article is already long enough. However if you do like IDE’s
and are amenable to using
the free Intellij IDEA IDE, here’s how to get it to open your new SBT project
so you can write and execute applications from inside without going to
the command line.

This step uses Mike Peltonen’s excellent sbt-idea
plugin for SBT to convert SBT projects to Intellij format. You’ll need to install
the Intellij IDEA and its Scala plugin, which are
free and open source. See my previous blog post
Zero to Hello World in Scala
for download links and installation instructions.

When Intellij is installed, run this to install the sbt-idea plugin globally :

For many developers, the first big hurdle in learning a new language is
overcome when they can write and run a basic command-line application in an IDE.
Although Scala developers can use the excellent REPL that comes with
the scala app,
being able to write and run an application in the IDE provides a comfortable
foundation for the extensive learning curve of the Scala language (and of functional
programming in general).

This tutorial shows how to go from zero to running a basic “Hello, World” Scala application
in Intellij IDEA 12 CE IDE (the free, open-source version of Intellij IDEA).
The intructions cover OS X but should be applicable with minor effort on Windows
and Linux systems.

Right-click on the Scala plugin and choose *Download and Install” to install it.

After closing the dialog and restarting Intellij you are ready to create the project.

Part 2 - Create The Scala Project

In the “Intellij IDEA” dialog select Create New Project

When the “New Project” dialog appears, select Scala Module on the left side

Enter a name & location for your project

If the Project SDK dropdown is set to ”” click New.. to select your JDK installation directory.

On the right side of the dialog select Set Scala Home and enter “/usr/local/scala-2.10.0” in the text field immediately below this option. You should see a message that you have selected version 2.10.0, and the Compiler library and Standard library fields should be filled out.

Select the Make global libraries feature to ensure future Scala projects can use this configuration

Select Finish to create your new Scala project

Part 3 - Hello, Scala

When your project appears, click the 1: Project tab on the left side of the window to display the “Project” view. You can also select View -> Tool Windows -> Project from the menu bar. Even better, Intellij ought to have shown this view by default when you create a new project since you can’t get started without it!

Open your project and right-click on the src folder, then select New -> Scala Class in the contextual menu.

In the “Create New Scala Class” dialog enter the name “Hello” and set the Kind dropdown to “Object”. Click OK to create the new file.

Now your new Scala object is ready, but it requires some work before you can execute it.

Extend Scala’s App trait to make your object an entry point for an application. You can do this by changing object Hello to
object Hello extends App .

Add the idiomatic Hello, World statement. This simply requires writing println("Hello, World") in the body of the object.

Your code should look like this:

123

objectHelloextendsApp{println("Hello, World")}

To run your application, right-click on your Hello object in the Project pane on the left and choose Run ‘Hello.main()’ . After a short period compiling the program you should see the following in the Run view:

1

Hello, World

I hope you found this short tutorial to be useful. Note that although I have used Intellij IDEA in this article and
recommend it for Scala development, many Scala developers prefer the Scala IDE for Eclipse IDE instead.

Everything was fine when your method was returning the expected
Some wrapper in development… unit tests… staging… and production.
But then came an unexpected condition when None was returned
and your code wasn’t expecting it. None.get will instead throw
the above exception, probably
in a place where you weren’t expecting to catch one.

Recommendation: Always use Option.getOrElse instead of Option.get

Use Option.getOrElse to provide a default value or behavior when your
reference is empty.

If your reference is defined, getOrElse returns the unwrapped value

If your reference is empty, getOrElse returns the result of your function literal
or default value

By providing a workaround for empty references, you are making your application
sturdier and emphasizing how it handles unexpected conditions. Future maintainers
of your code (and you should always plan for them) will thank you.

Note: your function literal is not actually required to return a valid default
value here. In cases where an exception is more appropriate there would not
actually be anything returned:

The List.head()
method in Scala’s List collection class provides a simple way
to get the first element in the list, but if the list is empty it will
throw this exception (since there is nothing
it can actually return)

You can prevent this by checking that the list is non-empty before calling this method.
A better (and more Scala-ish) way is to instead call List.headOption()
which uses Scala’s Option pattern.

How to handle the case of an empty list depends on your requirements and context,
especially what you plan[ned] to do with the first element of the list.
If you can defer handling the empty list until the value is used,
keep the Option wrapper around the element.

1

vallatestUser:Option[String]=users.headOption

Or does an empty list denote a state so broken that you cannot continue?