如何为SBT中的所有子项目设置默认依赖关系?
尝试了解如何设置 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,但只是说明我的观点。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用继承和混合:
Use inheritance and mixins:
正常的解决方案是将依赖项放入每个子项目的一个类中,就像您对 Proj 类所做的那样。通常每个子项目都需要一个类,因为它们通常具有独特的依赖关系。
如果您很懒,您可以声明具有内联依赖项的类:
在上面的示例中(对于产品 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:
In the example above (for the product x), the
xY
module depends on thexBase
module.The Dependencies object makes it easy to re-use the dependencies in the modules.
此后发生了很多变化,在 sbt 0.13.x 中,现在可以在根项目中使用
project/RootBuild.scala
来“为所有子项目设置默认依赖项”聚合
其他子项目(然后将设置解析委托给它们),如下所示:设置了
resolvers
后,子项目也将进行设置。根项目的
build.sbt
如下:查阅sbt官方文档了解.scala 构建定义。
然而,还有另一种(更好?)方法来定义通用配置 ThisBuild 范围。
通过上面的
RootBuild.scala
构建定义和build.sbt
,我在 ThisBuild 中使用in ThisBuild
来确定整个构建的设置范围,构建配置最终在多项目设置中默认有两个解析器
。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 thataggregate
s the other subprojects (that they then delegate setting resolution to) as follows:With the
resolvers
set, the subprojects will have it set, too.build.sbt
for the root project is as follows: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.
With the above
RootBuild.scala
build definition andbuild.sbt
where I usedin ThisBuild
to scope the setting for the entire build, the build configuration ended up with tworesolvers
being default in the multi-project setup.