Sbt

From Knowitall
Jump to: navigation, search

SBT is a build system for Scala/Java. Many of our projects use it now. You can get sample build files from our projects.

SBT Eclipse is a plugin for SBT that creates Eclipse projects for you based on your build. After setting it up, you just go to the root folder of any sbt project and type: sbt eclipse. Then, from within Eclipse, you "import an existing Eclipse project."

SBT Assembly is a plugin that lets you create huge .jar files with all the dependencies you need for a particular class. This is mostly useful for creating jars that you can run on Hadoop. See here or here to see how to set up your build file. When you have it set up, you just run sbt assembly to make your jar file.

Useful commands for sbt are:

  • sbt compile, for compiling, of course.
  • sbt test, which builds and runs tests.
  • sbt 'run arg1 arg2 ...' to run code. If there's more than one main class, it will prompt you to select a number for which one. You can automate this by just passing in the number through standard input, i.e., echo 2 | sbt 'run arg1 arg2 ...' Or,
  • sbt 'run-main edu.washington.cs.knowitall.MyMainClass arg1 arg2 ...'
  • sbt publish-local, which is like mvn install. It saves the artifacts from the current project into a local Ivy repository, which other projects can use.
  • sbt clean for when you want to clean your project.

You can also run sbt in interactive mode by just typing sbt. One helpful command is ~ compile, which recompiles your code whenever you save a file.

How to create jar files in SBT

You need the sbt assembly plugin, which will create a .jar file bundled with all the dependencies your program needs.

Add this to the top of your build.sbt:

 import AssemblyKeys._
assemblySettings

And add this to the body of your build.sbt:

 mainClass in assembly := Some("edu.washington.cs.knowitall.my.MainClass")
mergeStrategy in assembly <<= (mergeStrategy in assembly) { (old) => { case x => { val oldstrat = old(x) if (oldstrat == MergeStrategy.deduplicate) MergeStrategy.first else oldstrat } } }

This sets the main class and resolves conflicting dependencies.

If you have a Build.scala file instead, then the process is similar.