Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collecting dependencies under sbt 0.10 (putting all dependency jars to target/scala-version/lib/)

Tags:

scala

sbt

I'm using sbt 0.10 with Scala 2.9.1.

I've read http://groups.google.com/group/simple-build-tool/browse_thread/thread/792e5360877e78/987b6af687b8e33b?lnk=gst&q=collect+jars#987b6af687b8e33b

However I don't want a one huge jar. I just want dependency jars ending up in target/scala-2.9.1.final/lib directory.

The reason why I don't want a one huge jar, that project uses a lot of libs, and usually only the application .jar changes. However as this is multi-language project and not all team members have scala or sbt, jars are just commited to git. Having one huge dar updated regullary would inflate repo size.

How can I copy those dependencies? ;)

like image 748
arturaz Avatar asked Sep 08 '11 16:09

arturaz


People also ask

Which is the correct way to add dependencies in SBT file?

Library dependencies can be added in two ways: unmanaged dependencies are jars dropped into the lib directory. managed dependencies are configured in the build definition and downloaded automatically from repositories.

Where are dependencies stored in SBT?

If you have JAR files (unmanaged dependencies) that you want to use in your project, simply copy them to the lib folder in the root directory of your SBT project, and SBT will find them automatically.

What is transitive dependency in SBT?

SBT Dependencies SBT is the most usual build tool for Scala projects. As a project gets more complex, it will increase the number of dependencies. And each dependency brings other dependencies, called transitive dependencies. Eventually, a project can suffer from the JAR dependency hell.


2 Answers

You didn't specify the ivy configuration to copy the configurations from but here's a fully working example that will copy all your managed dependencies to the target/<scala version>/lib folder (scala version is given by the crossTarget task). Just copy this and put it in the file project/Build.scala (or whatever filename you want) :

import sbt._
import Keys._

object MyBuild extends Build {

  lazy val copyDependencies = TaskKey[Unit]("copy-dependencies")

  def copyDepTask = copyDependencies <<= (update, crossTarget, scalaVersion) map {
    (updateReport, out, scalaVer) =>
    updateReport.allFiles foreach { srcPath =>
      val destPath = out / "lib" / srcPath.getName
      IO.copyFile(srcPath, destPath, preserveLastModified=true)
    }
  }

  lazy val root = Project(
    "root",
    file("."),
    settings = Defaults.defaultSettings ++ Seq(
      copyDepTask
    )
  )
}

If you want a specific configuration, replace

updateReport.allFiles by updateReport.select(configuration = Set("compile")) or whatever ivy configuration you want.

like image 71
Fred Dubois Avatar answered Nov 13 '22 21:11

Fred Dubois


The various "Classpath" keys contain different views onto the classpath, but probably the easiest thing is to modify the sbt-assembly plugin to simply drop the jars into a directory instead of doing the unzip/rezip cycle. The plugin is very small (only 150 lines) and it's pretty obvious from a cursory glance what the right place to change is.

like image 23
RM. Avatar answered Nov 13 '22 22:11

RM.