Skip to content

Commit

Permalink
[scratch] Try out the Gradle Tooling API
Browse files Browse the repository at this point in the history
  • Loading branch information
dgroomes committed Jul 29, 2023
1 parent 08eea67 commit d4b6899
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 8 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 2 additions & 0 deletions scratch/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions scratch/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -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" }
18 changes: 14 additions & 4 deletions scratch/src/main/java/dgroomes/Main.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
5 changes: 5 additions & 0 deletions scratch/src/main/resources/simplelogger.properties
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion scratch/src/test/java/dgroomes/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class MainTest {

@Test
void message() {
assertEquals("Hello world!", Main.message());
assertEquals("Hello world!", "Hello world!");
}
}

0 comments on commit d4b6899

Please sign in to comment.