Skip to content

gradle/dpeuni-gradle-intro-devs-deps

Repository files navigation

DPE University Training

Gradle Dependency Management Exercise

This is a hands-on exercise to go along with the Introduction to Gradle Build Tool for Developers training module. In this exercise you will go over the following:

  • See dependencies for a project
  • Adding dependencies
  • Examining dependency conflict resolution

Prerequisites

  • Completed the first and second hands-on exercise
  • You can perform the exercises in the same Gradle project used in previous exercises

See Dependencies for a Project

  1. Open the terminal run ./gradlew :app:dependencies. You will see dependencies grouped by configurations.

  1. To see the dependencies of just the runtimeClasspath configuration run ./gradlew :app:dependencies --configuration=runtimeClasspath


Adding Dependencies

Let's add some more functionality to our application. Currently it only prints a greeting message. Let's make it fetch some data from a url and print it. We will use the Google Http Client for this.

We need to get the Gradle dependency statement for this library. Follow the steps below to get this:

  1. Go to https://mvnrepository.com/

  2. In the search bar type google http client

  3. One of the top results will be com.google.http-client » google-http-client, click on it, and then click on the 1.44.1 version

  4. Click on the Gradle (Kotlin) tab

  5. Copy just the module ID and version, not the dependency configuration

  1. Open the version catalog file which is gradle/libs.versions.toml

  2. Under the libraries section, add an entry called google-http-client and set it to the copied contents:

google-http-client = "com.google.http-client:google-http-client:1.44.1"
  1. If using IntelliJ, hit the Gradle refresh icon

  1. In app/build.gradle.kts, in the dependencies section add an implementation dependency to libs.google.http.client. Notice that instead of dashes you will use dots:
implementation(libs.google.http.client)
  1. Run ./gradlew :app:dependencies --configuration=runtimeClasspath again and notice the additional dependencies - both direct and transitive.


Examining Dependency Conflict Resolution

  1. Notice in the dependencies, the google-http-client library depends on guava, which is also a direct dependency.

  1. The guava version google-http-client depends on is older than the version in the direct dependency, so you will see Gradle handles the conflict by using the highest version of the guava library.


Update Application

  1. Open the app/src/main/java/com/gradle/lab/App.java file and replace the contents after the package declaration with the following:
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.javanet.NetHttpTransport;

import java.io.IOException;

public class App {
    public String getGreeting() {
        return "Hello World!";
    }

    public String getUrl() {
        // This is a small website and easily prints.
        return "https://wiby.me/";
    }

    public static void main(String[] args) throws IOException {
        App app = new App();
        System.out.println(app.getGreeting());

        HttpRequestFactory requestFactory = new NetHttpTransport().createRequestFactory();
        HttpRequest request = requestFactory.buildGetRequest(new GenericUrl(app.getUrl()));
        String rawResponse = request.execute().parseAsString();
        System.out.println("\n---------\n");
        System.out.println(rawResponse);
    }
}
  1. Then open the terminal and execute ./gradlew :app:run, notice the website contents are also printed.


Solution Reference

If you get stuck you can refer to the project files in this repository.

About

Hands-on exercise for DPE University

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages