SBT : Where’s my NUGET

 

This article assumed 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 everyting that SBT does, It is after all a complete build system.

So let’s concentrate on a few things that are common place 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.

Lets 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

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 a 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:

http://www.scala-sbt.org/0.12.4/docs/Detailed-Topics/Publishing.html#publishing-locally

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: