Gradlew scripts in gradle/bin to find gradlew scripts upwards within project space


(spencer_allain) #1

Using gradlew is nice for a lot of reasons, but it somewhat forces you to run things from the command line at the top project level only or use …/…/gradlew depending upon where in the subproject tree one is.

How about if there were scripts called gradlew and gradlew.bat that shipped with gradle in the bin directory (so they’d be picked up along the path if a gradle instance were added to someone’s path) and they simply attempt to walk up the current directory structure looking for a gradlew/gradlew.bat file.

Below are some scripts that do just that - whether they are well written is a separate issue :wink:

Not sure if the windows script works in XP, but it definitely works in Vista and above.

gradlew ======================================================================================

#!/bin/bash
  FOUND=0
CURR_PATH="$PWD"
REAL_GRADLEW="$CURR_PATH/gradlew"
  if [ -x "$REAL_GRADLEW" ]
then
  FOUND=1
else
  while [ "$CURR_PATH" != "/" ]
  do
    CURR_PATH=$(dirname "$CURR_PATH")
    REAL_GRADLEW="$CURR_PATH/gradlew"
      if [ -x "$REAL_GRADLEW" ]
    then
      FOUND=1
      break
    fi
  done
fi
  if [ $FOUND -eq 1 ]
then
  $REAL_GRADLEW "$@"
else
  echo "Unable to find gradlew file upwards in filesystem"
fi
  exit 0

gradlew.bat ======================================================================================

@echo off
setlocal enabledelayedexpansion
set CURR_PATH=%cd%
  set REAL_GRADLEW=%CURR_PATH%\gradlew.bat
  if exist %REAL_GRADLEW% (
  goto :found
)
  :while1
  call :getdir "%CURR_PATH%"
    set REAL_GRADLEW=!CURR_PATH!\gradlew.bat
    if exist !REAL_GRADLEW! (
    goto :found
  )
    if "%CURR_PATH:~-1%" == ":" (
    goto :notfound
  )
goto :while1
  :notfound
echo Unable to find gradlew.bat file upwards in filesystem
goto :goodbye
  :found
call !REAL_GRADLEW! %*
  :goodbye
endlocal
goto :EOF
  :getdir
set "CURR_PATH=%~dp1"
set "CURR_PATH=%CURR_PATH:~0,-1%"

-Spencer


(Luke Daley) #2

I’ve raised this to the tracker as a new feature as GRADLE-2429.

It’s certainly a good idea. We’d have to sort out how this is distributed.