Skip to content

Instantly share code, notes, and snippets.

@jmini
Created May 10, 2022 21:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmini/42930d8f3bc1228718b1313513378d08 to your computer and use it in GitHub Desktop.
Save jmini/42930d8f3bc1228718b1313513378d08 to your computer and use it in GitHub Desktop.
Use the Gradle tooling API in a Jbang script
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS org.gradle:gradle-tooling-api:7.4.2
//DEPS org.slf4j:slf4j-simple:1.7.10
//REPOS gradle-releases=https://repo.gradle.org/gradle/libs-releases
import java.io.File;
import org.gradle.tooling.GradleConnector;
import org.gradle.tooling.ProjectConnection;
import org.gradle.tooling.model.GradleProject;
public class listProjectDirectories {
public static void main(String... args) {
if (args == null || args.length != 1) {
System.err.println("Usage: " + listProjectDirectories.class.getSimpleName() + " <root gradle project path>");
System.exit(1);
throw new IllegalStateException("Unreachable code");
}
GradleConnector connector = GradleConnector.newConnector();
connector.forProjectDirectory(new File(args[0])); // project dir
ProjectConnection connection = connector.connect();
try {
GradleProject project = connection.getModel(GradleProject.class);
printProjectDirectory(project);
} finally {
connection.close();
}
}
private static void printProjectDirectory(GradleProject project) {
System.out.println(project.getProjectDirectory().getAbsolutePath());
for (GradleProject child : project.getChildren()) {
printProjectDirectory(child);
}
}
}
@jmini
Copy link
Author

jmini commented May 10, 2022

Script can be executed using jbang.

Example:

In a folder /home/work/git/openapi-style-validator where you have a local clone of following repository: OpenAPITools/openapi-style-validator.

If you run ./gradlew projects you get this:

> Task :projects

------------------------------------------------------------
Root project 'openapi-style-validator'
------------------------------------------------------------

Root project 'openapi-style-validator'
+--- Project ':openapi-style-validator-cli'
+--- Project ':openapi-style-validator-gradle-plugin'
+--- Project ':openapi-style-validator-lib'
\--- Project ':openapi-style-validator-maven-plugin'

To see a list of the tasks of a project, run gradlew <project-path>:tasks
For example, try running gradlew :openapi-style-validator-cli:tasks

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.2/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 1s

But this doesn't tell you where the project directory of each project is located and it might be some work to parse the output of this command.

What you can do is to run the jbang script presented on this page and run: jbang listProjectDirectories.java /home/work/git/openapi-style-validator. This gives you the exact location of each gradle project:

/home/work/git/openapi-style-validator
/home/work/git/openapi-style-validator/cli
/home/work/git/openapi-style-validator/gradle-plugin
/home/work/git/openapi-style-validator/lib
/home/work/git/openapi-style-validator/maven-plugin

This is just one example and the script can be modified.

See also:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment