如何在多模块构建中设置两个 Scala.js 跨项目之间的依赖关系?

如何在多模块构建中设置两个 Scala.js 跨项目之间的依赖关系?

问题描述:

假设我有两个子项目,lib1lib2,其中 build.sbt 如下所示:

Say I have two sub-projects, lib1 and lib2 with build.sbts that look like this:

lazy val libX = crossProject.in(file(".")).settings(
  ... // a bunch of settings
).jvmSettings(
  ... // a bunch of settings  
).jsSettings(
  ... // a bunch of settings
)

lazy val libXJVM = apiClient.jvm
lazy val libXJS = apiClient.js

我需要在另一个大型多模块项目中使用它们,以便 lib2 依赖于 lib1.

I need to use them in another large multi-module project so that lib2 depends on lib1.

如果我在主 build.sbt 中尝试这个:

If I try this in the main build.sbt:

lazy val lib1 = crossProject.in(file("lib1"))

lazy val lib2 = crossProject.in(file("lib2")).dependsOn(lib1)

lazy val lib1JVM = lib1.jvm
...

然后项目依赖似乎可以工作,但是库内部 build.sbt 文件(例如 libraryDependencies)中的设置被完全忽略并且构建失败.

then the project dependency seems to work but the settings from the libraries internal build.sbt files (e.g. libraryDependencies) are completely ignored and the build fails.

如果我试试这个:

lazy val lib1JS = project.in(file("lib1"))
lazy val lib2JS = project.in(file("lib2")).dependsOn(lib1JS)

dependsOn 似乎被忽略了,lib2 将无法编译,因为它无法从 lib1 导入类型.

dependsOn is seemingly ignored and lib2 won't compile because it can't import types from lib1.

有没有什么方法可以在不复制主构建文件中的 crossProject 设置的情况下完成这项工作?

Is there any way to make this work without duplicating the crossProject settings in the main build file?

如果我理解正确,您正在尝试dependsOn(交叉)在完全独立的构建中定义的项目?

If I understand correctly, you are trying to dependsOn (cross) projects that are defined in a completely separate build?

如果是这种情况,您需要的是 ProjectRef.没有直接的CrossProjectRef,所以JVM和JS部分需要手动使用ProjectRef.它看起来像:

If that is the case, what you need are ProjectRef. There is no direct of CrossProjectRef, so you need to manually use ProjectRefs for the JVM and JS parts. It would look something like:

lazy val lib1JVM = ProjectRef(file("path/to/other/build", "lib1JVM")
lazy val lib1JS = ProjectRef(file("path/to/other/build", "lib1JS")

然后你可以依赖

lazy val myProject = crossProject
  .jvmConfigure(_.dependsOn(lib1JVM))
  .jsConfigure(_.dependsOn(lib1JS))

请注意,我不太明白您要使用问题的 lazy val lib2 = crossProject.in(file("lib2")).dependsOn(lib1) 部分做什么.如果 lib2 应该依赖于 lib1,那么这个依赖应该已经在 lib2 的构建中声明了.在重用 lib1lib2 的大型多模块构建中,向来自另一个构建的 lib2 添加依赖项为时已晚.

Note that I do not quite understand what you are trying to do with the lazy val lib2 = crossProject.in(file("lib2")).dependsOn(lib1) part of your question. If lib2 should depend on lib1, that dependency should already have been declared in the build of lib2. In your larger multi-module build that reuses lib1 and lib2, it is too late to add dependencies to lib2 which comes from another build.