18.10. Using GitHub Projects as Project Dependencies

Problem

You want to use a Scala library project on GitHub as an SBT project dependency.

Solution

Reference the GitHub project you want to include in your project/Build.scala file as a RootProject.

For example, assuming you want to use the Scala project at https://github.com/alvinj/SoundFilePlayer as a dependency, put the following contents in a file named project/Build.scala in your SBT project:

import sbt._

object MyBuild extends Build {

  lazy val root = Project("root", file(".")) dependsOn(soundPlayerProject)
  lazy val soundPlayerProject =
       RootProject(uri("git://github.com/alvinj/SoundFilePlayer.git"))

}

You can now use that library in your code, as shown in this little test program:

package githubtest

import com.alvinalexander.sound._
import javazoom.jlgui.basicplayer._
import scala.collection.JavaConversions._
import java.util.Map

object TestJavaSound extends App {

  val testClip = "/Users/al/Sarah/Sounds/HAL-mission-too-important.wav"
  val player = SoundFilePlayer.getSoundFilePlayer(testClip)
  player.play

}

With this configuration and a basic build.sbt file, you can run this code as usual with the sbt run command.

Including this GitHub project is interesting, because it has a number of JAR files in its own lib folder, and compiling and running this example works fine.

Note that although this works well for compiling and running your project, you can’t package all of this code into a JAR file by just using the sbt package command. Unfortunately, ...

Get Scala Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.