10

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?

madhead
  • 31,729
  • 16
  • 153
  • 201
Tim Dalsing
  • 159
  • 2
  • 7
  • 2
    I just upvoted this... I need the same thing. Gradle's build.gradle obviously replaces XML files in the case of Maven or Ant. It seems to me that the trouble is that a DSL configuration file, unlike an XML file, is dynamic, as it sits on top of an object (of class implementing `Project` in the case of Gradle) and helps execute the latter. Nevertheless it should be possible to extract some info. Just posted a similar question: https://stackoverflow.com/questions/59514521/extract-info-from-a-groovy-dsl-file – mike rodent Dec 28 '19 at 20:19

1 Answers1

5

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 -> { ... });
}
madhead
  • 31,729
  • 16
  • 153
  • 201
  • 3
    This is close but not quite what I was looking for. I want to inspect the build, especially but not limited to dependencies. The Project class (https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html) is what I'm hoping to get an instance of. The GradleProject appears to be mostly used for executing a build. – Tim Dalsing Aug 16 '19 at 18:10