I need to implement some logic (to integrate in a CI-CD pipeline) that, when building a Gradle project, extracts information about the project. The data to extract is the following:
It must work both on simple and multi module project, collecting both dependencies declared in “dependencies” and dependencies declared in the “versionCatalogUpdate”.
Using the Tooling API for Java, I’m able to extract the project’s name, group, and version, but I don’t understand how to extract the dependencies list. Please consider that I’m new to Gradle.
As you only want the “declared” dependencies you might need to build an own tooling api model that you inject as plugin using an init script when connecting with the tooling api that collects the data you want to have and that you can then request from the tooling api.
Probably not a too beginner-level task.
No idea what you mean with versionCatalogUpdate, that is nothing that exists in vanilla Gradle, it is probably from some plugin you apply or something you defined yourself.
With your own tooling api model you can probably also extract that information hopefully.
“you might need to build an own tooling api model that you inject as plugin using an init script when connecting with the tooling api”
Is there any documentation I can read to deepen what you have suggested?
In the meantime, I have found a way to get the data I need by using the “Idea Model” provided in the tooling API:
IdeaProject project = connection.getModel(IdeaProject.class);
for (IdeaModule module : project.getModules()) {
for (IdeaDependency dep : module.getDependencies()) {
if (dep instanceof IdeaSingleEntryLibraryDependency libDep) {
IdeaDependencyScope depScope = dep.getScope();
GradleModuleVersion modVer = libDep.getGradleModuleVersion();
if (modVer != null) {
log.info("Found Gradle dependency: {}", modVer.getGroup() + ":" + modVer.getName() + ":"+modVer.getVersion());
}
}
}
}
Anyway, this solution has two drawbacks:
It forces the download of the dependencies jar, introducing potential performance issues. I’m looking forward to a solution that does not imply downloads.
It returns all dependencies, including the transitives.
It seems that, using the Idea model, there is no way to avoid the jar downloads and to discriminate between declared and transitive dependencies.