如何在SBT中为所有子项目设置默认依赖项?

时间:2010-12-02 11:39:15

标签: scala sbt

尝试了解如何设置SBT subprojects。为我的所有子项目设置默认依赖项的正确方法是什么?

我试过这个,但是我的子项目没有找到任何依赖项(它们下载得很好)。

import sbt._

class MyProjects(info: ProjectInfo) extends DefaultProject(info)
{
  val projA = project("projA", "ProjectA")
  val projB = project("projB", "ProjectB")

  val akkaRepo = "Akka maven2 repo" at "http://www.scalablesolutions.se/akka/repository/"
  val multiverseRepo = "Multiverse maven2 repo" at "http://multiverse.googlecode.com/svn/maven-repository/releases/"
  val guiceyFruitRepo = "GuiceyFruit Maven2 repo" at "http://guiceyfruit.googlecode.com/svn/repo/releases/"
  val jBossRepo = "JBoss maven2 repo" at "https://repository.jboss.org/nexus/content/groups/public/"

  val junit = "junit" % "junit" % "4.5" % "test"
  val scalatest = "org.scalatest" % "scalatest" % "1.2" % "test"
  val akka = "se.scalablesolutions.akka" % "akka-core_2.8.0" % "0.10"
}

然后,根据this我尝试了以下内容。它奏效了,但这不是我期望的。是不是更简单的是为所有子项目设置默认依赖项?

import sbt._  

class MyProjects(info: ProjectInfo) extends DefaultProject(info)
{
  val projA = project("projA", "ProjectA", new Proj(_))
  val projB = project("projB", "ProjectB", new Proj(_))

  val akkaRepo = "Akka maven2 repo" at "http://www.scalablesolutions.se/akka/repository/"
  val multiversRepo = "Multiverse maven2 repo" at "http://multiverse.googlecode.com/svn/maven-repository/releases/"
  val guiceyFruitRepo = "GuiceyFruit Maven2 repo" at "http://guiceyfruit.googlecode.com/svn/repo/releases/"
  val jBossRepo = "JBoss maven2 repo" at "https://repository.jboss.org/nexus/content/groups/public/"

  class Proj(info:ProjectInfo) extends DefaultProject(info){
    val junit = "junit" % "junit" % "4.5" % "test"
    val scalatest = "org.scalatest" % "scalatest" % "1.2" % "test"
    val akka = "se.scalablesolutions.akka" % "akka-core_2.8.0" % "0.10"
  }
}

编辑:应该指出有better way使用Akka,但只是说明了我的观点。

3 个答案:

答案 0 :(得分:15)

使用继承和mixins:

import sbt._

class ModularProject(info: ProjectInfo) extends DefaultProject(info){

    lazy val childProject = project("projA", "ProjectA", 
        new DefaultProject(_)   
            with Repositories 
            with GlobalDependencies
            with AkkaDependencies)

    trait Repositories{
        lazy val akkaRepo = "Akka maven2 repo" at 
        "http://www.scalablesolutions.se/akka/repository/"
        lazy val multiversRepo = "Multiverse maven2 repo" at 
        "http://multiverse.googlecode.com/svn/maven-repository/releases/"
        lazy val guiceyFruitRepo = "GuiceyFruit Maven2 repo" at 
        "http://guiceyfruit.googlecode.com/svn/repo/releases/"
        lazy val jBossRepo = "JBoss maven2 repo" at 
        "https://repository.jboss.org/nexus/content/groups/public/"
    }

    trait GlobalDependencies{
        lazy val junit = "junit" % "junit" % "4.5" % "test"
        lazy val scalatest = "org.scalatest" % "scalatest" % "1.2" % "test"
    }

    trait AkkaDependencies{
        lazy val akka = "se.scalablesolutions.akka" % "akka-core_2.8.0" % "0.10"
    }       

}

答案 1 :(得分:1)

正常的解决方案是将依赖项放在每个子项目的类中,就像使用Proj类一样。通常每个子项目需要一个类,因为它们通常具有唯一的依赖项。

如果你很懒,可以在线声明具有依赖关系的类:

object Dependencies {
    ....
    lazy val jodaTime = "joda-time" % "joda-time" % ...
    lazy val scalaTime = "org.scala-tools" % "time" % ...
    lazy val redis = "com.redis" % "redisclient" % ...
}

val xBase = project("x-base", "x-base", new DefaultProject(_) {
    val jodaTime = Dependencies.jodaTime
    val scalaTime = Dependencies.scalaTime
  })

val xY = project("x-y", "x-y", new DefaultProject(_) { val redis = Dependencies.redis }, xBase)

在上面的示例中(对于产品x),xY模块取决于xBase模块。

Dependencies对象可以轻松地重用模块中的依赖项。

答案 2 :(得分:1)

自那以后,很多事情发生了变化,现在可以使用{{1>}在{{1}的根项目中使用project/RootBuild.scala “为所有子项目设置默认依赖项其他子项目(然后他们将设置分辨率委托给)如下:

aggregate

设置import sbt._ import Keys._ object RootBuild extends Build { override lazy val settings = super.settings ++ Seq(resolvers += "Akka maven2 repo" at "http://www.scalablesolutions.se/akka/repository/") } 后,子项目也会设置它。

resolvers
根项目的

[root]> resolvers [info] a/*:resolvers [info] List(Akka maven2 repo: http://www.scalablesolutions.se/akka/repository/) [info] b/*:resolvers [info] List(Akka maven2 repo: http://www.scalablesolutions.se/akka/repository/) [info] root/*:resolvers [info] List(Akka maven2 repo: http://www.scalablesolutions.se/akka/repository/) 如下:

build.sbt

请参阅sbt关于.scala Build Definition的官方文档。

然而,还有另一种(更好的?)方法来定义具有ThisBuild范围的通用配置。

lazy val root = project in file(".") aggregate (a, b)

lazy val a = project dependsOn b

lazy val b = project

使用上面的lazy val root = project in file(".") aggregate (a, b) lazy val a = project dependsOn b lazy val b = project resolvers in ThisBuild += "JBoss maven2 repo" at "https://repository.jboss.org/nexus/content/groups/public/" 构建定义和RootBuild.scala我使用build.sbt来设置整个构建的范围,构建配置最终导致默认有两个in ThisBuild在多项目设置中。

resolvers