简体   繁体   English

如何测试sbt解析器

[英]How to test sbt resolvers

On my local machine I have an ivy cache that has been filled by working on multiple projects. 在我的本地机器上,我有一个常春藤缓存,已经通过处理多个项目来填充。

A library X is loaded using resolver Y in project A . 使用项目A解析程序Y加载库X This same library X is used in project B , no problems resolving this library because it's in my local cache. 项目B使用了相同的库X ,解析此库没有问题,因为它位于我的本地缓存中。

When one of my colleagues loads project B he get's the error that library X could not be resolved. 当我的一个同事加载项目B他得到了库X无法解决的错误。 The problem: resolver Y is missing. 问题:解析器Y丢失了。

How can I test if my sbt project has a complete set of resolvers to resolve all dependencies without removing my ivy cache? 如何在不删除常春藤缓存的情况下测试我的sbt项目是否有一套完整的解析器来解析所有依赖项?

Another even more elegant solution would be to investigate the SBT sources if it is easily possible to setup this behavior as a separate task. 另一个更优雅的解决方案是调查SBT源,如果很容易将此行为设置为单独的任务。 The necessary steps could be the same as in my other answer . 必要的步骤可能与我的其他答案相同。

  1. Extract parameter sbt.ivy.home from update task and provide a parameterized overload for it (if this is possible) update任务中提取参数sbt.ivy.home并为其提供参数化重载(如果可能的话)
  2. Define new task testDependencies see documentation 定义新任务testDependencies 请参阅文档
  3. Create tempDirectory 创建tempDirectory
  4. Call update(tempDirectory) 调用更新(tempDirectory)
  5. Gather results 收集结果
  6. Remove tempDirectory 删除tempDirectory
  7. Promote results 促进结果
  8. Provide a pull request ;) or an sbt plugin 提供拉取请求;)或sbt插件

This command allows you to find if you are missing any updates for the current project. 此命令允许您查找是否缺少当前项目的任何更新。 Note that this will not discover any missing resolvers for your plugins. 请注意,这不会发现插件的任何丢失的解析器。

commands += Command.command("testUpdate") { state =>
  val base = baseDirectory.value
  val newState = Project
    .extract(state)
    .append(Seq(ivyPaths := new IvyPaths(base, Some(base / "tmp-cache"))), state)
  val (s, _) = Project
    .extract(newState)
    .runTask(update, newState)
  s
}

It could be expanded by removing the directory afterwards. 之后可以通过删除目录来扩展它。

I found an admittedly simple but also a bit hacky and nonetheless working solution. 我发现一个公认的简单但也有点hacky,但仍然工作的解决方案。 As described here you can setup the ivy home directory. 如此处所述,您可以设置常春藤主目录。 After setting this up it will provoke the sbt instances on your system to update all dependencies, due to a fresh cache directory. 设置完成后,由于新的缓存目录,它会激活系统上的sbt实例以更新所有依赖项。 When all dependencies could be resolved you could inspect the std out for some string indicating success. 当所有依赖关系都可以解决时,您可以检查std输出一些指示成功的字符串。 Like Done updating. 就像Done updating.一样Done updating. and delete the temporal folder afterwards. 然后删除临时文件夹。 Caution, a fresh resolve from scratch might take a while! 小心,从头开始新的决心可能需要一段时间! ~5min 100mBit/s and using an SSD drive ~5min 100mBit / s并使用SSD驱动器

Instead of defining the sbt.ivy.home variable system wide and in an somewhat unportable fashion i would recommend to use the less system invasive option of defining the sbt.ivy.home variable within the environment variable SBT_OPTS on your local command/terminal session. 我建议使用较少的系统侵入性选项来定义本地命令/终端会话中的环境变量SBT_OPTSsbt.ivy.home变量,而不是在系统范围内定义sbt.ivy.home变量。 On Windows this looks as follows: 在Windows上,这看起来如下:

C:\Users\isi\Projects\learning\sbt-test-dependencies>SET SBT_OPTS=-Dsbt.ivy.home="C:\path\to\your\temp\directory"

C:\Users\isi\Projects\learning\sbt-test-dependencies>sbt
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
[info] Loading project definition from C:\Users\isi\Projects\learning\sbt-test-dependencies\project
[info] Updating {file:/C:/Users/isi/Projects/learning/sbt-test-dependencies/project/}sbt-test-dependencies-build...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] downloading https://repo.scala-sbt.org/scalasbt/sbt-plugin-releases/com.typesafe.sbteclipse/sbteclipse-plugin/scala_2.10/sbt_0.13/4.0.0/jars/sbteclipse-plugin.jar ...
[info]  [SUCCESSFUL ] com.typesafe.sbteclipse#sbteclipse-plugin;4.0.0!sbteclipse-plugin.jar (4783ms)
...
[info] downloading https://jcenter.bintray.com/org/scala-lang/jline/2.10.5/jline-2.10.5.jar ...
[info]  [SUCCESSFUL ] org.scala-lang#jline;2.10.5!jline.jar (419ms)
[info] downloading https://jcenter.bintray.com/org/fusesource/jansi/jansi/1.4/jansi-1.4.jar ...
[info]  [SUCCESSFUL ] org.fusesource.jansi#jansi;1.4!jansi.jar (325ms)
[info] Done updating.
[info] Set current project to sbt-test-dependencies (in build file:/C:/Users/isi/Projects/learning/sbt-test-dependencies/)/Users/isi/Projects/learning/sbt-test-dependencies/)

The output can be piped to lets say grep and the exit code can be used for further processing. 可以通过管道传输输出,例如grep ,退出代码可以用于进一步处理。 Notice, that the console output above was produced using the interactive sbt command, a similar output is generated using the non interactive sbt update command. 请注意,上面的控制台输出是使用interactive sbt命令生成的,使用非交互式sbt update命令生成类似的输出。

There is now an SBT plugin available, called SBT dirty money , to add additional functionality for tackle this problem. 现在有一个名为SBT脏钱的SBT插件,可以添加额外的功能来解决这个问题。 Although you have to execute an extra command. 虽然您必须执行额外的命令。

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

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