Search code examples
scalasbtfriend

How to get list of dependency jars from an sbt 0.10.0 project


I have a sbt 0.10.0 project that declares a few dependencies somewhat like:

object MyBuild extends Build {
    val commonDeps = Seq("commons-httpclient" % "commons-httpclient" % "3.1",
                         "commons-lang" % "commons-lang" % "2.6")

    val buildSettings = Defaults.defaultSettings ++ Seq ( organization := "org" )

    lazy val proj = Project("proj", file("src"),
        settings = buildSettings ++ Seq(
            name                    := "projname",
            libraryDependencies     := commonDeps, ...)

    ...
}

I wish to creat a build rule to gather all the jar dependencies of "proj", so that I can symlink them to a single directory.

Thanks.


Solution

  • Example SBT task to print full runtime classpath

    Below is roughly what I'm using. The "get-jars" task is executable from the SBT prompt.

    import sbt._
    import Keys._
    object MyBuild extends Build {
      // ...
      val getJars = TaskKey[Unit]("get-jars")
      val getJarsTask = getJars <<= (target, fullClasspath in Runtime) map { (target, cp) =>
        println("Target path is: "+target)
        println("Full classpath is: "+cp.map(_.data).mkString(":"))
      }
      lazy val project = Project (
        "project",
        file ("."),
        settings = Defaults.defaultSettings ++ Seq(getJarsTask)
      )
    }
    

    Other resources

    • Unofficial guide to sbt 0.10.
    • Keys.scala defines predefined keys. For example, you might want to replace fullClasspath with managedClasspath.
    • This plugin defines a simple command to generate an .ensime file, and may be a useful reference.