我想创建一个具有继承和共享依赖项的SBT项目.
使用Maven的POM文件,可以设置Project Inheritance,您可以在其中设置父项目.我想和SBT做同样的事情.
xchange-stream
library使用Maven的项目继承来解析从父项目编译时的子项目依赖项.
这是我对文件结构的看法:
sbt-project/
project/
dependencies.scala # Contains dependencies common to all projects
build.sbt # Contains definition of parent project with references
# to subprojects
subproject1/
build.sbt # Contains `subproject3` as a dependency
subproject2/
build.sbt # Contains `subproject3` as a dependency
subproject3/
build.sbt # Is a dependency for `subproject1` and `subproject2`
其中project1和project2可以在其依赖项列表中包含project3,如下所示:
libraryDependencies ++= "tld.organization" % "project3" % "1.0.0"
这样当subproject1或subproject2通过从子目录中调用sbt compile编译时,或者当父sbt-project从主sbt-project目录编译时,子项目3将在本地编译并使用SBT发布,或以其他方式编写可用于需要它的项目.
另外,如何在sbt-project / build.sbt中或sbt-project / project目录中的任何位置指定共享依赖项,以便在子目录中调用sbt compile时它们可在subproject1和subproject2中使用?
以下示例无法回答以上任何一点:
> jbruggem/sbt-multiproject-example
:
使用递归的build.sbt文件,但不共享子项目之间的依赖关系.
> Defining Multi-project Builds with sbt:pbassiner/sbt-multi-project-example
:
对其子目录中的项目使用单个build.sbt文件.
> sachabarber/SBT_MultiProject_Demo
:
使用单个build.sbt文件.
最佳答案
Such that when
subproject1
orsubproject2
are compiled by invokingsbt compile
from within their subdirectories…
也许Maven意味着与shell环境和cd命令一起使用,但这并不是sbt至少在2019年sbt 1.x的工作方式.
sbt方式是使用sbt作为交互式shell,并在顶层启动它.然后,您可以调用编译为subproject1 / compile,或使用project subproject1切换到它,并在那里调用compile.
房子插件
通过创建自定义插件可以实现类似于父POM的功能.
package com.example
import sbt._
import Keys._
object FooProjectPlugin extends AutoPlugin {
override def requires = plugins.JvmPlugin
val commonsIo = "commons-io" % "commons-io" % "2.6"
override def buildSettings: Seq[Def.Setting[_]] = Seq(
organization := "com.example"
)
override def projectSettings: Seq[Def.Setting[_]] = Seq(
libraryDependencies += commonsIo
)
}
SBT-拉差
这不是你要求的,但我有一个实验插件,允许你在源依赖和二进制依赖之间切换.见hot source dependencies using sbt-sriracha.
使用它可以为project1,project2和project3创建三个单独的sbt构建,它们都位于$HOME / workspace目录中.
ThisBuild / scalaVersion := "2.12.8"
ThisBuild / version := "0.1.1-SNAPSHOT"
lazy val project3Ref = ProjectRef(workspaceDirectory / "project3", "project3")
lazy val project3Lib = "tld.organization" %% "project3" % "0.1.0"
lazy val project1 = (project in file("."))
.enablePlugins(FooProjectPlugin)
.sourceDependency(project3Ref, project3Lib)
.settings(
name := "project1"
)
使用此设置,您可以启动sbt -Dsbt.sourcemode = true,它将获取project3作为子项目.