如何依赖一个共同的crossProject

How to depend on a common crossProject

我正在定义多个 JVM/JS 交叉项目。每个都包含一些通用的 JVM/JS scala 代码,我想将这些代码提取到每个项目都可以依赖的通用项目中。有人可以推荐我为一般项目和相关项目定义 build.scala 文件的最佳方法吗?

您可以创建Multi-project builds

假设您有这样的项目结构;

root        
    project/Build.scala
    project1
        src/
        project1.sbt
    project2
        src/
        project2.sbt
    projectN
        src/
        projectN.sbt

您可以轻松地在 Build.scala

中定义依赖项
lazy val root = Project(id = "Main-Project",
    base = file(".")) aggregate(project1, project2,..)

lazy val project2 = Project(id = "project2",
    base = file("project1")).dependsOn(project1)

...

CrossProject支持您习惯的正常dependsOn操作。所以你可以:

// call to settings needed so for an implicit conversion to kick in
lazy val common = crossProject.settings() 
lazy val p1 = crossProject.dependsOn(common)
lazy val p2 = crossProject.dependsOn(common)

lazy val commonJVM = common.jvm
lazy val commonJS = common.js
lazy val p1JVM = p1.jvm
lazy val p1JS = p1.js
lazy val p2JVM = p2.jvm
lazy val p2JS = p2.js

GitHub 上有一个 full example

我最终得出了以下解决方案。

lazy val common = crossProject.in(file(".")).
    settings(
    ).
    jvmSettings(
    ).
    jsSettings(
    )

lazy val commonJVM = common.jvm
lazy val commonJS = common.js

...

lazy val p1 = crossProject.in(file(".")).
    settings(
    ).
    jvmSettings(
    ).
    jsSettings(
    ).
    jvmConfigure(_.dependsOn(ProjectRef(uri("../common"), "commonJVM"))).
    jsConfigure(_.dependsOn(ProjectRef(uri("../common"), "commonJS")))

lazy val p1JVM = p1.jvm.
    settings(...

lazy val p1JS = p1.js.
    settings(...