Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / NuGet

SBT: Where’s my NUGET

0.00/5 (No votes)
14 Oct 2015CPOL3 min read 8.5K  
In this post, I will go through some of the common things you may want to do with SBT.

This article assumes you have done the other stuff in getting the tools to create SCALA projects.

SBT stands for Scala Build Tool. This tool is kind of equivalent of Nuget in .NET, you can use SBT to download dependencies and also to compile your own code into JAR files, much the same way you would have with Nuget.exe where you would have created .nupkg for .NET projects.

Going through every feature of SBT is slightly out of scope for this post, however I will go through some of the common things you may want to do with SBT.

For the full walk through, you should check out these 2 links:

Like I say, I will not be able to cover everything that SBT does. It is after all a complete build system.

So let’s concentrate on a few things that are commonplace when using some sort of package management / build system.

How Do I Build My Own Scala Code To A JAR

So when you create a new SBT project in IntelliJ (my chosen) IDE.

Let's say I had created a new project in IntelliJ that looked like this:

You will get a default SBT file that looks like this:

name := "SBTDemo" version := "1.0" scalaVersion := "2.11.7"

This simply specifies the name of the:

image

Where I have a Scala class like this:

Java
object SBTDemo { def main(args: Array[String]) = { println("Hi!") System.in.read() () } }

We can create a new JAR for this using the following procedure:

  1. Open a command prompt where you created the scala project
  2. Check that folder is the one with the built.sbt folder in
  3. Type SBT in command line window
  4. This will give you a SBT context, and then you should be able to enter SBT command directly
    • So we should be able to type “SBT package”, which should package the current project into a JAR file. For me, this was this simple TEST project that created the following JAR file:

image

You may be thinking great so now I have a JAR can SBT run it. Well no, you would tend to run the actual JAR through Java or Scala command line. Here is an example Scala command line to run the example JAR file produced by this simple SBT example.

image

How Do I Get Dependencies

Another really common thing to want to do is to reference external dependencies. These dependencies are just like Nuget packages in .NET, they are hosted on the web there is a package repository, and they may be brought down into the project using SBT.

These dependencies are all mainly available from the Maven Central Repository or mvnrepository.com if you prefer that.

Once you have found what you would like to reference, you can update you SBT file.

For example, suppose you wanted to use RabbitMQ, we would update our SBT file to look like this:

name := "SBTDemo" version := "1.0" scalaVersion := "2.11.7" 
	libraryDependencies += "com.rabbitmq" % "amqp-client" % "3.5.6"

If you are using an IDE, the IDE will probably download the dependencies for you, you can however use a SBT command line, and issue the SBT command “update”.

SBT will bring all 3rd party JARS down to your IVY2 folder which should be something like:

C:\Users\sacha\.ivy2\cache

Building Your Own Code as a Shared JAR

Imagine you have some shared code and you want different projects to reference this as a dependency (think project reference, or DLL in .NET).

We now know we can use SBT to resolve 3rd party dependencies, but how about our own code. How does that get into the IVY2 cache folder. This is the job of SBT PublishLocal.

Here is a good guide on how to do that:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)