4

Is it possible to build an sbt plugin in a multi-project setup and use that plugin in some other sub-project of the same multi-project?

For example:

root/
+  mySbtPlugin/
+  myProject/
   +  project/
      +  plugins.sbt    // Uses `mySbtPlugin`
Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
sinharaj
  • 1,093
  • 2
  • 11
  • 16

1 Answers1

6

It is not possible since the only way to define plugins for a single or multi-module project is via project (meta)build. I was tricked again to have a solution for you when I set up the sandbox environment with the layout you described.

sbt allows project (meta)project to be in the root directory only. No other project directories are processed and become part of the build definition.

That's why your best (and only) bet is to have the multi-module build for myProject and mySbtPlugin to ease your development, and enable the plugin only for these project(s) you really want to (be careful with auto-plugins, though).

project/plugins.sbt

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

lazy val sbtNonamePlugin = ProjectRef(file("../sbt-noname"), "sbt-noname")

addSbtPlugin("pl.japila" % "sbt-noname" % "1.0")

build.sbt

lazy val `my-project`, `sbt-noname` = project

sbt-noname/build.sbt

sbtPlugin := true

name := "sbt-noname"

organization := "pl.japila"

version := "1.0"

sbt-noname/src/main/scala/sbtnoname/Plugin.scala

package sbtnoname

import sbt._
import plugins._

object Plugin extends AutoPlugin {

  override def trigger = allRequirements

  override val projectSettings: Seq[Setting[_]] = inConfig(Test)(baseNonameSettings)

  lazy val sayHello = taskKey[Unit]("Say hello")

  lazy val baseNonameSettings: Seq[sbt.Def.Setting[_]] = Seq(
    sayHello := {
      println("I'm the plugin to say hello")
    }
  )
}

With the files above, run sbt.

> about
[info] This is sbt 0.13.6-SNAPSHOT
[info] The current project is {file:/Users/jacek/sandbox/multi-plugin/}my-project 0.1-SNAPSHOT
[info] The current project is built against Scala 2.10.4
[info] Available Plugins: sbt.plugins.IvyPlugin, sbt.plugins.JvmPlugin, sbt.plugins.CorePlugin, sbt.plugins.JUnitXmlReportPlugin, sbtnoname.Plugin, com.timushev.sbt.updates.UpdatesPlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.10.4
> projects
[info] In file:/Users/jacek/sandbox/multi-plugin/
[info]   * multi-plugin
[info]     my-project
[info]     sbt-noname
> plugins
In file:/Users/jacek/sandbox/multi-plugin/
    sbt.plugins.IvyPlugin: enabled in multi-plugin, sbt-noname, my-project
    sbt.plugins.JvmPlugin: enabled in multi-plugin, sbt-noname, my-project
    sbt.plugins.CorePlugin: enabled in multi-plugin, sbt-noname, my-project
    sbt.plugins.JUnitXmlReportPlugin: enabled in multi-plugin, sbt-noname, my-project
    sbtnoname.Plugin: enabled in multi-plugin, sbt-noname, my-project
> my-project/test:sayHello
I'm the plugin to say hello
[success] Total time: 0 s, completed Jun 15, 2014 3:49:50 PM
Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420