初学者问题,我有一个多项目 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)
有什么区别?
最佳答案
任何未附加到特定项目设置(即 .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
。
当根项目也定义了自己的组织
时,这将在示例中成立。
lazy val root = (project in file(".")).settings(organization := "foo")
lazy val common = (project in file("common")).settings(organization := "bar")
使用命令 sbt“showorganization”
和 sbt“show common/organization”
可以轻松测试这一点。它将分别打印 foo
和 bar
。
最后,如果您希望为所有子项目定义相同的值,请在根项目中为 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 中有何区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54417189/