使一个 sbt 配置依赖于另一个 [英] Make one sbt config depend on another

查看:50
本文介绍了使一个 sbt 配置依赖于另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sbt 文档显示了如何仅在项目之间声明依赖关系的示例.但我肯定有方法可以声明一个配置依赖于另一个配置,就像测试配置使用编译配置中的类路径一样.

The sbt documentation shows example of how to declare dependency only between projects. But I'm positive that there are ways to declare one config be dependent on another, just like the Test configuration uses classpath from the Compile configuration.

如何声明我自己的配置,使其依赖于编译配置生成的类路径?

How can I declare my own configuration so that it would depend on the Compile config generated classpath?

我更仔细地查看了建议的解决方案,然后又出现了一堆问题.所以我重新提出问题

I take a more close look to suggested solution and bunch of question arose again. So I reopen the question

我无法仅从测试和编译配置之间的委托关系推断出 sbt 行为.

I can not deduce sbt behavior solely from the delegate relation between Test and Compile config.

尽管被委托,但测试和编译配置的源目录却完全不同.

The source directories turn out to be completely different for Test and Compile config despite being delegated.

> show test:unmanagedSourceDirectories
[info] List(./src/test/scala, ./src/test/java)
> show test:scalaSource
[info] ./src/test/scala
> show test:javaSource
[info] ./src/test/java
> show compile:unmanagedSourceDirectories
[info] List(./src/main/scala, ./src/main/java)
> show compile:scalaSource
[info] ./src/main/scala
> show compile:javaSource
[info] ./src/main/java

和其他重要的键,例如 unmanagementClasspathfullClasspath 不是 SettingsKey 可以自然地与委托堆叠在一起.它们是成熟的 TaskKey ,用于存储在它们后面生成类路径的复杂过程.

And other significant keys such as unmanagementClasspath and fullClasspath are not SettingsKeys that may be naturally stacked with delegating. They are full-fledged TaskKeys that stores complex procedure for generating class-paths behind them.

因此,问题仍然存在:如何为自定义定义的配置模拟从编译到测试配置的导出类?还有一个密切相关的可选问题:对于上述预定义的配置,这实际上是如何完成的?

So, the question is still actual: How can I mimic exporting classes from Compile to Test config for my custom defined config? And there is closely related optional question: how this is actually done for aforementioned pre-defined configs?

推荐答案

您可以使一个配置扩展另一个.您可以使用 extend 方法来实现.

You can make one configuration extend another. You do that by using extend method.

lazy val MyConfig = config("myConfig") extend(Compile)

lazy val root = project.in(file(".")).
    configs(MyConfig).
    settings(inConfig(MyConfig)(Defaults.compileSettings ++ Defaults.compileInputsSettings): _*)

在这种情况下,对于 MyConfig 配置中未定义的设置,

Extend 将委托给 Compile.

Extend will delegate to Compile in this case, for settings which are undefined in the MyConfig configuration.

您可以通过运行 SBT 并执行例如 show myConfig:managedClasspath 来检查它,输出应该与 show compile:managedClasspath 完全相同.

You can check it by running SBT and executing for example show myConfig:managedClasspath, the output should be exactly the same as for show compile:managedClasspath.

如果您检查新配置的 managedClasspath,您会看到它委托给 compile.

If you inspect your new configuration's managedClasspath you'll see that it delegates to the compile.

[info] Delegates:
[info]  myConfig:managedClasspath
[info]  compile:managedClasspath
[info]  *:managedClasspath
[info]  {.}/myConfig:managedClasspath
[info]  {.}/compile:managedClasspath
[info]  {.}/*:managedClasspath
[info]  */myConfig:managedClasspath
[info]  */compile:managedClasspath
[info]  */*:managedClasspath

正如我上面所说的,SBT 只会委托给未在给定配置中定义的设置.

As I've stated above, SBT will only delegate to the setting if it's not defined in the given configuration.

例如,如果您没有为 myConfig 定义任何特定的编译器选项,则设置将从 compile 中获取.

For example, if you do not define any specific compiler options for myConfig the settings will be taken from compile.

> show compile:scalacOptions
[info] List()
> show myConfig:scalacOptions
[info] List()

更改compile 配置中的设置将对myConfig 产生影响:

Changing setting in compile configuration will have an effect on myConfig:

> set scalacOptions in Compile += "-Xexperimental"
> show compile:scalacOptions
[info] List(-Xexperimental)
> show myConfig:scalacOptions
[info] List(-Xexperimental)

覆盖 myConfig 中的设置将使 SBT 使用该配置中定义的设置,而 Compile 将拥有自己的值:

Overriding the setting in myConfig will make SBT to use the setting defined in that configuration, while Compile will have its own value:

> set scalacOptions in MyConfig  := Seq("-Xcheck-null")
> show compile:scalacOptions
[info] List(-Xexperimental)
> show myConfig:scalacOptions
[info] List(-Xcheck-null)

注意委托是一种方式.对 MyConfig 的更改不会影响 Compile 配置.

Note the delegation is one way. Change to the MyConfig has no influence on the Compile configuration.

您可以查看文档 了解详情.

You can check the documentation for details.

这篇关于使一个 sbt 配置依赖于另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆