Gradle

Gradle Wrapper Example

In this example, we will understand how Gradle can be used to build projects even if the developer machine doesn’t have Gradle installed, by using Gradle Wrapper. This is a best practice to unify the Gradle version used by the entire development team.

1. What’s Gradle Wrapper?

Gradle Wrapper is a type batch or shell script that downloads and automatically configures Gradle to execute tasks. Imagine that you want to run a Gradle build, well you need to download and install Gradle in your computer, so this concept allows is to distribute our project and build configurations with no need to have Gradle installed.

2. What we need to start?

This is a simple example, really you only need Gradle to start and to create the base wrapper to distribute to all others, but to make more readable will use Eclipse. So

  1. As IDE: Eclipse Luna 4.4
  2. Eclipse Gradle Plugin
  3. Java JDK 1.7
  4. Gradle 2.3 or higher

3. Environment Configuration

Please set your Gradle environment variables and install the Gradle plugin on your IDE. To avoid to be boilerplate visit this previous posts that show how to configure your Gradle Environment. Gradle Hello World Tutorial

4. Creating Wrapper Script

In Eclipse, Create a new Gradle Project and then edit gradle build script.

Gradle Wrapper Project
Gradle Wrapper Project

Then, in the build.gradle script we must add a task of type org.gradle.api.tasks.wrapper.Wrapper to customize the configuration of the default Wrapper task.

task createGradleWrapper(type: Wrapper) {
	gradleVersion = '2.3'
	scriptFile = 'GradleJ' //define a new name for gradle
	jarFile = 'gradle-bin.jar' //jar with files to download and invoke Gradle
	distributionUrl = 'https://services.gradle.org/distributions/gradle-2.3-bin.zip'
}

Then, we can execute this task to generate the wrapper files. Execute gradle createGradleWrapper or gradle cGW in abbreviated form on Windows command shell. This is the output:

C:\Users\Andres\workspaceLuna\GradlWrapperExample>gradle cGW
:GradlWrapperExample:createGradleWrapper UP-TO-DATE

BUILD SUCCESSFUL

Total time: 1.044 secs
C:\Users\Andres\workspaceLuna\GradlWrapperExample>

5. Using Gradle Wrapper

Then, after the execution of the task, two files are generated: gradlej and gradlej.bat in the root of the project (so refresh it to see them), that contain all the logic and configurations to run Gradle.

This new files are part of the project, so is a good practice add these files to the version control, to able team people that checkout project and build the scripts with gradle and gradlew by default if you don’t customize the name) instead of theirs Gradle version.

So, to test Gradle Wrapper we add this simple task and we run with gradlej instead of gradle command.

task helloWrapper << {
	println 'Welcome to JCG Gradle Wrapper Tutorial'
}

Execute this command gradlej helloWrapper or gradlej hW in abbreviated form on Windows command shell. This is the output:

C:\Users\Andres\workspaceLuna\GradlWrapperExample>gradlej hW
:GradlWrapperExample:helloWrapper
Welcome to JCG Gradle Wrapper Tutorial

BUILD SUCCESSFUL

Total time: 1.445 secs
C:\Users\Andres\workspaceLuna\GradlWrapperExample>

This is how we can use Gradle wrapper to build projects even if other developers don’t have Gradle installed.

6. Key Points

Tips

  • Gradle Wrapper is a good practice to standardize the builds
  • Is a good practice too, add Gradle Wrapper in a control version system to distribute to the team
  • distributionUrl property can used to reference a download URL in your company intranet or a custom fixed Gradle version.
  • If you build via Gradle Wrapper, any Gradle version installed in the PC is ignored.
  • You will save time on installing and setting Gradle in every developer’s machine.

7. Download the Eclipse Project

This was an example of Gradle Wrapper.

Download
You can download the full source code of this example here: Gradle Wrapper Project

Andres Cespedes

Andres is a Java Software Craftsman from Medellin Colombia, who strongly develops on DevOps practices, RESTful Web Services, Continuous integration and delivery. Andres is working to improve software process and modernizing software culture on Colombia.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button