Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
127 lines (82 sloc) 5.29 KB
layout title
Sbt Tutorial

We use sbt for building, testing, running and submitting assignments. This tutorial explains all sbt commands that you will use during our class. The Tools Setup page explains how to install sbt.

Starting up sbt

In order to start sbt, open a terminal ("Command Prompt" in Windows) and navigate to the directory of the assignment you are working on. Typing sbt will open the sbt command prompt.

shell$ cd /path/to/progfun-project-directory                        # This is the shell of the operating system
shell$ sbt
> _                                                                 # This is the sbt shell

Running the Scala Interpreter

You can start the Scala interpreter inside sbt using the console task. The interpreter (also called REPL, for "read-eval-print loop") is useful for trying out snippets of Scala code. Note that the interpreter can only be started if there are no compilation errors in your code.

In order to quit the interpreter and get back to sbt, type ctrl-d.

> console                                                           # In the sbt shell, we run the task `console`
[info] Starting scala interpreter...
Welcome to Scala version 2.10.0-M5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_33).
Type in expressions to have them evaluated.
Type :help for more information.

scala> println("Oh, hai!")                                          # This is the Scala REPL, type some Scala code
Oh, hai!

scala> val l = List(1, 2, 3)
l: List[Int] = List(1, 2, 3)

scala> val squares = => x * x)
squares: List[Int] = List(1, 4, 9)

scala>                                                              # Type [ctrl-d] to exit the Scala REPL
[success] Total time: 20 s, completed Aug 10, 2012 10:08:23 PM
>                                                                   # We're back to the sbt shell

Compiling your Code

The compile task will compile the source code of the assignment which is located in the directory src/main/scala.

> compile
[info] Compiling 4 Scala sources to /Users/luc/example/target/scala-2.10.0-M5/classes...
[success] Total time: 2 s, completed Aug 10, 2012 10:00:26 PM

If the source code contains errors, the error messages from the compiler will be displayed.

Testing your Code

The directory src/test/scala contains unit tests for the project. In order to run these tests in sbt, you can use the test command.

> test
[info] ListsSuite:
[info] - one plus one is two
[info] - sum of a few numbers *** FAILED ***
[info]   3 did not equal 2 (ListsSuite.scala:23)
[info] - max of a few numbers
[error] Failed: : Total 3, Failed 1, Errors 0, Passed 2, Skipped 0
[error] Failed tests:
[error]   example.ListsSuite
[error] {file:/Users/luc/example/}assignment/test:test: Tests unsuccessful
[error] Total time: 5 s, completed Aug 10, 2012 10:19:53 PM

Running your Code

If your project has an object with a main method (or an object extending the trait App), then you can run the code in sbt easily by typing run. In case sbt finds multiple main methods, it will ask you which one you'd like to execute.

> run
Multiple main classes detected, select one to run:

 [1] example.Lists
 [2] example.M2

Enter number: 1

[info] Running example.Lists 
main method!
[success] Total time: 33 s, completed Aug 10, 2012 10:25:06 PM

Running the Style Checker

As part of the grading process, we run a style checker on the submitted source code to find common coding style issues. To make sure that your code conforms to all our style rules, you can run the style checker yourself before submitting. The sbt task styleCheck does exactly that.

> styleCheck
[info] Checking file /Users/luc/Documents/epfl/teaching/progfun/assignments/src/main/scala/example/Lists.scala... OK!
[info] Processed 1 file(s)
[info] Found 0 errors
[info] Found 0 warnings
[info] Style Score: 100 out of 100
[success] Total time: 1 s, completed Aug 10, 2012 11:34:02 PM

Submitting your Solution to Coursera

The sbt task submit allows you to submit your solution for the assignment. It will pack your source code into a .jar file and upload it to the coursera servers. Note that the code can only be submitted if there are no compilation errors.

The submit tasks takes two arguments: your e-mail address and the submission password. NOTE: the submission password is not your login password. Instead, it's a special password generated by coursera. It is available on the Assignments page.

> submit suBmISsioNPasSwoRd
[info] Packaging /Users/luc/example/target/scala-2.10.0-M5/progfun-example_2.10.0-M5-1.0.0-sources.jar ...
[info] Done packaging.
[info] Compiling 1 Scala source to /Users/luc/example/target/scala-2.10.0-M5/classes...
[info] Connecting to coursera. Obtaining challenge...
[info] Computing challenge response...
[info] Submitting solution...
[success] Your code was successfully submitted: Your submission has been accepted and will be graded shortly.
[success] Total time: 6 s, completed Aug 10, 2012 10:35:53 PM
Something went wrong with that request. Please try again.