简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM