Get Gradle Plugins programmatically with Gradle tooling api

656 Views Asked by At

I am using Gradle tooling API to get different insights about Gradle projects such as project tasks, Gradle version, and more.

Part of this analysis requires me to know what plugins were applied (directly and transitively) by the project. I could not find a way to get Project’s plugins from the tooling API. Is there a way to do it?

1

There are 1 best solutions below

1
badsyntax On

The tooling API is limited in how you can query a build and there's no API to list build plugins, but the tooling API allows you to add your own plugins & models which you can use to query the build.

It's a little complex, but you need to add an init script to the build (eg via init.gradle) to provide your custom model & plugin to query the build.

This is a good repo that demonstrates how to do it: https://github.com/melix/gradle-tapi-demo-artifacts

Here's some code from that repo that demonstrates how to list build artifacts with a custom plugin & model:

ModelBuilder<OutgoingArtifactsModel> customModelBuilder = connection.model(OutgoingArtifactsModel.class);
customModelBuilder.withArguments("--init-script", copyInitScript().getAbsolutePath());
OutgoingArtifactsModel model = customModelBuilder.get();
for (File artifact : model.getArtifacts()) {
    System.out.println("artifact = " + artifact);
}

If you want to list plugins you can use the project.getPlugins() API.