0

I saw SBT Plugin: How to add compiler plugin as a dependency that is not propagated downstream? but this does not answer my question.

Here is the structure:

other plugins
    |-----> added to my own plugin
                |-----> Consumer project

the consumer project needs to add addCompilerPlugin and enablePlugins in their own build.sbt because of the other plugins.

I have the other plugins added in the build.sbt of my own plugin.

Where do I put addCompilerPlugin and enablePlugins in my plugin so the consumer project do not have to do it themselves?

Thanks

Wonay
  • 1,160
  • 13
  • 35

1 Answers1

1

addCompilerPlugin is just a shortcut for modifying a particular setting key, while enablePlugins is a method to modify the project configuration itself. Therefore, these things are on different levels, and so are handled differently for your purpose.

To ensure that enabling your plugin would also enable other plugins, you need to modify the requires declaration in your plugin:

object YourPlugin extends AutoPlugin {
  override def requires: Plugins = FirstDependencyPlugin & SecondDependencyPlugin
}

Now, when your plugin is added to a project:

lazy val someProject = project.enablePlugins(YourPlugin)

then the FirstDependencyPlugin and SecondDependencyPlugin plugins will also be enabled.

To enable a compiler plugin, you just need to ensure that your plugin provides the setting definition returned by addCompilerPlugin:

object YourPlugin extends AutoPlugin {
  override def projectSettings: Seq[Def.Setting[_]] = super.projectSettings ++ Vector(
    addCompilerPlugin("com.example" % "some-plugin" % "1.2.3")
  )
}

Now, when your plugin is added to a project, the Def.Setting[_] provided by it will be automatically applied to this project, and it will use the specified compiler plugin to build your code.

Vladimir Matveev
  • 120,085
  • 34
  • 287
  • 296
  • Thanks ! What if I do not want to have to enable my own project either ? – Wonay Dec 18 '18 at 19:29
  • Not sure I understand your question. What do you mean, "enable my own project"? – Vladimir Matveev Dec 18 '18 at 21:21
  • I don't want to have to call `enablePlugins` even in the consumer project – Wonay Dec 18 '18 at 22:06
  • 1
    I see. There is this thing called [triggered plugins](https://www.scala-sbt.org/1.x/docs/Plugins.html#Root+plugins+and+triggered+plugins), which is basically a way to enable a plugin automatically *as long as* another plugin is enabled. You could try to make your plugin dependent to some always-enabled plugin and make it triggered; but this way your plugin won't be able to pull other dependencies automatically. I don't think there is a way to do both. – Vladimir Matveev Dec 18 '18 at 22:43
  • ah :( Thats too bad. I decided to keep my plugin always on with `trigger: allRequirements` + `requires` not changed. And consumer will have to add the other plugin themselves. – Wonay Dec 19 '18 at 02:48