Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SBT Subprojects do not recognize plugin commands

Tags:

sbt

sbt-plugin

I'm having an issue with getting SBT Subprojects to recognize commands provided by plugins. I have the following plugin source:

object DemoPlugin extends AutoPlugin {
  override lazy val projectSettings = Seq(commands += demoCommand)

  lazy val demoCommand =
    Command.command("demo") { (state: State) =>
      println("Demo Plugin!")
      state
    }
}

Which is used by a project configured as follows:

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

lazy val sub = (project in file("sub")).
  enablePlugins(DemoPlugin).
  settings(
    //...
  )

The plugin is, of course, listed in project/plugins.sbt. However, when I open up sbt in the project, I see the following:

> sub/commands
[info] List(sbt.SimpleCommand@413d2cd1)
> sub/demo
[error] Expected ':' (if selecting a configuration)
[error] Not a valid key: demo (similar: doc)
[error] sub/demo

Even stranger, using consoleProject, I can see that the command in the project is the one defined by DemoPlugin!

scala> (commands in sub).eval.map { c => c.getClass.getMethod("name").invoke(c) }
res0: Seq[Object] = List(demo)

I'm looking to be able to type sub/demo, and have it perform the demo command. Any help would be much appreciated!

like image 897
Robert Grider Avatar asked Feb 20 '15 19:02

Robert Grider


1 Answers

Commands aren't per-project. They only work for the top-level project.

It's also recommended to try and use tasks, or if needed input tasks where you might want to use a command.

If you really need a command, there's a way to have a sort of "holder" task, see the answer to Can you access a SBT SettingKey inside a Command?

like image 142
Dale Wijnand Avatar answered Sep 20 '22 08:09

Dale Wijnand