Simple Java builds with SBT
SBT is a build and dependency management tool written in Scala. When I need to quickly setup a project with some dependencies, I often use SBT, because setting up a simple build is very easy and takes much less typing than with Maven.
There are also a couple of other advantages with SBT, namely:
-
sbt automatically recognises your application’s main class and can launch the application just by typing
sbt run
(no need for a plugin like the Maven exec plugin just to run the application). -
sbt can watch for changes to files and automatically recompile in the background as you work
Basics
All you need is to organise your build is to create a file named build.sbt
.
Then include the project name and version, that you can setup like this: name := “my new project”
version := "0.1"
scalaVersion := "2.9.1"
If you are building a Java project, the Scala version is not really important. Just use the one that comes with your SBT.
Dependencies
The next thing that you want to use a build tool for is automatically resolve dependencies and put them in your path.
This can be achieved like this: libraryDependencies += “com.example” % “software” % “0.2.0”
+=
is used instead of :=
because dependencies are a list you are appending to. The format is artifact group id, followed by the artifact name and the version. %
is simply a separator.
Repositories
If you need other repositories than Maven Central, you can add them like this: resolvers += “Repository name” at “address”
Again, at
is simply a separator defined by the configuration language.
That’s it! Your project should be ready to build.