3

I've just written my first SBT Autoplugin which has a custom task that generates a settings file (if the file is not already present). Everything works as expected when the task is explicitly invoked, but I'd like to have it automatically invoked prior to compilation of the project using the plugin (without having the project modify it's build.sbt file). Is there a way of accomplishing this, or do I somehow need to override the compile command? If so, could anyone point me to examples of doing so? Any help would be extremely appreciated! (My apologies if I'm missing something simple!) Thanks!

Timothy Perrigo
  • 723
  • 4
  • 18

2 Answers2

6

You can define dependencies between tasks with dependsOn and override the behavior of a scoped task (like compile in Compile) by reassigning it.

The following lines added to a build.sbt file could serve as an example:

lazy val hello = taskKey[Unit]("says hello to everybody :)")

hello := { println("hello, world") }

(compile in Compile) := ((compile in Compile) dependsOn hello).value

Now, every time you run compile, hello, world will be printed:

[IJ]sbt:foo> compile
hello, world
[success] Total time: 0 s, completed May 18, 2018 6:53:05 PM

This example has been tested with SBT 1.1.5 and Scala 2.12.6.

stefanobaghino
  • 11,253
  • 4
  • 35
  • 63
  • If I understand correctly, the `(compile in Compile)...` would have to be added to the build.sbt for every project that uses the Autoplugin, is that correct? I'm looking for a way to define the task in the Autoplugin so that it is executed prior to compilation (without requiring projects that use the plugin to alter their build files). Is this possible, perhaps with some variation of what you posted? (I apologize if I've misunderstood...Still trying to get the hang of creating plugins!) – Timothy Perrigo May 18 '18 at 18:13
  • That did seem to do the trick once I moved it into the (overridden) project settings in the plugin. I apologize for misunderstanding...Thanks for the help! – Timothy Perrigo May 22 '18 at 18:50
  • override lazy val projectSettings = Seq( (Compile / compile) := ((Compile / compile) dependsOn generateXmlMeta).value ) – mogli Sep 02 '21 at 16:17
  • this is not working for me. kindly suggest – mogli Sep 02 '21 at 16:18
2
val runSomeShTask = TaskKey[Unit]("runSomeSh", " run some sh")
    lazy val startrunSomeShTask = TaskKey[Unit]("runSomeSh", " run some sh")
    startrunSomeShTask := {
      val s: TaskStreams = streams.value
      val shell: Seq[String] = if (sys.props("os.name").contains("Windows")) Seq("cmd", "/c") else Seq("bash", "-c")
      // watch out for those STDOUT , SDERR redirection, otherwise this one will hang after sbt test ... 
      val startMinioSh: Seq[String] = shell :+ " ./src/sh/some-script.sh"
      s.log.info("set up run some sh...")
      if (Process(startMinioSh.mkString(" ")).! == 0) {
        s.log.success("run some sh setup successful!")
      } else {
        throw new IllegalStateException("run some sh setup failed!")
      }
    }

    // or only in sbt test 
    // test := (test in Test dependsOn startrunSomeShTask).value
    (compile in Compile) := ((compile in Compile) dependsOn startrunSomeShTask).value
Yordan Georgiev
  • 5,114
  • 1
  • 56
  • 53