SBT在文件或多项目中的commonSettings中放置设置有什么区别

时间:2019-01-29 08:59:17

标签: sbt sbt-assembly sbt-plugin

初学者的问题,我有一个多项目sbt文件,如果我在文件的开头放置常规设置,会有区别吗?例如:

<Album>

或者如果我将其设置为常用设置

organization := "com.example"
  version := "0.0.1-SNAPSHOT"
  scalaVersion := "2.11.12"
resolvers ++= Seq(
    "Apache Development Snapshot Repository" at "https://repository.apache.org/content/repositories/snapshots/",
    "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/",

    Resolver.mavenLocal
  )
assemblyMergeStrategy in assembly := {
    case PathList("META-INF", xs @ _*) => MergeStrategy.discard
    case x => MergeStrategy.first
  }
lazy val commonSettings  = Seq(  libraryDependencies ++= commonDependencies ++ testingDependencies)
lazy val sharedProject = (project in file(...))
.settings(commonSettings: _*)
val projectA = .....dependsOn(sharedPorject)
val projectB = .....dependsOn(sharedPorject)

有什么区别?

1 个答案:

答案 0 :(得分:2)

定义的任何未附加到特定项目的设置的设置,即.settings(),都将附加到根项目。

这样的代码

organization := "foo"

相同
lazy val root = (project in file(".")).settings(organization := "foo")

现在,如果您定义了一个新的子项目,例如common,并向其中添加了organization

lazy val common = (project in file("common")).settings(organization := "bar")

并且只有它的值organization设置为bar

在根项目也定义了自己的organization的情况下,这将保留在示例中。

lazy val root = (project in file(".")).settings(organization := "foo")

lazy val common = (project in file("common")).settings(organization := "bar")

使用命令sbt "show organization"sbt "show common/organization"可以轻松测试。它将分别打印foobar

最后,如果您希望为所有子项目定义相同的值,请在根项目中为范围ThisBuild添加设置,如下例所示:

organization in ThisBuild := "foo"

lazy val common = (project in file("common")).settings(???)

或将设置存储在Seq中,并将其应用于所有子项目和root。这将与作用域ThisBuild中的作用类似,但更加明确:

val commonSettings = Seq(organization := "foo")

lazy val root = (project in file(".")).settings(commonSettings)
lazy val common = (project in file("common")).settings(commonSettings)