Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/gradle/dpeuni-gradle-intro-devs-deps
- Owner: gradle
- License: other
- Created: 2024-05-17T08:28:24.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2024-05-17T08:57:42.000Z (6 months ago)
- Last Synced: 2024-10-08T11:34:30.350Z (about 1 month ago)
- Language: Java
- Size: 53.7 KB
- Stars: 1
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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 Project1. 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 DependenciesLet'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` version4. 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 Resolution1. 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 Application1. 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 ReferenceIf you get stuck you can refer to the project files in this repository.