sbtsbt-release

How to define SBT Release task in Build.scala?


I'm defining my build/release process in build.sbt. I also have a project/Build.scala that has some definitions which are used in build.sbt. Doing this to keep build.sbt readable.

I'm trying to add a few new tasks to the releaseProcess. I'm defining these tasks in Build.scala as

object StartService {
  val myNewTask = taskKey[Unit]("Execute a CLI command")
  myNewTask := {
    streams.value.log.info(s"Executing command")
    s"cmd $arg1 $arg2" !
  }
}

build.sbt has

releaseProcess := Seq[ReleaseStep](
...
  releaseStepTask(StartService.myNewTask)
...
)

When executing release command I keep getting an exception that myNewTask is undefined. What is the right way to import tasks defined in Build.scala?

java.lang.RuntimeException: /:myNewTask is undefined. at scala.sys.package$.error(package.scala:27) at sbt.Extracted$$anonfun$getOrError$1.apply(Extracted.scala:94) at sbt.Extracted$$anonfun$getOrError$1.apply(Extracted.scala:94) at scala.Option.getOrElse(Option.scala:120)


Solution

  • You need to add the task definition to your project. Just by being in an object, that doesn't happen. The simplest way is to add the setting directly into build.sbt.

    To automatically add it to several projects, you could define it as a plugin in your project directory:

    object SbtScaryPlugin extends AutoPlugin {
    
      override def trigger = allRequirements
      override def requires = JvmPlugin
    
      object autoImport {
        val myNewTask = taskKey[Unit]("Execute a CLI command")
      }
    
      import autoImport._
    
      override lazy val projectSettings = Seq(
        myNewTask := {
          streams.value.log.info(s"Executing command")
          s"cmd $arg1 $arg2" !
        }
      )
    }