简体   繁体   English

如果我有一个SBT多项目构建,如何使A的测试依赖项不泄漏到B的测试中

[英]If I have an SBT multi-project build, how can I make A's test dependencies not leak in to B's tests

So, I have an SBT project with modules A and B. 因此,我有一个包含模块A和B的SBT项目。

  • A depends on B. A取决于B。
  • A and B's normal project packages are compatible. A和B的常规项目包是兼容的。
  • A has a package for testing that is incompatible with a package that B uses for tests. A的测试包与B的测试包不兼容。
  • A does not depend on B's test packages A不依赖于B的测试包
  • A's tests fail, because of the test package incompatibility A的测试失败,因为测试包不兼容

In my eyes, this failure is invalid, because A's tests do not depend on B's tests. 在我看来,这种失败是无效的,因为A的测试不依赖于B的测试。

I'm using 我正在使用

A.dependsOn(B % "compile->compile;test->compile")

meaning 含义

  • A's compile depends on B's compile A的编译取决于B的编译
  • A's test depends on B's compile. A的测试取决于B的编译。

Am I doing something wrong? 难道我做错了什么?

Here's the actual build file . 这是实际的构建文件 Relevant project is doobieSupport23 depending on core 相关项目是doobieSupport23取决于core

Here's the build error . 这是构建错误 You can see that it's pulling in scalaz 7.2 in the build, but it should only be pulling scalaz 7.1.6 您可以看到它在构建中加入了scalaz 7.2,但是应该只在拉入scalaz 7.1.6。

The problem is definitely not in projects depending on each other, the configuration "compile->compile;test->compile" , as it seems to me, is perfectly valid for what you need to achieve. 问题绝对不是在彼此依赖的项目中,在我看来,配置"compile->compile;test->compile"对于您需要实现的目标是完全有效的。

What's causing the problem is that your libraryDependencies are shared due to A being dependent on B. Fortunately, sbt allows to manage dependencies quite precisely. 造成此问题的原因是,由于A依赖于B而共享了libraryDependencies 。幸运的是,sbt可以非常精确地管理依赖关系。

The easiest suggestion would be to apply a newer version of the conflicting dependency to a scope as narrow as possible - meaning, if scalaz 7.2.0 is only needed in core's tests, so be it! 最简单的建议是将冲突依赖的更新版本应用到尽可能小的范围内-这意味着,如果仅在核心测试中需要scalaz 7.2.0,那就这样吧!

lazy val core = project(...)
  .settings(libraryDependencies in Test += "org.scalaz" %% "scalaz" % "7.2.0")
lazy val doobieSupport23 = project(...).dependsOn(core)
  .settings(libraryDependencies in Test += "org.scalaz" %% "scalaz" % "7.1.6")

If you can't make it work because the newer version is actually used in other core's dependencies, the following trick may do it: 如果由于较新版本实际上已在其他内核的依赖项中使用而使它无法工作,则可以使用以下技巧:

val scalazOld = "org.scalaz" %% "scalaz" % "7.1.6"
val scalazNew = "org.scalaz" %% "scalaz" % "7.2.0"

lazy val core = project(...)
  .settings(libraryDependencies += "org.scalaz" %% "scalaz" % "7.2.0")

lazy val doobieSupport23 = project(...).dependsOn(core)
  .settings(
    libraryDependencies in Test += "org.scalaz" %% "scalaz" % "7.1.6" force()
  )

Note the force() combinator. 注意force()组合器。 I'm not sure this way it won't fail in runtime due to incompatible classes in classpath. 我不确定以这种方式它不会在运行时因类路径中的类不兼容而失败。

What I had specified in the issue works exactly as expected. 我在问题中指定的内容完全符合预期。

The problem ended up being that my build file had an extraneous dependency that I had just glossed over. 问题最终是我的构建文件有一个刚刚掩盖的无关项。 That dependency was causing doobie 3.0 being pulled in where I only expected doobie 2.3. 这种依赖性导致doobie 3.0被拉到我只期望doobie 2.3的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM