简体   繁体   中英

How do I programatically alter a Groovy/Gradle file in Groovy?

My goal is to alter an Android build.gradle file programatically. I want to add a plugin on a specific line, and then a dependency in a specific dependency -section.

My question is does Groovy contain a parser for its own language? How can I easily modify a build.gradle-file (or any Groovy file) programatically?

AfaIk, programatically altering a groovy file is not easy. There are parsers like the build in AST parser or config slurper, but there is no easy way back to code. Aand all those parsers throw not only the formatting, but also the comments away. The resulting source wouldn't be what you expect.

Grails plugins often have the same problem. The solution is to use regular expressions in order to change the code or to avoid changing the code at all (adding an additional config instead of changing the original).

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