From d4b68995d19d71faa231e97870d0d83b1b15deb3 Mon Sep 17 00:00:00 2001 From: David Groomes Date: Sat, 29 Jul 2023 15:49:02 -0500 Subject: [PATCH] [`scratch`] Try out the Gradle Tooling API --- README.md | 7 ++++++- scratch/build.gradle.kts | 2 ++ scratch/gradle/libs.versions.toml | 6 ++++-- scratch/src/main/java/dgroomes/Main.java | 18 ++++++++++++++---- .../src/main/resources/simplelogger.properties | 5 +++++ scratch/src/test/java/dgroomes/MainTest.java | 2 +- 6 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 scratch/src/main/resources/simplelogger.properties diff --git a/README.md b/README.md index e88170d..b8ad579 100644 --- a/README.md +++ b/README.md @@ -87,5 +87,10 @@ General clean-ups, changes and things I wish to implement for this project: subproject to the 'assembleAll' Gradle task. * [ ] Incorporate Gradle's [test fixtures](https://docs.gradle.org/current/userguide/java_testing.html#sec:java_test_fixtures) feature in one of the subprojects -* [ ] Consider exploring Gradle's programmatic API for running Gradle. I don't know if I'll ever have a use-case for this +* [x] DONE (I implemented something in `scratch`. I couldn't figure out the architecture though. I think it creates a new classloader and loads the jars from the download Gradle distros) Consider exploring [Gradle's Tooling API](https://docs.gradle.org/current/userguide/third_party_integration.html#embedding) for programmatic access to Gradle. I don't know if I'll ever have a use-case for this, but I won't know until I learn it. + * This note caught my eye. How does this work? + * > A fundamental characteristic of the Tooling API is that it operates in a version independent way. This means that + > you can use the same API to work with builds that use different versions of Gradle, including versions that are + > newer or older than the version of the Tooling API that you are using. The Tooling API is Gradle wrapper aware + > and, by default, uses the same Gradle version as that used by the wrapper-powered build. diff --git a/scratch/build.gradle.kts b/scratch/build.gradle.kts index 9f31974..fcad14f 100644 --- a/scratch/build.gradle.kts +++ b/scratch/build.gradle.kts @@ -4,11 +4,13 @@ plugins { repositories { mavenCentral() + maven { url = uri("https://repo.gradle.org/gradle/libs-releases") } } dependencies { implementation(libs.slf4j.api) runtimeOnly(libs.slf4j.simple) + implementation(libs.gradle.tooling.api) testImplementation(libs.junit.jupiter.api) testRuntimeOnly(libs.junit.jupiter.engine) diff --git a/scratch/gradle/libs.versions.toml b/scratch/gradle/libs.versions.toml index aff4544..176a40d 100644 --- a/scratch/gradle/libs.versions.toml +++ b/scratch/gradle/libs.versions.toml @@ -1,11 +1,13 @@ [versions] -# SLF4J releases: http://www.slf4j.org/news.html -slf4j = "2.0.7" +slf4j = "2.0.7" # SLF4J releases: http://www.slf4j.org/news.html junit = "5.10.0" # JUnit releases: https://junit.org/junit5/docs/current/release-notes/index.html +gradle = "8.2.1" # Gradle releases: https://gradle.org/releases + [libraries] slf4j-api = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" } slf4j-simple = { module = "org.slf4j:slf4j-simple", version.ref = "slf4j" } junit-jupiter-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "junit" } junit-jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "junit" } +gradle-tooling-api = { module = "org.gradle:gradle-tooling-api", version.ref = "gradle" } diff --git a/scratch/src/main/java/dgroomes/Main.java b/scratch/src/main/java/dgroomes/Main.java index 53c436c..af5d489 100644 --- a/scratch/src/main/java/dgroomes/Main.java +++ b/scratch/src/main/java/dgroomes/Main.java @@ -1,17 +1,27 @@ package dgroomes; +import org.gradle.tooling.GradleConnector; +import org.gradle.tooling.ProjectConnection; +import org.gradle.tooling.model.build.BuildEnvironment; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.File; + public class Main { private static final Logger log = LoggerFactory.getLogger(Main.class); public static void main(String[] args) { - log.info(message()); - } + var externalProjectDir = new File("/Users/dave/repos/personal/arrow-playground/basic"); + log.info("Let's inspect the Gradle 'build environment' for the project in the directory: {}", externalProjectDir); + var gradleConnector = GradleConnector.newConnector().forProjectDirectory(externalProjectDir); - public static String message() { - return "Hello world!"; + try (ProjectConnection connection = gradleConnector.connect()) { + var buildEnvironment = connection.getModel(BuildEnvironment.class); + log.info("The Gradle Tooling API found this 'build environment' information:"); + log.info("Gradle version: {}", buildEnvironment.getGradle().getGradleVersion()); + log.info("Gradle home: {}", buildEnvironment.getGradle().getGradleUserHome()); + } } } diff --git a/scratch/src/main/resources/simplelogger.properties b/scratch/src/main/resources/simplelogger.properties new file mode 100644 index 0000000..35f7e42 --- /dev/null +++ b/scratch/src/main/resources/simplelogger.properties @@ -0,0 +1,5 @@ +# Configuring the 'slf4j-simple' logger +org.slf4j.simpleLogger.showDateTime=TRUE +org.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss.SSS +org.slf4j.simpleLogger.log.dgroomes=info +org.slf4j.simpleLogger.log.org.gradle=info diff --git a/scratch/src/test/java/dgroomes/MainTest.java b/scratch/src/test/java/dgroomes/MainTest.java index b5cf3a3..a2526e6 100644 --- a/scratch/src/test/java/dgroomes/MainTest.java +++ b/scratch/src/test/java/dgroomes/MainTest.java @@ -8,6 +8,6 @@ class MainTest { @Test void message() { - assertEquals("Hello world!", Main.message()); + assertEquals("Hello world!", "Hello world!"); } }