2

I'm making first steps with an SBT plugin.

object WoahPlugin extends Plugin {
  override lazy val settings = commands += thingCommand

  lazy val thingCommand = Command.command("thing") {state: State =>
    println("this is the thing~!")
    state
  }
}

After defining a command, I want to try it out. Do I need to build the plugin and define a new project that uses it in order to try it out? Or can I open an SBT session in the plugin's project and import it directly?

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
Synesso
  • 37,610
  • 35
  • 136
  • 207

1 Answers1

2

tl;dr You can't have a plugin project and be able to test it out without another project that uses it.

A plugin enhances a build definition of a project so you set up a plugin that expands the project with capabilities you want - additional settings and tasks. See Plugins in the official documentation of sbt.

You set up plugins in meta-project under project directory, by convention usually in project/plugins.sbt (but any project/*.sbt file would do).

If you need to test a plugin you've got two notable choices that all boil down to having a separate project for the plugin and another to use it - the choice is about the place where the test project is as compared to the plugin project under test.

Plugin project in project metaproject

You can have the sources of the plugin under project/src/main/scala so they belong to their own project, but because of the directory they live in project, they automatically become part of the metaproject for another project one level up.

With the plugin's sources inside the meta-project project you can define a plugin dependency in project/plugins.sbt and have the plugin installed (given the recent changes to sbt since 0.13.5 it doesn't necessarily mean the plugin's enabled).

Plugin project anywhere

The plugin project is a sbt project and as such can be referenced from another sbt project using ProjectRef. If it's a git:// or file:// project, you declare the dependency on the plugin project using dependsOn and the project reference with ProjectRef.

See Can multi-projects from GIT be used as SBT dependencies? and How can sbt pull dependency artifacts from git?.

Community
  • 1
  • 1
Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
  • 1
    Thanks for the comprehensive answer. Your edit of the title is odd though. I was asking if it is possible, and the answer is no. Now it looks as though I've made an assumption that it is possible. – Synesso Sep 07 '14 at 02:19
  • 1
    Thanks a lot for accepting the answer. As to the title's change I think it doesn't make that much difference however I'm later going to correct it unless you beat me to it. – Jacek Laskowski Sep 07 '14 at 12:53