3

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?

For example, in various points in the code I refer to resourceManaged in Compile which relates to src/main/resourcesbut when test is run, I would like it to be resourceManaged in Test when relates to src/test/resources.

How do I abstract away the scope?

melps
  • 1,247
  • 8
  • 13

1 Answers1

2

This is a topic discussed in Plugins Best Practices, specifically in the Configuration advices section.

Provide raw settings and configured settings

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
)

As you can see in the above it's accessing sources key, but it's not specified which configuration's source.

inConfig

override lazy val projectSettings = inConfig(Compile)(baseObfuscateSettings)

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)
Eugene Yokota
  • 94,654
  • 45
  • 215
  • 319