build.sbt

SBT: Example build.sbt variables/settings (from PPrint) alvin August 31, 2017 - 9:47am

Because I think it’s often best to “learn by example,” I’ve become a connoisseur of SBT build.sbt examples, and this build.sbt file from Lihaoyi’s PPrint project demonstrates a lot of SBT variables:

SBT: How to get started using the Build.scala file (instead of build.sbt)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.16, “Using Build.scala Instead of build.sbt.”

Problem

In an SBT project, you want to use the project/Build.scala file instead of build.sbt to define your Scala project, or you need some examples of how to use Build.scala to solve build problems that can’t be handled in build.sbt.

How to configure SBT to find a repository (working with resolvers)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.11, “Telling SBT How to Find a Repository (Working with Resolvers).”

Problem

In a Scala SBT project, you want to add a managed dependency to your project from an Ivy repository that SBT doesn’t know about by default.

Scala/SBT: How to specify a main method/class to run

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.9, “Specifying a Main Class to Run with SBT.”

Problem

In a Scala SBT project, you have multiple main methods in objects in your project, and you want to specify which main method should be run when you type sbt run, or specify the main method that should be invoked when your project is packaged as a JAR file.

How to set the SBT logging level

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.13, “Setting the SBT Log Level.”

Problem

You’re having a problem compiling, running, or packaging your project with SBT, and need to adjust the SBT logging level to debug the problem. (Or, you’re interested in learning about how SBT works.)

Solution

Set the SBT logging level in your build.sbt file with this setting:

SBT: How to deploy a single, executable Jar file

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.14, “How to Deploy a Single, Executable JAR File.”

Problem

You’re building a Scala application, such as a Swing application, and want to deploy a single, executable JAR file to your users.

How to use ScalaCheck in the SBT console

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.)

An Akka actors ‘remote’ example

While doing some crazy things with SARAH, I realized that the best way to solve a particular problem was to use remote Akka actors. I haven’t had the opportunity to work with Akka much since finishing the Scala Cookbook, so I dug around trying to find a simple Akka remote “Hello, world” example. Unable to find a good one, I read some stuff, and created it myself.