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

4

5

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
  }
}
Aubrey answered 19/2, 2012 at 3:41 Comment(0)
A
2

As answered by Eugene Vigdorchik, what made it work as the following code:

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

object Build extends sbt.Build {

  import Dependencies._

  var unmanagedListing = unmanagedJars in Compile :=  {
    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(
        C.akkaActor,
        C.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 C {
    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
  }
}

Source repo with the full example available on Github.

Aubrey answered 28/2, 2012 at 11:25 Comment(0)
E
8

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
}
Erminois answered 19/2, 2012 at 11:37 Comment(7)
I dont have a build.sbt file, were do i place this on my Build.scala file?Adalbertoadalheid
@MaurícioLinhares Have you tried simply adding those lines to your Build.scala file?Erminois
@MaurícioLinhares Have you adapted my code? It is for directories lib/batik, lib/libtw and lib/kiama.Erminois
Of course I have. Adding prints on it shows it is never called, just as my own code above, so it did not overrite the unmanagedJars task.Adalbertoadalheid
Maybe the problem is that you are actually overriding unmanagedJars with the very first line in your Build. Have you tried just using my code and not fiddling with unmanagedJars otherwise?Erminois
@MaurícioLinhares Are you sure you wrote unmanagedJars in Compile, exactly like that, including upper and lower cases?Cartage
Here is a project github.com/mauricio/sbt-with-many-unmanaged-repos that represents the issue, this is exactly what I am doing.Adalbertoadalheid
C
3

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")
}
Collen answered 19/2, 2012 at 22:58 Comment(1)
Didn't work. A sample project showing the issue is here -> github.com/mauricio/sbt-with-many-unmanaged-repos - any help is appreciated.Adalbertoadalheid
A
2

As answered by Eugene Vigdorchik, what made it work as the following code:

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

object Build extends sbt.Build {

  import Dependencies._

  var unmanagedListing = unmanagedJars in Compile :=  {
    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(
        C.akkaActor,
        C.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 C {
    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
  }
}

Source repo with the full example available on Github.

Aubrey answered 28/2, 2012 at 11:25 Comment(0)
T
-1

Here's a general solution to recursive loading of unmanaged JARs (for sbt 0.13.x): https://mcmap.net/q/1924134/-sbt-accessing-sub-directories

Turtleneck answered 30/3, 2015 at 22:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.