5

我试图让 SBT 构建一个可能有多个非托管目录的项目。如果我有一个目录,我可以很容易地这样做:

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

但是由于我有两个带有非托管 jar 的目录,所以我需要能够将它们全部添加。我在这里找到了一些信息,但对于我的完整.scala文件构建似乎仍然没有用。

我创建了一个简单的项目,在这里显示问题。下面是我的Build.scala文件。

更新

我从sbt-users 列表中获得了一些帮助,并且能够正确定义非托管 jar,但代码仍然无法编译(但sbt show unmanaged-jars正确显示了文件)。

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
  }
}
4

4 回答 4

7

我只是使用 sbt 0.11.xbuild.sbt从我的文件中发布片段。它可能会被重构一下。

unmanagedJars in Compile <++= baseDirectory map { base =>
    val libs = base / "lib"
    val dirs = (libs / "batik") +++ (libs / "libtw") +++ (libs / "kiama")
    (dirs ** "*.jar").classpath
}
于 2012-02-19T11:37:15.447 回答
3

您可以将其他路径添加到文件夹列表以扫描非托管依赖项。例如,要在run任务的“lib”之外查找名为“config”的文件夹,您可以添加以下内容。将compile任务更改RuntimeCompile.

unmanagedClasspath in Runtime <+= (baseDirectory) map {
  bd => Attributed.blank(bd / "config")
}
于 2012-02-19T22:58:26.910 回答
2

正如Eugene Vigdorchik回答的那样,是什么使它像以下代码一样工作:

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
  }
}

源代码库以及Github 上提供的完整示例。

于 2012-02-28T11:25:44.773 回答
-1

这是递归加载非托管 JAR 的通用解决方案(对于 sbt 0.13.x):https ://stackoverflow.com/a/29357699/1348306

于 2015-03-30T22:55:53.600 回答