Tim Dalsing
Tim Dalsing

Reputation: 189

Parse build.gradle programmatically

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?

Upvotes: 13

Views: 6456

Answers (1)

madhead
madhead

Reputation: 33412

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 -> { ... });
}

Upvotes: 5

Related Questions