Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add jars from more than one unmanaged directory in an SBT .scala project configuration

I'm trying to get SBT to build a project that could have more than one unmanaged directory. If I had a single directory, I could easily do it like this:

unmanagedBase := file( "custom-libs" ).getAbsoluteFile

But since I have two directories with unmanaged jars, I need to be able to add them all. I have found some information in here, but still doesn't seem useful for my full .scala file build.

I have created a simple project that shows the issue in here. And below is my Build.scala file.

UPDATE

I've got some help form the sbt-users list and have been able to define the unmanaged-jars correctly, but the code still doesn't compile (but sbt show unmanaged-jars shows the files correctly).

import sbt._
import com.github.siasia._
import PluginKeys._
import Keys._

object Build extends sbt.Build {

  import Dependencies._

  val unmanagedListing = unmanagedJars :=  {
    Dependencies.listUnmanaged( file(".").getAbsoluteFile )
  }

  lazy val myProject = Project("spray-template", file("."))
    .settings(WebPlugin.webSettings: _*)
    .settings(port in config("container")  := 8080)
    .settings(
      organization  := "com.example",
      version       := "0.9.0-RC1",
      scalaVersion  := "2.9.1",
      scalacOptions := Seq("-deprecation", "-encoding", "utf8"),
      resolvers     ++= Dependencies.resolutionRepos,
      libraryDependencies ++= Seq(
        Compile.akkaActor,
        Compile.sprayServer,
        Test.specs2,
        Container.jettyWebApp,
        Container.akkaSlf4j,
        Container.slf4j,
        Container.logback
      ),
      unmanagedListing
    )

}

object Dependencies {
  val resolutionRepos = Seq(
    ScalaToolsSnapshots,
    "Typesafe repo" at "http://repo.typesafe.com/typesafe/releases/",
    "spray repo" at "http://repo.spray.cc/"
  )

  def listUnmanaged( base : RichFile ) : Keys.Classpath = {
    val baseDirectories = (base / "custom-libs") +++ ( base / "custom-libs2" )
    (baseDirectories ** "*.jar").classpath
  }

  object V {
    val akka    = "1.3"
    val spray   = "0.9.0-RC1"
    val specs2  = "1.7.1"
    val jetty   = "8.1.0.v20120127"
    val slf4j   = "1.6.4"
    val logback = "1.0.0"
  }

  object Compile {
    val akkaActor   = "se.scalablesolutions.akka" %  "akka-actor"      % V.akka    % "compile"
    val sprayServer = "cc.spray"                  %  "spray-server"    % V.spray   % "compile"
  }

  object Test {
    val specs2      = "org.specs2"                %% "specs2"          % V.specs2  % "test"
  }

  object Container {
    val jettyWebApp = "org.eclipse.jetty"         %  "jetty-webapp"    % V.jetty   % "container"
    val akkaSlf4j   = "se.scalablesolutions.akka" %  "akka-slf4j"      % V.akka
    val slf4j       = "org.slf4j"                 %  "slf4j-api"       % V.slf4j
    val logback     = "ch.qos.logback"            %  "logback-classic" % V.logback
  }
}
like image 793
Maurício Linhares Avatar asked Feb 19 '12 03:02

Maurício Linhares


People also ask

Can I use jar files in SBT projects?

You can use both managed and unmanaged dependencies in your SBT projects. 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.

How do I use code from a JAR file in Scala?

If you know that you want to use code from a JAR file when you start the REPL session, add the -cp or -classpath argument to your scala command when you start the session. This example shows how to load and use my DateUtils.jar library:

How do I use unmanaged dependencies in SBT?

There’s nothing to add to build.sbt to use unmanaged dependencies, though you could change the unmanagedBase key if you’d like to use a different directory rather than lib . To use custom_lib instead of lib :

Can I use Java libraries in my Scala project?

Ivy is also used by Ant and Maven, and as a result, you can easily use the wealth of Java libraries that have been created over the years in your Scala projects. There are two general forms for adding a managed dependency to a build.sbt file.


2 Answers

I just post the fragment from my build.sbt file, using sbt 0.11.x. It could probably be refactored a bit.

unmanagedJars in Compile <++= baseDirectory map { base =>
    val libs = base / "lib"
    val dirs = (libs / "batik") +++ (libs / "libtw") +++ (libs / "kiama")
    (dirs ** "*.jar").classpath
}
like image 138
ziggystar Avatar answered Sep 18 '22 23:09

ziggystar


You can add additional paths to the list of folders to scan for unmanaged dependencies. For example, to look in a folder called "config" in addition to "lib" for the run task, you can add the following. For the compile task change Runtime to Compile.

unmanagedClasspath in Runtime <+= (baseDirectory) map {
  bd => Attributed.blank(bd / "config")
}
like image 33
Connor Doyle Avatar answered Sep 19 '22 23:09

Connor Doyle