Skip to content

Instantly share code, notes, and snippets.

@MikeMitterer
Created February 7, 2018 22:29
Show Gist options
  • Save MikeMitterer/9b092bf909ce92d603abf1193b3bbd85 to your computer and use it in GitHub Desktop.
Save MikeMitterer/9b092bf909ce92d603abf1193b3bbd85 to your computer and use it in GitHub Desktop.
Gradle wrapper script / Mac friendly
#!/bin/bash
# PURPOSE
#
# The Gradle wrapper[1] is a simple and convenient way of making Gradle
# builds self-contained and reproducible. However, in multi-project
# builds, it can be cumbersome to type in relative paths e.g.:
# ./gradlew # when in root
# ../gradlew # when in subdir
#
# This script finds any Gradle wrapper (gradlew) executable in the
# current directory or any directory above it. If none can be found,
# it will fall back to the system-wide installation at $SYSTEM_GRADLE.
#
#
# INSTALLATION
#
# 1. Place this script somewhere on your PATH with precedence over GRADLE_HOME.
# For example, put this script in $HOME/bin and then add $HOME/bin as the
# first element on your $PATH.
##
# USAGE
#
# Use exactly like you would a normal Gradle executable. All arguments
# supplied are `exec`d against the gradle(w) executable once found.
#
# $ gradle [options]
#
#
# DEBUGGING
#
# To observe the search for gradlew and to ensure which one is
# ultimately used, invoke the script with Bash's "-x" option. Below you
# can see the directory traversal at work, finally selecting the
# 'gradlew' script one directory up from where 'gradle' was invoked.
#
# $ cd /Work/spring-framework/spring-context
# $ bash -x $(which gradle) --version
# + GRADLEW=/Work/spring-framework/spring-context/gradlew
# + GRADLEW=/Work/spring-framework/gradlew
# + /Work/spring-framework/gradlew --version
#
# ------------------------------------------------------------
# Gradle 4.4.1
# ------------------------------------------------------------
# ...
#
#
# AUTHOR
#
# Chris Beams (http://twitter.com/cbeams)
# Mike Mitterer (office@mikemitterer.at)
#
# BUGS
#
# It doesn't look for 'gradlew' in the root directory. Why would you
# want it to? Improvements welcome at http://github.com/cbeams/dotfiles.
#
# [1] http://gradle.org/docs/current/userguide/gradle_wrapper.html
#------------------------------------------------------------------------------
# COLORS
# Sample: echo -e "I ${RED}love${NC} Stack Overflow\n"
#
BLACK='\033[0;30m'
RED='\033[0;31m'
GREEN='\033[0;32m'
BROWN='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
LIGHT_GRAY='\033[0;37m'
DARK_GREY='\033[1;30m'
LIGHT_RED='\033[1;31m'
LIGHT_GREEN='\033[1;32m'
YELLOW='\033[1;33m'
LIGHT_BLUE='\033[1;34m'
LIGHT_PURPLE='\033[1;35m'
LIGHT_CYAN='\033[1;36m'
WHITE='\033[1;37m'
NC='\033[0m' # No Color
SUCCESS=${GREEN}
INFO=${BLUE}
WARNING=${YELLOW}
FAILED=${RED}
ERROR=${RED}
# Check if File exists
SYSTEM_GRADLE=/usr/local/bin/gradle
if [ ! -f ${SYSTEM_GRADLE} ]; then
SYSTEM_GRADLE=/usr/bin/gradle
fi
CWD=$PWD
until [ $CWD == / ]; do
GRADLEW=$CWD/gradlew
if [ -e $GRADLEW ]; then
exec $GRADLEW $@
fi
CWD=$(dirname $CWD)
done
echo -e "${WARNING}No Gradle wrapper found, using: ${GREEN}$SYSTEM_GRADLE${NC}"
exec ${SYSTEM_GRADLE} "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment