Created
May 10, 2022 21:28
-
-
Save jmini/42930d8f3bc1228718b1313513378d08 to your computer and use it in GitHub Desktop.
Use the Gradle tooling API in a Jbang script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: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:This is just one example and the script can be modified.
See also: