lastland
lastland

Reputation: 910

Add plugins under a same project in sbt

I'm trying to build a Scala project aggregated by multiple projects, one of which is an sbt plugin. I would like to use this plugin in another subproject under the same parent project, but I don't quite understand how to do this.

My "build.sbt" in the project root is like this:

lazy val plugin = project
  .in(file("sbt-Something"))
  .dependsOn(lib)
  .settings(common: _*)
  .settings(name := "My plugin",
    sbtPlugin := true)

lazy val examples = project
  .in(file("examples"))
  .dependsOn(lib, plugin)
  .settings(common: _*)
  .settings(name := "Examples")

How to add the plugin as a plugin to project examples?

Upvotes: 3

Views: 1377

Answers (1)

lpiepiora
lpiepiora

Reputation: 13749

I don't think you can have a plugin at the same "level" that project which is using it.

If you think of it, the plugin must be available before the compilation of the project that is using it. This is because it may, for example modify the build settings, which would influence the way the project is built.

If you want to keep your plugin within your project you can do so by declaring a project in the project/project directory.

$YOUR_PROJECT_ROOT/project/build.sbt

lazy val plugin = project
  .in(file("sbt-plugin"))
  .dependsOn(lib)
  .settings(name := "My plugin", sbtPlugin := true)

lazy val lib = project.in(file("lib"))

lazy val root = project.in(file(".")).dependsOn(plugin)

Then you can put your code to sbt-plugin directory, and your shared library code to the lib folder.

In your normal build you can reference the shared library and the plugin.

$YOUR_PROJECT_ROOT/build.sbt

val lib = ProjectRef(file("project/lib"), "lib")

val root = project.in(file(".")).dependsOn(lib).enablePlugins(MyPlugin)

Please note that maybe it would be better to keep the shared library as a separate project, because I think this setup may be a bit tricky. For example if you change something in the shared library the main project should recompile and should use new code. The plugin however will only use new code after issuing the reload command on the project.

If you want to share settings between the projects you can check answers to How to share version values between project/plugins.sbt and project/Build.scala?

Upvotes: 5

Related Questions