Gradle Wrapper

Gradle Wrapper | Gradle Wrapper let’s run the Gradle project without installing the Gradle software into our system. It can dynamically download the Gradle at runtime and then run the application.

GradleApp
    -> src
        -> main
            -> java
                -> com.knowprogram
                    -> Arithmetic.java
        -> test
            -> java
    -> build.gradle

In Arithmetic.java:-

package com.knowprogram;
public class Arithmetic {

    public int sum(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        System.out.println("Welcome to Gradle");
        Arithmetic arithmetic = new Arithmetic();
        System.out.println("Result: "+ arithmetic.sum(10, 331));
    }
}

In build.gradle:-

apply plugin: 'java'
version = '1.0'

task runApp(type: JavaExec) {
    main = 'com.knowprogram.Arithmetic'
    classpath = sourceSets.main.runtimeClasspath
}

// get wrapper support
task myTask(type: Wrapper)

Execute gradle wrapper command in CMD from the project directory which will generate gradlew and gradlew.bat files in the project directory.

> gradle wrapper
gradlew & gradle.bat files

Now, let us remove Gradle’s path from the system. In Windows, open “Advanced System Settings” => Edit Path => Remove “Gradle”. Open a new CMD because changes will not be reflected in the old CMD. In the new CMD, verify gradle -v, it should give the message:- ‘gradle’ is not recognized as an internal or external command, operable program, or batch file.

Now, since Gradle is not configured. Let us run the application through gradlew run without having Gradle installed in the system.

> gradlew run

Downloading https://services.gradle.org/distributions/gradle-8.9-bin.zip
............10%.............20%.............30%.............40%.............50%
.............60%.............70%.............80%.............90%.............100%

> Task :runApp
Welcome to Gradle-1
Result: 341

The gradlew run will first download the Gradle and then run the application. If we run gradlew run once again, it won’t download the Gradle and directly run the application. In the Windows C:\Users\<user>\.gradle\wrapper\dists folder, you can see the Gradle Wrapper-related installations.

gradlew

We can send this application folder having gradlew files to another computer using Git or SVN and they can run the GradleApp without installing Gradle on their computer by just using gradlew run.

When we use gradlew runcommand then it downloads the latest version of Gradle software but if the application is developed using another version then we can specify the version information while executing gradlew run command as follows:-

> gradlew wrapper --gradle-version 6.4.1

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *