Gradle recommends the Gradle Wrapper as the preferred way to execute any Gradle build. In a nutshell, the Gradle Wrapper is a shell script that downloads and invokes a given version of Gradle. This has two main advantages. The first is that you don't need to have a Gradle setup in your environment. The second is that you can guarantee that the build script is always executed with the same version of Gradle.
Adding the Gradle Wrapper is as simple as executing the following command:
$ gradle wrapper > Task :wrapper BUILD SUCCESSFUL in 0s 1 actionable task: 1 executed
The process is really straightforward, as described in the Gradle documentation. You can even configure most of the wrapper properties by using command line options: options:
--distribution-type, etc. However, this assumes that you have a valid Gradle setup in your environment (which basically defeats the purpose of the wrapper).
If you don't have a valid Gradle setup in your system, but you do have Docker, you can still add the wrapper. You can execute the following command to add the Wrapper:
docker run --rm \ -u "$(id -u):$(id -g)" \ -v $(pwd):/opt/gen \ -w /opt/gen \ gradle:6.9-jre /usr/bin/gradle wrapper
The behavior is the same as if running the command from a local Gradle distribution, so the same command line options apply:
docker run --rm \ -u "$(id -u):$(id -g)" \ -v $(pwd):/opt/gen \ -w /opt/gen \ gradle:6.9-jre /usr/bin/gradle wrapper --gradle-version 7.1.1 --distribution-type all
The previous command runs the provided
/usr/bin/gradle command within the
gradle:6.9-jre Docker Image based container.
--rmremoves the Docker container once the command completes.
-u "$(id -u):$(id -g)"forces the Docker container to run as the current user:group. This will allow gradle to generate the Wrapper files with the right permissions.
-v $(pwd):/opt/genmounts the current local directory into the container so that the generated files are persisted locally.
-w /opt/gensets the working directory so that gradle generates the wrapper in the mounted directory.
Finally, to make the Gradle Wrapper files available to the rest of the team, you'll need to check the following files and directories into your version control system (everything except
├── gradle │ └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew └── gradlew.bat