Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse build.gradle programmatically

Tags:

java

gradle

I am creating an automation solution that needs to parse and read a Gradle build, including build.gradle, settings.gradle, and gradle.properties, and any submodules. I know there is an API that include the Project class, which seems to be what I want. The problem is that it is not obvious how to get an instance of a Project class.

Where in the API is the code to parse the build and return a Project instance?

like image 233
Tim Dalsing Avatar asked Aug 16 '19 15:08

Tim Dalsing


1 Answers

You'll have to use Gradle's Tooling API. The entry point is GradleConnector class:

try(
    ProjectConnection connection = GradleConnector.newConnector()
        .forProjectDirectory(new File("/path/to/project"))
        .connect()
) {
    GradleProject project = connection.getModel(GradleProject.class);

    // Do some things with the project
    // project.getTasks().forEach(task -> { ... });
}
like image 145
madhead - StandWithUkraine Avatar answered Sep 22 '22 02:09

madhead - StandWithUkraine