To execute the script, and then complete the script, proceed to execute another sbt command, one approach is to implement a custom sbt command that does the following:
- Executes the external process provided as the first argument, passing any additional arguments to this process.
- Waits for the external process to complete.
- After the external process completes, it runs another sbt command.
This is shown in this Build.scala file:
import sbt._ import Keys._ // imports standard command parsing functionality import complete.DefaultParsers._ object CommandExample extends Build { // Declare a project, adding new commands. lazy override val projects = Seq(root) lazy val root = Project("root", file(".")) settings( commands ++= Seq(start, customStart) ) // A fake "start" command. def start = Command.command("start") { state => println("Fake start command executed.") state } // A command that executes an external command before executing the "start" command. // The name of the external command is the first parameter. // Any additional parameters are passed along to the external command. def customStart = Command.args("customStart", "<name>") { (state, args) => if (args.length > 0) { val externalCommand = args.mkString(" ") println(s"Executing '$externalCommand'") externalCommand ! } "start" :: state } }
This is an example of execution:
$ sbt [info] Loading project definition from /home/fernando/work/github/fernandoacorreia/so24565469/project [info] Set current project to hello (in build file:/home/fernando/work/github/fernandoacorreia/so24565469/) > customStart ls -la Executing 'ls -la' total 40 drwxr-xr-x 6 fernando fernando 4096 Jul 8 10:52 . drwxr-xr-x 27 fernando fernando 4096 Jul 8 08:51 .. -rw-r--r-- 1 fernando fernando 95 Jul 8 10:47 build.sbt drwxr-xr-x 8 fernando fernando 4096 Jul 8 08:58 .git -rw-r--r-- 1 fernando fernando 203 Jul 8 09:04 .gitignore -rw-r--r-- 1 fernando fernando 1082 Jul 8 08:51 LICENSE drwxr-xr-x 3 fernando fernando 4096 Jul 8 10:51 project -rw-r--r-- 1 fernando fernando 111 Jul 8 08:51 README.md drwxr-xr-x 3 fernando fernando 4096 Jul 8 08:57 src drwxr-xr-x 2 fernando fernando 4096 Jul 8 10:52 target Fake start command executed. >
The external command passed as the customStart parameter can be any executable command, for example. binary file or shell script.
For more information on creating commands, see the Documentation documentation page.
To learn more about running external processes, see the External Process Documentation page.
A complete example is available for download in this GitHub repository .
Since sbt executes the script (as a child process), the script cannot execute commands in the original (parent) sbt process.
It would be possible to write the script output to either stdout or a file, and the sbt command to execute an arbitrary command created by the script, but a bit confusing.
Another approach is to invert execution order. Instead of sbt executing the script, the script will be executed first, directly from the shell, and the script will execute the sbt command.
For example, this script:
Produces this conclusion:
$ ./script Running custom script [info] Loading project definition from /home/fernando/work/github/fernandoacorreia/so24565469/project [info] Set current project to hello (in build file:/home/fernando/work/github/fernandoacorreia/so24565469/) Fake start command executed.