0

I have this hello world plugin:

object HelloPlugin extends AutoPlugin {

  object autoImport {
    val sayHello: TaskKey[Unit] = TaskKey("saying hello")
  }

  import autoImport._
  override def projectSettings = Seq(

    sayHello := {
      println("------------------------------------ heeeeeeeeeeeeeeeelooooooooo -------------------")
    }
  )

}

I'd like using my sayHello task before and after compile time. How can I do it?

I found relative question, but it's not about AutoPlugin extending.

faoxis
  • 1,912
  • 5
  • 16
  • 31

2 Answers2

0

You just need to adapt that answer to your situation. The answer stays the same: use dependsOn, but instead of build.sbt you add it to your plugin's projectSettings:

override def projectSettings = Seq(
  sayHello := { ... },
  Compile/compile := (Compile/compile).dependsOn(sayHello).value
)
laughedelic
  • 6,230
  • 1
  • 32
  • 41
  • Great! But I don't understand how I can import `Compile/compile`. `IntelliJ Idea` can't help me. :( Also, can I make something just after compile as well? – faoxis Aug 03 '18 at 05:00
  • What do you mean by "import `Compile/compile`"? If you're using sbt 0.13, write `compile in Compile` instead. To do something after a task read this: https://www.scala-sbt.org/release/docs/Howto-Sequential-Task.html – laughedelic Aug 04 '18 at 21:11
0

You need to override compile TaskKey, like:

override def projectSettings: Seq[Def.Setting[_]] = Seq(
    compile in Compile := Def.taskDyn {
          hello.value // call before
          val c = (compile in Compile).value // actual compilation
          Def.task {
            hello.value // call after
            c
          }
        }.value
)

def hello: Def.Initialize[Task[Unit]] = Def.task {
   println("hello")
}

Docs: https://www.scala-sbt.org/1.0/docs/Howto-Dynamic-Task.html#build.sbt+v2

Atais
  • 10,857
  • 6
  • 71
  • 111