Build and Setup Gradle Project

Waqar Mansoor
4 min readAug 14, 2020

Well if you are new to gradle or you are shifting to gradle from maven, this article will definitely help you out. According to the gradle official page gradle is an open-source build automation tool, if you are a paranoid just go and check the official link :-) https://docs.gradle.org/current/userguide/userguide.html,

here we will discuss only the practical approach.

Install Gradle

First you need to install gradle in your machine, you can either use package manager or the old school way a manual process, both of them are available in the official website https://gradle.org/install/

After installation type gradle in the command line to check if it is installed correctly.

$ gradle

Initialize a project

Well thats more easier than expected, just run this command

$ gradle init 

it will return you bunch of options, you need to select basic option and then select groovy, enter project name or leave it empty and hit enter that will bootstrap a basic gradle project structure for you, as you can see below .

Gradle Initialization

Understand the Gradle Strucuture

You can leave this section, if you are in such a hurry or save this for some other day, but if you are a geek than read it carefully.

Gradle Wrapper

We will start from gradle wrapper, if you look in the folder there you will find two files gradlew (for mac/unix based systems) and gradlew.bat (for windows). We use gradle wrapper to build our project with a standardized and consistent version of gradle, it may be possible that two developers sustain two different gradle versions in their machines to make the version consistent, gradle project folder carry its own version so that the developers always build with this standardized version, or incase of any urge of gradle version update it will be easy to distribute the updated version via gradle wrapper.

To use gradle wrapper commands instead of using just gradle, need to use gradlew, like below command is using locally installed gradle version

$ gradle build 

but this below command is using wrapper version of gradle

$ gradlew build

Last but not the least is gradle-wrapper.jar contains the code for downloading the gradle distribution, whereas gradle-wrapper.properties file manages the runtime properties for gradle. Both of these files are available in gradle/wrapper/ directory.

Build.gradle

This file is very important and its used for many purposes, here is the list of things that can be defined in this file

  • add plugins in a plugins block
plugins {
id 'java' //this is core plugin (gradle provided itself)
id 'org.springframework.boot' version '2.3.3.RELEASE' //this plugin is provided by community}
  • define properties in ext block in key/value pairs, these properties can be used through out the script, incase you are curious ext is for extra properties
ext {
springBootVersion = "3.1.1.RELEASE"
lombokVersion = "1.18.10"
}
  • define repositories of maven, ivy etc in repositories block
repositories {
mavenCentral()
mavenLocal()
maven {
url "https://maven.springframework.org/release"
}
}
  • Alternatively you can also use MavenBom (Bill of Materials) to use the recommended versions of the dependencies that best fit together, this help in resolving transitive dependencies as well.
dependencyManagement {
imports {
mavenBom ‘io.spring.platform:platform-bom:1.2.1.RELEASE’
}
}

After adding this block no need to define the dependency version in the dependencies block, it will automatically control the version of dependencies.

dependencies {
compile ‘org.springframework:spring-core’
}
  • Define dependencies
dependencies {
implementation "org.springframework.boot:spring-boot-starter- web:$springBootVersion"
compile("org.modelmapper:modelmapper:$modelMapperVersion")
compileOnly "org.projectlombok:lombok:$lombokVersion"
annotationProcessor "org.projectlombok:lombok:$lombokVersion"
testImplementation "org.springframework.boot:spring-boot-starter-test:$springBootVersion"
}

if you are curious about the different conventions of dependencies just follow this stackoverflow link https://stackoverflow.com/questions/44493378/whats-the-difference-between-implementation-and-compile-in-gradle

  • Some other petty things like group, version and sourceCompatibility are also a part of this file
group = ‘com.company.service’
version = ‘0.0.1-SNAPSHOT’
sourceCompatibility = ‘1.8’

Settings.gradle

This file is usually use to define submodules in the project with respect to the root object, if you are not familiar with the multi-module project approach check this official spring page https://spring.io/guides/gs/multi-module/

We can define submodules with root in settings.gradle like this

rootProject.name = ‘root-service’include ‘sub-module’

Always remember there should be only one settings.gradle in multi-module project.

Gradle.properties

This file is not created by default, but it can be added manually, in this file we define properties related to the build process, like configuring JVM properties for memory management etc.

org.gradle.daemon = false

We can put this file in either root of the project or in the gradle installation directory, for further information please visit this official documentation, https://docs.gradle.org/current/userguide/build_environment.html

Gradle Daemons

Gradle daemons are just the long lived background processes, that runs in the background to speed up the build process, they first start in the background once you initially fire up the JVM, for the next consecutive build they facilitate the build process by reusing the cached information and thus reduces the build time.

We can check the status of daemons by this command

$ gradle --status

To stop background daemons

$ gradle --stop

To increase the idle time we can add this property in gradle.properties file

org.gradle.daemon.idletimeout = 20800000

Spring Based Gradle Java Project

Here is the link of gradle based java spring project feel free to use in your projects, please don’t forget to give claps for our story.

--

--

Waqar Mansoor

I am a tech enthusiast and a visionary person and been in the software industry for the last 3 years, I believe in sharing knowledge with each other.