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

发布于 2024-10-05 19:19:17 字数 2148 浏览 10 评论 0 原文

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

我尝试了这个,但是我的子项目没有获取任何依赖项(它们下载得很好)。

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

然后,基于这个我尝试了以下操作。它有效,但这不是我期望要做的。是否有更简单的方法为所有子项目设置默认依赖项?

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

编辑:应该指出有一个更好的方法来使用Akka,但只是说明我的观点。

Trying to understand how to set up SBT subprojects. What is the correct way to set default dependencies for all my sub projects?

I tried this, but my sub projects weren't picking up any of the dependencies (they downloaded fine).

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

Then, based on this I tried the following. It worked, but it's not what I was expecting to have to do. Isn't there a simpler was to set default dependencies for all subprojects?

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

Edit: Should point out there is an better way to use Akka, but was just illustrating my point.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

山川志 2024-10-12 19:19:17

使用继承和混合:

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

}

Use inheritance and 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"
    }       

}
穿透光 2024-10-12 19:19:17

正常的解决方案是将依赖项放入每个子项目的一个类中,就像您对 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 对象可以轻松地重用模块中的依赖关系。

The normal solution is to put the dependencies in a class for each sub project, just like you did with the Proj-class. Usually you need one class per sub project, since they often have unique dependencies.

If you are lazy, you can declare the class with the dependencies in-line:

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)

In the example above (for the product x), the xY module depends on the xBase module.

The Dependencies object makes it easy to re-use the dependencies in the modules.

俏︾媚 2024-10-12 19:19:17

此后发生了很多变化,在 sbt 0.13.x 中,现在可以在根项目中使用 project/RootBuild.scala 来“为所有子项目设置默认依赖项” 聚合其他子项目(然后将设置解析委托给它们),如下所示:

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如下:

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

lazy val a = project dependsOn b

lazy val b = project

查阅sbt官方文档了解.scala 构建定义

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

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,我在 ThisBuild 中使用 in ThisBuild 来确定整个构建的设置范围,构建配置最终在多项目设置中默认有两个解析器

[root]> resolvers
[info] a/*:resolvers
[info]  List(Akka maven2 repo: http://www.scalablesolutions.se/akka/repository/, JBoss maven2 repo: https://repository.jboss.org/nexus/content/groups/public/)
[info] b/*:resolvers
[info]  List(Akka maven2 repo: http://www.scalablesolutions.se/akka/repository/, JBoss maven2 repo: https://repository.jboss.org/nexus/content/groups/public/)
[info] root/*:resolvers
[info]  List(Akka maven2 repo: http://www.scalablesolutions.se/akka/repository/, JBoss maven2 repo: https://repository.jboss.org/nexus/content/groups/public/)

A lot has changed since, and with sbt 0.13.x it's now possible "to set default dependencies for all my sub projects" using project/RootBuild.scala in the root project that aggregates the other subprojects (that they then delegate setting resolution to) as follows:

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/")
}

With the resolvers set, the subprojects will have it set, too.

[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 for the root project is as follows:

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

lazy val a = project dependsOn b

lazy val b = project

Consult the official documentation of sbt about .scala Build Definition.

There's however another (better?) way to define a common configuration with the ThisBuild scope.

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/"

With the above RootBuild.scala build definition and build.sbt where I used in ThisBuild to scope the setting for the entire build, the build configuration ended up with two resolvers being default in the multi-project setup.

[root]> resolvers
[info] a/*:resolvers
[info]  List(Akka maven2 repo: http://www.scalablesolutions.se/akka/repository/, JBoss maven2 repo: https://repository.jboss.org/nexus/content/groups/public/)
[info] b/*:resolvers
[info]  List(Akka maven2 repo: http://www.scalablesolutions.se/akka/repository/, JBoss maven2 repo: https://repository.jboss.org/nexus/content/groups/public/)
[info] root/*:resolvers
[info]  List(Akka maven2 repo: http://www.scalablesolutions.se/akka/repository/, JBoss maven2 repo: https://repository.jboss.org/nexus/content/groups/public/)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文