Job Board
Consulting

SBT Jar Name for Scripting

When using sbt in spark scala projects it can be useful to access the name of the assembly that will be created with sbt within your bash or zsh shell scripts. Thankfully extracting the values is rather straight forward.

Extracting Assembly Jar Name from SBT

The approach is to create a task in sbt that will print the value to standard out.

Here in this example we will output both the name and the full path. Add the following to your build.sbt file:

lazy val printJarPath = taskKey[Unit]("Prints the absolute jar path")
printJarPath := {
  println(s"${(assemblyOutputPath in assembly).value}")
}

lazy val printJarName = taskKey[Unit]("Prints the absolute jar name")
printJarName := {
  println(s"${(assemblyJarName in assembly).value}")
}

Once you have these functions/tasks defined within sbt you can extract them with sbt within your shell scripts by capturing the output of sbt:

JAR_PATH=$(sbt -batch -error printJarPath)
JAR_NAME=$(sbt -batch -error printJarName)

For older versions of sbt that don't have the command line switches -batch of -error you can use tail and head to extract only the desired stdio output as follows:

JAR_PATH=$(sbt printJarPath | tail -2 | head -1)
JAR_NAME=$(sbt printJarName | tail -2 | head -1)
Example Details

Created: 2023-11-20 02:05:00 PM

Last Updated: 2023-11-20 02:05:00 PM