Dependency Management in Gradle

Dependency Management in Gradle

Learn to manage dependencies in Gradle. We'll look at declaring dependencies in a Gradle build script. We'll be using Gradle 6.7.

1. Overview

In this tutorial, we'll look at declaring dependencies in a Gradle build script. For our examples, we'll be using Gradle 6.7.

2. Typical Structure

Let's start with a simple Gradle script for Java projects:

plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter:2.3.4.RELEASE'
    testImplementation 'org.springframework.boot:spring-boot-starter-test:2.3.4.RELEASE'
}

As can be seen above, we have three code blocks: plugins, repositories, and dependencies.

First, the plugins block tells us that this is a Java project. Secondly, the dependencies block declares version 2.3.4._RELEASE of the _spring-boot-starter dependency needed to compile the project's production source code. Additionally, it also states that the project's test suite needs spring-boot-starter-test to compile.

The Gradle build pulls all dependencies down from the Maven Central repository, as defined by the repositories block.

Let's focus on how we can define dependencies.

3. Dependency Configurations

There are different configurations in which we can declare dependencies. In this regard, we can choose to be more or less precise, as we'll see later on.

3.1. How To Declare Dependencies

To start, the configuration has 4 parts:

  • group – identifier of an organization, company, or project
  • name – dependency identifier
  • version – the one we want to import
  • classifier – useful to distinguish dependencies with the same group, name, and version

We can declare dependencies in two formats. The contracted format allows us to declare a dependency as a String:

implementation 'org.springframework.boot:spring-boot-starter:2.3.4.RELEASE'

Instead, the extended format allows us to write it as a Map:

implementation group:'org.springframework.boot', name: 'spring-boot-starter', version: '2.3.4.RELEASE'

3.2. Types of Configuration

Furthermore, Gradle provides many dependencies configuration types:

  • api – used to make the dependencies explicit and expose them in the classpath. For instance, when implementing a library to be transparent to the library consumers'
  • implementation– required to compile the production source code and are purely internal. They aren't exposed outside the package
  • compileOnly– used when they need to be declared only at compile-time, such as source-only annotations or annotation processors. They don't appear in the runtime classpath or the test classpath
  • compileOnlyApi – used when required at compile time and when they need to be visible in the classpath for consumers
  • runtimeOnly– used to declare dependencies that are required only at runtime and aren't available at compile time
  • testImplementation– required to compile tests
  • testCompileOnly– required only at test compile time
  • testRuntimeOnly– required only at test runtime

We should note that the latest versions of Gradle deprecate some configurations like compile, testCompile, runtime, and testRuntime. At the time of writing, they're still available.

gradle java programming developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Top 10+ Java Development Companies in India & Developers Reviews 2020 – TopDevelopers.co

A thoroughly researched list of top 10 Java development companies in India with ratings & reviews to help find the best java developers around India.

Hire Java Developers Team | Java Developer India - Developers.dev

.DEV is a leading java Developer agency. Our offshore Java Developer Staff offers robust cross-platform java development services at an affordable rate. 1000+ Sucessful Projects, 100+ Happy Customers, 650+ Professionals, 60% Saving on Project Cost

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.

3 Best Programming Languages for Java Developers

In this article, I’ll share 3 JVM Programming languages Java programmers can learn and why you should learn them. Being a Polyglot developer is of good quality and often valued highly in interviews.