Uploaded image for project: 'Gradle'
  1. Gradle
  2. GRADLE-2429

A script to search upward for a gradlew to use

    Details

    • Type: New Feature
    • Status: Resolved
    • Resolution: Duplicate
    • Affects Version/s: 1.1
    • Fix Version/s: None

      Description

      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 Not sure if the windows script works in XP, but it definitely works in Vista and above.

      *Nix

      #!/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
      

      Windows

      @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%"
      

        Attachments

        1. grad
          2 kB
          Joern Huxhorn

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                forums Gradle Forums
              • Votes:
                8 Vote for this issue
                Watchers:
                16 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: