sbt

CBT, a Scala build tool alvin April 7, 2017 - 3:25pm

I haven’t tried it yet, but I was just reminded that Christopher Vogt (and others) have created a build tool named CBT as an alternative to 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.)

How to run external shell commands in SBT (Simple Build Tool) alvin September 13, 2016 - 8:07pm

To run external shell commands in SBT, first start SBT from your operating system command line:

$ sbt

Then run the consoleProject task/command:

> consoleProject

After some output you’ll see this prompt:

scala>

Now you can execute shell commands by including them in double quotes, and following them by an exclamation mark, like this:

scala> "ls -al" !

For more information, see the SBT consoleProject documentation page.

How to start a Scala REPL session inside SBT (Simple Build Tool)

Scala FAQ: How do I start a Scala REPL session from SBT (i.e., inside the Simple Build Tool)?

Answer: Type console to start a REPL session from inside SBT. This (a) compiles your Scala project and then (b) starts a REPL session.

If you don’t want to compile your project before starting a REPL session from inside SBT, type consoleQuick instead.

How to run a Scala SBT-packaged jar file with Java (the `java` command)

If you want to run/execute a main method from a jar file you created with Scala and the sbt package command, this little tutorial shows how to do it. To make things a little more complicated, my Scala project depends on three external jar files, and the main method requires a command-line argument.

As noted in the Summary, you’ll probably want to use a tool like SBT-Assembly for larger projects.

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.