Skip to content

Instantly share code, notes, and snippets.

@marianogappa
Last active July 24, 2020 08:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marianogappa/265876f9e69505b4635e to your computer and use it in GitHub Desktop.
Save marianogappa/265876f9e69505b4635e to your computer and use it in GitHub Desktop.
Echoes the build.sbt project version to STDOUT
#!/usr/bin/env bash
# Echoes the build.sbt project version to STDOUT.
#
# NOTES:
# - place this script on the root of your project
# - this script will then work on the first build.sbt found anywhere on your project
# - this script assumes your project version setting is a literal e.g.: `version := "0.1-SNAPSHOT"`
# Exit on unset variables or if any of the following commands returns non-zero
set -eu
# cd to path of current script
# (based on http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in)
SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
cd $SCRIPT_DIR
# Find the first build.sbt in the path tree of this script,
# find the version line and extract the content within double quotes
find '.' -name "build.sbt" |
head -n1 |
xargs grep '[ \t]*version :=' |
head -n1 |
sed 's/.*"\(.*\)".*/\1/'
# check for updates to this script at https://gist.github.com/MarianoGappa/265876f9e69505b4635e
@ronaldvanrij
Copy link

Versions can be set in a variety of ways, better is to let sbt figure that out and then cleaning up the result:
sbt 'inspect actual version' | grep "Setting: java.lang.String" | cut -d '=' -f2 | tr -d ' '

@TheTaylorHicks
Copy link

Versions can be set in a variety of ways, better is to let sbt figure that out and then cleaning up the result:
sbt 'inspect actual version' | grep "Setting: java.lang.String" | cut -d '=' -f2 | tr -d ' '

If you intend to use this command in scripts, make sure to add the -no-color flag to SBT to avoid having shell-control characters polluting your version.

sbt -no-color 'inspect actual version' | grep "Setting: java.lang.String" | cut -d '=' -f2 | tr -d ' '

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment