How to get the build definition Project

I can extract java compile options for a project using something like project.getTasks().getByName("JavaCompile"). I can also query out the sourcesets, dependency jars, compilation task name etc. relatively easily via the Project instance.

Is there a way to extract this information for the build definition itself? Some kind of meta Project instance that has a sourceset that points to all the build.gradle(.kts) files. That lists all the Gradle jars required to compile the build.gradle files. That tells me what Groovy/Kotlin compiler options are used and also what is the task name for compiling the build definition.

I can edit the build.gradle.kts files in Intellij and it will navigate to the gradle-api-8.6.jar if I click on the Task class so I assume this information is available somewhere.

The only things I’ve been able to find are the Project#getBuildFile method to point to the relevant build.gradle file and the Project#getBuildscript method but that returns empty lists for ScriptHandler#getDependencies.

There is no task that does this and probably no way to get that information from within a build.
If you really need that information for something, you probably need to use the tooling API like IDEs are doing to get the relevant information.

Btw. be aware that things like tasks.getByName are a bad idea, as they completely break task configuration avoidance for all tasks.

Hi Björn

Thanks for the info.

The tasks#byName stuff was just an example. I’m actually using the tooling API ModelBuilder stuff but can’t find it there. I’ll trawl through the intellij source to see how they do it.

1 Like

Thanks.

I’m using the Tooling API but haven’t been able to find out how to get anything beyond the locations of the build.gradle(.kts) files themselves.

What gradle jars, classpaths or compiler options Gradle uses to compile the build definition doesn’t seem to be available.