Created
March 9, 2017 09:16
-
-
Save sagrawal31/4dc4f9a852935fba4078954a28120721 to your computer and use it in GitHub Desktop.
Auto use Grails 3 versions from gradle.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 thegradle.properties
. For colorful output, you may add the following to your~/.bashrc
or~/.zshrc
:Outputs can be like:
When default version is set to 3.2.3 and app version is 3.2.4

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

When a particular Grails 3 version is not installed
