最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

scala - How to run CLI commands using build.sbt? - Stack Overflow

programmeradmin1浏览0评论

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
  • Not sure what do you mean with a CLI command. Do you want to run something like a bash script? Do you want to define a new task in sbt such as compile or test? – Gastón Schabas Commented Feb 4 at 15:39
  • @GastónSchabas I want to run a bash script or a command like for example 'touch filename.ext' command via sbt by mentioning it in build.sbt. I'm not sure how to do it.. – Anish B. Commented Feb 4 at 15:50
  • Something like sbt - external processes? – Gastón Schabas Commented Feb 4 at 16:02
  • @GastónSchabas Yes, external processes. Actually I want to bloopInstall command via sbt command by defining into build.sbt somehow. – Anish B. Commented Feb 4 at 16:09
  • I don't understand what exactly you want to achieve. Could sbt-bloop plugin solve your problem? If not, could you edit your post detailing a bit more about the context you have, the exactly command you want to execute and the expected output or behavior? – Gastón Schabas Commented Feb 4 at 17:52
 |  Show 2 more comments

1 Answer 1

Reset to default 2

You 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
发布评论

评论列表(0)

  1. 暂无评论