简体   繁体   English

如何在Groovy中以编程方式更改Groovy / Gradle文件?

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

My goal is to alter an Android build.gradle file programatically. 我的目标是以编程方式更改Android build.gradle文件。 I want to add a plugin on a specific line, and then a dependency in a specific dependency -section. 我想在特定行上添加一个插件,然后在特定dependency -section中添加一个dependency

My question is does Groovy contain a parser for its own language? 我的问题是Groovy是否包含自己语言的解析器? How can I easily modify a build.gradle-file (or any Groovy file) programatically? 我如何轻松地以编程方式修改build.gradle文件(或任何Groovy文件)?

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. 有一些语法分析器,例如AST语法分析器或config slurper中的内部版本,但是没有简单的方法可以返回代码。 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. Grails插件经常有相同的问题。 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). 解决方案是使用正则表达式来更改代码或完全避免更改代码(添加其他配置而不是更改原始配置)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM