如何使 sbt 任务在多个范围内运行 [英] How to make an sbt task run on under multiple scopes
问题描述
我编写了一个 sbt 插件,可以生成一些资源和资源.它被硬编码为在 Compile 范围内工作.
I have written an sbt plugin that generates some sources and resources. It is hard coded to work in the Compile scope.
我怎样才能让它也能在测试范围内工作,这样我就可以在运行测试时使用插件,它会查找并输出到正确的文件夹?
How can I make it work in the Test scope too, so I can use the plugin when running tests and it will look in and output to the correct folder?
例如,在代码中的各个点我指的是resourceManaged in Compile
,它与src/main/resources
有关,但是当test
运行时,当与 src/test/resources
相关时,我希望它是 resourceManaged in Test
.
For example, in various points in the code I refer to resourceManaged in Compile
which relates to src/main/resources
but when test
is run, I would like it to be resourceManaged in Test
when relates to src/test/resources
.
如何抽象出作用域?
推荐答案
This is a topic discussed in Plugins Best Practices, specifically in the Configuration advices section.
如果您的插件是 ObfuscatePlugin
,请提供不在任何配置范围内的 baseObfuscateSettings
:
If your plugin is ObfuscatePlugin
, provide baseObfuscateSettings
that's not scoped in any configuration:
lazy val baseObfuscateSettings: Seq[Def.Setting[_]] = Seq(
obfuscate := Obfuscate((sources in obfuscate).value),
sources in obfuscate := sources.value
)
正如您在上面看到的,它正在访问 sources
键,但没有指定哪个配置的源.
As you can see in the above it's accessing sources
key, but it's not specified which configuration's source.
override lazy val projectSettings = inConfig(Compile)(baseObfuscateSettings)
inConfig
将传入的设置序列限定为特定配置.如果您想开箱即用地支持 Compile
和 Test
,您可以说:
inConfig
scopes the passed in sequence of settings into a particular configuration. If you want to support both Compile
and Test
out of the box, you can say:
override lazy val projectSettings =
inConfig(Compile)(baseObfuscateSettings) ++
inConfig(Test)(baseObfuscateSettings)
这篇关于如何使 sbt 任务在多个范围内运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!