Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/gradle/dpeuni-gradle-intro-devs-deps

Hands-on exercise for DPE University
https://github.com/gradle/dpeuni-gradle-intro-devs-deps

Last synced: 14 days ago
JSON representation

Hands-on exercise for DPE University

Awesome Lists containing this project

README

        

# 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](https://dpeuniversity.gradle.com/app/courses/012de84f-fcd3-45d4-9c4c-284382eb3f3f)
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.



2. 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](https://github.com/googleapis/google-http-java-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/](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



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

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

```toml
google-http-client = "com.google.http-client:google-http-client:1.44.1"
```

8. If using IntelliJ, hit the Gradle refresh icon



9. 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)
```

10. 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.



2. 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:

```java
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);
}
}
```

2. 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.