Search code examples
scalasbtsbt-plugin

SBT Plugin: Propagate Compiler Plugin and Enable Plugin downstream


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


Solution

  • 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.