Skip to content

Instantly share code, notes, and snippets.

@jdigger
Last active March 21, 2017 19:05
Show Gist options
  • Save jdigger/7ff2c25d45eba5a2b585557db374daeb to your computer and use it in GitHub Desktop.
Save jdigger/7ff2c25d45eba5a2b585557db374daeb to your computer and use it in GitHub Desktop.
Gradle Wrapper search script
#!/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.
#
#
# 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 /src/my-proj/submod
# $ bash -x $(which gw) --version
# + GRADLEW=/src/my-proj/submod/gradlew
# + GRADLEW=/src/my-proj/gradlew
# + /src/my-proj/gradlew --version
# ...
#
#
# [1] http://gradle.org/docs/current/userguide/gradle_wrapper.html
CWD=$PWD
until [ $CWD == / ]; do
GRADLEW=$CWD/gradlew
if [ -e $GRADLEW ]; then
exec $GRADLEW $@
fi
CWD=$(dirname $CWD)
done
echo No Gradle wrapper found
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment