I want to define a CLI command in build.sbt
and want it to be executed when I run the sbt
command from the shell script.
For example: I have already included sbt-bloop
plugin in my scala-sbt project.
Now, the issue is I have to run these commands:
sbt bloopInstall
sbt run
But I don't have to do the above instead I just want to run:
sbt run
This command should run the bloopInstall
command too.
How to achieve this?
I want to define a CLI command in build.sbt
and want it to be executed when I run the sbt
command from the shell script.
For example: I have already included sbt-bloop
plugin in my scala-sbt project.
Now, the issue is I have to run these commands:
sbt bloopInstall
sbt run
But I don't have to do the above instead I just want to run:
sbt run
This command should run the bloopInstall
command too.
How to achieve this?
Share Improve this question edited Feb 4 at 18:27 Anish B. asked Feb 4 at 14:46 Anish B.Anish B. 16.6k4 gold badges28 silver badges50 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 2You have to add the following line to your build.sbt
to make a task depends on another one
run := (Compile / run).dependsOn(bloopInstall).evaluated
with this, we are making the task run
depends on bloopInstall
and you have to call evaluated
then to make the task be executed.
You can validate it running the inspect commnad
sbt "inspect run"
and you will get something like
[info] Input task: Unit
[info] Description:
[info] Runs a main class, passing along arguments provided on the command line.
[info] Provided by:
[info] ProjectRef(uri("file:/path/to/project"), "root") / run
[info] Defined at:
[info] /path/to/project/build.sbt:<line-definition> // not defining anything will be something like `Defaults.scala:1026`
[info] Dependencies:
[info] Compile / run
[info] bloopInstall // the task `bloopInstall` is added as a dependency
[info] Delegates:
[info] run
[info] ThisBuild / run
[info] Global / run
[info] Related:
[info] Compile / run
[info] Test / run
If you want something more custom, for example running first the task run
and then bloopInstall
you could do something like the following
run := {
(Compile / run).evaluated
bloopInstall.value
}
and if we run the inspect
command again we will get the following output
[info] Input task: Unit
[info] Description:
[info] Runs a main class, passing along arguments provided on the command line.
[info] Provided by:
[info] ProjectRef(uri("file:/path/to/project"), "root") / run
[info] Defined at:
[info] /path/to/project/build.sbt:<line-definition>
[info] Dependencies:
[info] bloopInstall // the order was inverted
[info] Compile / run
[info] Delegates:
[info] run
[info] ThisBuild / run
[info] Global / run
[info] Related:
[info] Compile / run
[info] Test / run
compile
ortest
? – Gastón Schabas Commented Feb 4 at 15:39build.sbt
. I'm not sure how to do it.. – Anish B. Commented Feb 4 at 15:50bloopInstall
command via sbt command by defining intobuild.sbt
somehow. – Anish B. Commented Feb 4 at 16:09