Parse and write gradle project files

I’d like to develop a tool to generate gradle projects to bootstrap application development (similar to Spring Initializr). But then also to maintain these over time, to incrementally add new dependencies or tasks to an existing project. Ideally in Java, but not necessarily.

How do I parse, edit and write a gradle project?

Gradle build scripts are either written in Groovy or in Kotlin (I greatly prefer the latter), so any tool that lets you parse and generate those could do.