如何在父 build.sbt 文件中引用项目定义?

How to reference a project definition in a parent build.sbt file?

我正在使用 scala-forklift 库,想通过修改库和示例项目中的代码来测试一个想法。

项目结构如下:

lazy val slickMigrationProject =
  Project("scala-forklift-slick", file(...))
    .dependsOn(...)
    .settings(...)
    ...

我的目标是用对父目录中代码的引用替换 scala-forklift-slick Maven 包引用,也许通过 .dependsOn(slickMigrationProject)?

尝试次数:

According to the documentation:

Any .sbt files in foo, say foo/build.sbt, will be merged with the build definition for the entire build, but scoped to the hello-foo project.

但这似乎不适用于 lazy val 值,因为我无法直接从父项目访问 slickMigrationProject

我已经成功地将 /example/build.sbt 的全部内容复制到 /build.sbt 并做了一些调整,但我想知道是否有更好的“单行”解决方案。 这是我合并后的 /build.sbt 文件的样子:

val repoKind = SettingKey[String]("repo-kind",
  "Maven repository kind (\"snapshots\" or \"releases\")")

lazy val slickVersion = "3.3.3"

lazy val scala212 = "2.12.11"
lazy val scala213 = "2.13.1"
lazy val supportedScalaVersions = List(scala212, scala213)

lazy val coreDependencies = libraryDependencies ++= List(
  "org.scala-lang" % "scala-compiler" % scalaVersion.value,
  "com.typesafe" % "config" % "1.3.2",
  "org.eclipse.jgit" % "org.eclipse.jgit" % "4.0.1.201506240215-r"
)

lazy val slickDependencies = List(
  "com.typesafe.slick" %% "slick" % slickVersion,
  "com.typesafe.slick" %% "slick-codegen" % slickVersion,
  "io.github.nafg" %% "slick-migration-api" % "0.7.0",
  "org.scala-lang.modules" %% "scala-collection-compat" % "2.0.0"
)

lazy val slickDependenciesWithTests = slickDependencies ++ List(
  "org.scalatest" %% "scalatest" % "3.1.0",
  "com.lihaoyi" %% "ammonite-ops" % "2.0.4",
  "commons-io" % "commons-io" % "2.6",
  "com.typesafe.slick" %% "slick-hikaricp" % slickVersion,
  "com.h2database" % "h2" % "1.4.200",
  "org.xerial" % "sqlite-jdbc" % "3.8.11.2",// 3.30.1 crashes SQLiteCommandTests
  "mysql" % "mysql-connector-java" % "5.1.38",
  "org.postgresql" % "postgresql" % "42.2.9",
  "org.hsqldb" % "hsqldb" % "2.5.0",
  "org.apache.derby" % "derby" % "10.14.2.0",
  "ch.qos.logback" % "logback-classic" % "1.2.3"
).map(_ % "test")

lazy val commonSettings = Seq(
  organization := "com.liyaos",
  licenses := Seq("Apache 2.0" ->
    url("https://github.com/lastland/scala-forklift/blob/master/LICENSE")),
  homepage := Some(url("https://github.com/lastland/scala-forklift")),
  scalaVersion := scala213,
  scalacOptions += "-deprecation",
  scalacOptions += "-feature",
  resolvers += Resolver.jcenterRepo,
  publishMavenStyle := true,
  publishArtifact in Test := false,
  repoKind := { if (version.value.trim.endsWith("SNAPSHOT")) "snapshots"
                else "releases" },
  publishTo := { repoKind.value match {
    case "snapshots" => Some("snapshots" at
        "https://oss.sonatype.org/content/repositories/snapshots")
    case "releases" =>  Some("releases"  at
        "https://oss.sonatype.org/service/local/staging/deploy/maven2")
  }},
  credentials += Credentials(Path.userHome / ".ivy2" / ".credentials"),
  pomExtra := (
    <scm>
      <url>git@github.com:lastland/scala-forklift.git</url>
      <connection>scm:git:git@github.com:lastland/scala-forklift.git</connection>
      </scm>
      <developers>
      <developer>
      <id>lastland</id>
      <name>Yao Li</name>
      </developer>
      </developers>))

// Derby is running is secured mode since version 10.12.1.1, so security manager must be disabled for tests  
// 
// https://issues.apache.org/jira/browse/DERBY-6648
Test / testOptions += Tests.Setup(() => System.setSecurityManager(null))

lazy val root = Project(
  "scala-forklift", file(".")).settings(
  crossScalaVersions := Nil,
  publishArtifact := false).aggregate(
  coreProject, slickMigrationProject, plainMigrationProject, gitToolProject, example)

lazy val coreProject = Project(
  "scala-forklift-core", file("core")).settings(
  commonSettings:_*).settings {Seq(
    crossScalaVersions := supportedScalaVersions,
    coreDependencies
  )}

lazy val slickMigrationProject = Project(
  "scala-forklift-slick", file("migrations/slick")).dependsOn(
  coreProject).settings(commonSettings:_*).settings { Seq(
    crossScalaVersions := supportedScalaVersions,
    libraryDependencies ++= slickDependenciesWithTests
  )} 

lazy val plainMigrationProject = Project(
  "scala-forklift-plain", file("migrations/plain")).dependsOn(
  coreProject).settings(commonSettings:_*).settings(crossScalaVersions := supportedScalaVersions)

lazy val gitToolProject = Project(
  "scala-forklift-git-tools", file("tools/git")).dependsOn(
  coreProject).settings(commonSettings:_*).settings(crossScalaVersions := supportedScalaVersions)

///////////////////////////////////////////////

name := "forklift-slick-example"

addCommandAlias("mgm", "example/migration_manager/run")

addCommandAlias("mg", "example/migrations/run")

lazy val exampleCommonSettings = Seq(
  organization := "com.liyaos",
  version := "2.0",
  scalaVersion := "2.13.1",
  scalacOptions += "-deprecation",
  scalacOptions += "-feature",
  resolvers += Resolver.sonatypeRepo("snapshots"),
  resolvers += Resolver.jcenterRepo,

)

lazy val loggingDependencies = List(
  "org.slf4j" % "slf4j-nop" % "1.6.4" // <- disables logging
)

lazy val exampleSlickDependencies = List(
  "com.typesafe.slick" %% "slick" % slickVersion
)

lazy val dbDependencies = List(
  "com.typesafe.slick" %% "slick-hikaricp" % slickVersion
  ,"com.h2database" % "h2" % "1.4.200"
)

lazy val forkliftDependencies = List(
//  "com.liyaos" %% "scala-forklift-slick" % forkliftVersion
)

lazy val appDependencies = dbDependencies ++ loggingDependencies

lazy val migrationsDependencies =
  dbDependencies ++ forkliftDependencies ++ loggingDependencies

lazy val migrationManagerDependencies = dbDependencies ++ forkliftDependencies

lazy val example = Project("example", file("example")).aggregate(
  app, migrations, migrationManager, generatedCode, tools).settings(
  exampleCommonSettings:_*)

lazy val app = Project("app", file("example/app"))
  .dependsOn(generatedCode)
  .settings(exampleCommonSettings:_*)
  .settings {libraryDependencies ++= appDependencies}

lazy val migrationManager = Project("migration_manager", file("example/migration_manager"))
  .dependsOn(slickMigrationProject)
  .settings(exampleCommonSettings:_*)
  .settings {libraryDependencies ++= migrationManagerDependencies}

lazy val migrations = Project("migrations", file("example/migrations"))
  .dependsOn(generatedCode, migrationManager, slickMigrationProject)
  .settings(exampleCommonSettings:_*)
  .settings {libraryDependencies ++= migrationsDependencies}

lazy val tools = Project("git-tools", file("example/tools/git"))
  .dependsOn(slickMigrationProject, gitToolProject)
  .settings(commonSettings:_*)
  .settings {
    libraryDependencies ++= forkliftDependencies ++ List(
//    "com.liyaos" %% "scala-forklift-git-tools" % forkliftVersion,
      "com.typesafe" % "config" % "1.3.0",
      "org.eclipse.jgit" % "org.eclipse.jgit" % "4.0.1.201506240215-r"
    )
  }

lazy val generatedCode = Project("generate_code", file("example/generated_code"))
  .settings(exampleCommonSettings:_*)
  .settings {libraryDependencies ++= exampleSlickDependencies}

问题:

有没有一种简单的方法可以将 /example/build.sbt 中的 scala-forklift-slick Maven 包引用替换为 link 到父目录的现有 scala-forklift-slick 项目定义 build.sbt 所以我可以用 .dependsOn(...) 代替?

或者做一些类似构建 scala-forklift-slick 的事情并使用本地磁盘包解析器更好?

Luis Miguel Mejía Suárez 的评论非常有效并且是更简单的方法。

在这个项目的背景下,我所要做的就是:

  1. -SNAPSHOT 附加到 /version.sbt 中的版本(通常不需要,但对于这个项目我必须这样做)
  2. 运行 sbt publishLocal 在父项目中。

在此之后,示例项目(已经针对 -SNAPSHOT 版本)能够获取本地构建的包。