monzonj
monzonj

Reputation: 3709

Attach sbt plugin to compile lifecycle

Let's suppose the following auto plugin:

import sbt._
import Keys._

object TestPlugin extends AutoPlugin {

  object autoImport {
    val myTask = TaskKey[Unit]("myTask", "It's just a task")
  }
  import autoImport._

  override def trigger = allRequirements

  def doSomething = (streams) map { (s) => 
    s.log.info(s"Something...")    
  }

  override lazy val buildSettings = Seq(
    myTask <<= doSomething
  )

}

I would like this plugin to run before compilation. When I type in the sbt console compile, this plugin runs (before the actual compilation happens)

I could use the following in the sbt configuration of the project where I will use this plugin:

(compile in Compile) <<= (compile in Compile)  dependsOn myTask

However, I'd rather have the plugin attach itself to the compilation process without having to explicity declare it in each project where I will use my plugin.

If I try to copy the line before in my plugin buildSettings I get the error:

[error]   {.}/compile:compile from {.}/compile:compile ((TestPlugin) TestPlugin.scala:32)
[error]      Did you mean compile:compile ?

Any idea how I could get it working?

Regards,

Upvotes: 2

Views: 383

Answers (1)

Gerolf Seitz
Gerolf Seitz

Reputation: 121

You need to override projectSettings and not buildSettings. This way, the settings are applied in the scope of the individual project, which in turn should have the compile:compile setting already defined and adding the line

(compile in Compile) <<= (compile in Compile)  dependsOn myTask

to the projectSettings should work as expected.

Upvotes: 1

Related Questions