How to add a custom command in sbt?

887 Views Asked by At

I'm trying to add a new command to my sbt.

In my build.sbt I have

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

In a sibling file mySuperDuperCommands.sbt I have

lazy val mySuperDuperTaskKey = TaskKey[Unit]("mySuperDuperCommand")

lazy val mySuperDuperCommand := { ... do *amazing* stuff ... }

It gives me the error cannot resolve symbol "mySuperDuperCommand" inside build.sbt. How to solve this conondrum?

2

There are 2 best solutions below

2
On BEST ANSWER

If you like to stay with a single build file then, in your build.sbt you can do:

lazy val mySuperDuperTask = TaskKey[Unit]("mySuperDuperCommand")
lazy val root = (project in file(".")).settings(mySuperDuperTask:= { ... })

of course by replacing ... with your own task implementation.

Alternatively, if you prefer to use two different files you can do something like this:

In project/Build.scala you can define your Task. For example:

import sbt._
import Keys._

object ProjectBuild {
    lazy val mySuperDuperTask = TaskKey[Unit]("mySuperDuperCommand", "Prints hello.")
    def buildSettings = Seq(mySuperDuperTask:={println("Hello")})
}

Then, in your build.sbt you can write:

import ProjectBuild._
lazy val root = (project in file(".")).settings(ProjectBuild.buildSettings : _*)

Then you can invoke your proper sbt mySuperDuperCommand.

Hope it helps!

0
On

Your line

lazy val mySuperDuperCommand := { ... do *amazing* stuff ... }

is wrong. := is a function that creates a SettingDefinition. Change the line to

lazy val mySuperDuperCommand: Def.SettingsDefinition = { 
  mySuperDuperTaskKey := { ... do *amazing* stuff ... }
}

and it should work.