build.sbt

SBT: How to get started using the Build.scala file (instead of build.sbt) alvin July 10, 2017 - 4:49pm

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) alvin July 10, 2017 - 4:21pm

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 alvin July 10, 2017 - 4:11pm

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 alvin July 10, 2017 - 2:52pm

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 alvin July 10, 2017 - 2:40pm

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.

An example Scala ‘App’ for the Cats “IO Monad” article alvin June 11, 2017 - 7:38pm

As a quick note, if you’re interested in using the IO monad described in this IO Monad for Cats article, here’s the source code for a complete Scala App based on that article:

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

The SBT syntax to add multiple library dependencies (Scala) alvin March 6, 2017 - 3:13pm

As a quick note, the Scala Simple Build Tool (SBT) syntax to add multiple library dependencies is this:

libraryDependencies ++= Seq(
  "com.typesafe.akka" %% "akka-actor" % "2.1.1",
  "com.typesafe.akka" %% "akka-remote" % "2.1.1"
)
An Akka actors remote example alvin February 28, 2017 - 8:55pm

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.

Scala: How to download and process XML data (such as an RSS feed) alvin September 13, 2016 - 8:58pm

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: