Gradle

April 15, 2025 ยท View on GitHub

https://gradle.org/

Newer JVM build system written in Groovy with a nicer build configuration file build.gradle.

Good replacement for Maven.

Key Points

  • good docs
  • good plugin support
  • easy to work with
  • Gradle Wrapper ./gradlew shell script for guaranteed Gradle version download for compatibility

See Gradle cookbook.

Gradle Daemon runs in the background:

gradle --stop

or if changing versions, gradle only manages its current version, so:

pkill -f org.gradle.launcher.daemon.bootstrap.GradleDaemon

Templates

build.gradle and gradle.properties templates can be found here:

Readme Card

wget -nc https://raw.githubusercontent.com/HariSekhon/Templates/master/build.gradle
wget -nc https://raw.githubusercontent.com/HariSekhon/Templates/master/gradle.properties

Gradle Wrapper

Generates:

  • gradlew / gradlew.bat scripts
  • gradle/wrapper/gradle-wrapper.jar
    • stub program that downloads the exact same version of gradle recorded in gradle-wrapper.properties as you just ran or the version specifed by --gradle-version eg. 2.0
  • gradle/wrapper/gradle-wrapper.properties

All of the above should be checked in to revision control.

gradle wrapper
git add -f gradlew gradlew.bat gradle/
git ci -m "added gradle wrapper"

In future gradle commands just substitute gradle for ./gradlew.

This downloads same gradle version as original builder to

.gradle/wrapper/dists/gradle-2.11-bin/

and uses that:

./gradlew ...

build.gradle

Create build.gradle - will port existing pom.xml if found:

gradle init
  • must specify rootProject.name otherwise taken to be name of containing directory which can be random on CI systems
  • name = property is read-only in build.gradle

settings.gradle:

rootProject.name = 'harisekhon-utils'

HariSekhon/Templates - build.gradle

build.gradle structure:

  • project
    • tasks
gradle --help

Show list of available tasks:

gradle tasks

Gradle build is equivalent to gradle check assemble - creates artifacts in build/libs/:

gradle build

Requires maven plugin - create artifacts + installs to ~/.m2/repository:

gradle install
  • build/
    • dependencies_cache
    • libs - find jar artifact here
    • reports/tests - open index.html for breakdown of unit tests

All verification tasks, including test. Some plugins add more checks (eg. CheckStyle):

gradle check

Create archives / jars:

gradle assemble

Show tree graph of dependencies for different stages, test:

gradle dependencies

Specifying Java version

compileOptions {
    sourceCompatibility JavaVersion.VERSION_21
    targetCompatibility JavaVersion.VERSION_21
}

kotlinOptions {
    jvmTarget = '21'
}

Plugins

Adds support for compiling languages:

apply plugin: 'java'
apply plugin: 'scala'
apply plugin: 'groovy'

adds install task for publishing to Maven repo, generates poms under build/poms/:

apply plugin: 'maven'

There is also a factory method for generating the pom without uploading to a Maven repo:

see https:docs.gradle.org/current/userguide/maven_plugin.html#N13A3E

executable jar:

apply plugin: 'application'
mainClassName = 'com.linkedin.harisekhon.Main'

Kotlin

https://kotlinlang.org/docs/gradle.html

https://kotlinlang.org/docs/gradle-best-practices.html#use-remote-build-cache

https://kotlinlang.org/docs/gradle-compilation-and-caches.html

Uber jar

  • ShadowJar
  • Application plugin

SonarQube Gradle Plugin

http:docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Gradle

https://docs.sonarsource.com/sonarqube-cloud/advanced-setup/ci-based-analysis/sonarscanner-for-gradle/

SonarQube Gradle plugin - add to build.gradle.

Unfortunately the gradle sonar plugin seems to not support sonar-project.properties

SonarQube

Configure ~/.gradle/gradle.properties:

systemProp.sonar.host.url=...
# optionally
systemProp.sonar.login=...
systemProp.sonar.password=...

SonarQube CLI Properties

Passing SonarQube options on the command line:

gradle sonarqube -Dsonar.host.url=http:sonar.mycompany.com \
                 -Dsonar.jdbc.password=myPassword \
                 -Dsonar.verbose=true

VersionEye

See HariSekhon/lib-java:

build.gradle:

plugins { id "org.standardout.versioneye" version "1.4.0" }

gradle.properties:

versioneye.projectid=<long_num_from_project_page_no_spaces>
gradle versionEyeUpdate

Ported from private Knowledge Base page 2016+