Sbt 在文件中或在多项目中的 commonSettings 中放置设置有啥区别

Posted

技术标签:

【中文标题】Sbt 在文件中或在多项目中的 commonSettings 中放置设置有啥区别【英文标题】:Sbt what is the difference placing settings in the file or in commonSettings in multi projectSbt 在文件中或在多项目中的 commonSettings 中放置设置有什么区别 【发布时间】:2019-06-22 08:27:52 【问题描述】:

初学者的问题,我有一个多项目的sbt文件,如果我把通用设置放在文件的开头有什么区别吗?例如:

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)

或者如果我把它放在通用设置中

lazy val commonSettings  = Seq(  
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
  ,
libraryDependencies ++= commonDependencies ++ testingDependencies)
lazy val sharedProject = (project in file(...))
.settings(commonSettings: _*)
val projectA = .....dependsOn(sharedPorject)
val projectB = .....dependsOn(sharedPorject)

有什么区别?

【问题讨论】:

【参考方案1】:

任何未附加到特定项目设置的设置,即.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 中并将其应用于所有子项目和根目录。这将具有与ThisBuild 范围内类似的效果,但更明确:

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

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

【讨论】:

以上是关于Sbt 在文件中或在多项目中的 commonSettings 中放置设置有啥区别的主要内容,如果未能解决你的问题,请参考以下文章

sbt / maven是否包含jar文件中的完整依赖项?

抑制 sbt-assembly 中的主类

Python:在现有 MIDI 文件中或在转换为 WAVE 文件期间指定乐器?

SBT:多构建依赖/聚合项目中的覆盖设置

Scala,docker - 如何使用 sbt-native-packager 在多模块应用程序中设置 mainClass?

GroupingError:错误:列必须出现在 GROUP BY 子句中或在聚合函数中使用