Skip to content

Instantly share code, notes, and snippets.

@sagrawal31
Created March 9, 2017 09:16
Show Gist options
  • Save sagrawal31/4dc4f9a852935fba4078954a28120721 to your computer and use it in GitHub Desktop.
Save sagrawal31/4dc4f9a852935fba4078954a28120721 to your computer and use it in GitHub Desktop.
Auto use Grails 3 versions from gradle.properties
#!/usr/bin/env bash
set -e
# Handy script to auto use the Grails 3 version based on what is defined in the "gradle.properties". This script uses
# the Grails installed by the famous SDKMan so it will look the Grails installation at "~/.sdkman/candidates/grails"
# so only make sure the particular Grails version is installed using "sdk install grails <version>".
#
# The only thing to care about is that this script should be included first in the "PATH" before the
# ".sdkman/candidates/grails/current" is included.
#
# Keeping the name of this script as "grails" so that you don't have to write anything else in order to use the auto
# grails version select feature.
grailsVersionToUse=""
function parseGradleProperties() {
while read LINE
do
propertyName=${LINE%=*}
propertyValue=${LINE#*=}
if [ $propertyName == "grailsVersion" ]; then
grailsVersionToUse=$propertyValue
fi
done < <(cat gradle.properties ) # Using process substitution
}
echo -e "${GREEN}➤ Using custom grails command script${NONE}"
if [ -f gradle.properties ]; then
parseGradleProperties
echo -e "${BLUE}✓ Using grails version ${grailsVersionToUse}${NONE}\n"
desiredGrailsCommandPath=~/.sdkman/candidates/grails/$grailsVersionToUse/bin/grails
# Check if particular Grails version is installed or not
if [ -f $desiredGrailsCommandPath ]; then
$desiredGrailsCommandPath $@
else
echo -e "${RED}✗ Grails ${grailsVersionToUse} is not installed. Please install using"\
"${YELLOW}sdk install grails ${grailsVersionToUse}${NONE}${NONE}"
fi
else
echo -e "${RED}⚠ Current working directory is not a Grails 3 repository, using default Grails${NONE}\n"
~/.sdkman/candidates/grails/current/bin/grails $@
fi
@sagrawal31
Copy link
Author

sagrawal31 commented Mar 9, 2017

Just make sure, in the $PATH variable, this script is included before the ~/.sdkman/candidates/grails/current is included.

Now just run whatever commands you want to run grails run-app, grails war or any grails command, it will auto use the version as defined in the gradle.properties. For colorful output, you may add the following to your ~/.bashrc or ~/.zshrc:

export NONE='\033[00m'
export RED='\033[01;31m'
export GREEN='\033[01;32m'
export YELLOW='\033[01;33m'
export BLUE='\e[0;36m'
export BOLD='\033[1m'
export UNDERLINE='\033[4m'

Outputs can be like:

When default version is set to 3.2.3 and app version is 3.2.4
image

When we are not at the root of any Grails 3 app
image

When a particular Grails 3 version is not installed
image

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