build.sbt

How to use ScalaCheck in the SBT REPL (command line)

If you add ScalaCheck to an SBT project like this:

libraryDependencies += "org.scalacheck" %% "scalacheck" % "1.13.4" % "test"

it’s only available in the SBT “test” scope. This means that when you start a Scala REPL session inside of SBT with its console command, the ScalaCheck library won’t be available in that scope.

To use ScalaCheck with the SBT console (REPL), don’t use its console command — use test:console instead. A complete example looks like this:

$ sbt

> test:console

scala> import org.scalacheck.Gen.choose

Note that after you type test:console your project may be compiled, so that step may take a few moments.

In summary, use SBT’s console command to start a “normal” Scala REPL inside SBT, and use test:console to start a REPL that you can run tests inside of. (Note that this same advice also applies to using ScalaTest or specs2.)

Scala: How to download and process XML data (such as an RSS feed)

I was looking for a good way to access XML resources (like RSS feeds) in Scala, and I currently like the idea of using ScalaJ-HTTP to access the URL and download the XML content, and then using the Scala XML library to process the XML string I download from the URL.

This example Scala program shows my current approach:

How to configure SBT to work with Eclipse

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 18.7, “How to configure SBT to work with Eclipse.”

Problem

You want to use Eclipse with a project you’re managing with SBT.

Solution

Use the Scala IDE for Eclipse project so you can work on Scala projects in Eclipse, and use the “sbteclipse” plug-in to enable SBT to generate files for Eclipse.

How to create an SBT project with subprojects

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 18.6, “How to create an SBT project with subprojects.”

Problem

You want to configure SBT to work with a main project that depends on other subprojects you’re developing.

Solution

Create your subproject as a regular SBT project, but without a project subdirectory. Then, in your main project, define a project/Build.scala file that defines the dependencies between the main project and subprojects.

SBT: How to control which version of a managed dependency is used

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 18.5, “SBT: How to control which version of a managed dependency is used.”

Problem

You want to make sure you always have the desired version of a managed dependency, including the latest integration release, milestone release, or other versions.

How to manage project dependencies with SBT (Simple Build Tool)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.4, “How to manage dependencies with SBT (Simple Build Tool).”

Problem

You want to use one or more external libraries in your Scala/SBT projects.

Solution

You can use both managed and unmanaged dependencies in your SBT projects.

How to run Scala project tests with SBT and ScalaTest

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.3, “How to run tests with SBT and ScalaTest.”

Problem

You want to set up an SBT project with ScalaTest, and run the tests with SBT.

Solution

Create a new SBT project directory structure as shown in Recipe 18.1, and then add the ScalaTest library dependency to your build.sbt file, as shown here:

A large Scala SBT build.sbt example

Wow, the following code might be the largest Scala SBT build.sbt configuration file I’ve ever seen. It comes from this project.

Here’s the build.sbt file contents:

How to set the Scala version in the SBT build.sbt configuration file

SBT FAQ: How do I set the desired Scala compiler version in the SBT build.sbt configuration file?

Set the value of the scalaVersion variable in your SBT build.sbt file. For instance, to use Scala 2.9.2, put an entry like this in the build.sbt file:

scalaVersion := "2.9.2"

To use Scala 2.10.0, put an entry like this in the build.sbt file: