如何自动添加gradle依赖项

我想知道是否有任何gradle任务或任何其他将添加依赖性的 util,例如

dependencies { ... // so far dependencies compile 'com.new.dependency:xy" } 

build.gradle文件中,而不是手动键入它。

我正在尝试编写IntelliJ插件,它会自动将库添加到任何项目中。 到目前为止,我需要分析/解析文档内容,这很乏味。

例如,当您转到IntelliJ中的项目设置并添加库时,代码会自动添加。

您可以将集合指定为依赖项,例如:

 dependencies { compile ['org.slf4j:slf4j-api:1.7.5','org.apache.commons:commons-lang3:3.1'] } 

这意味着您可以使用任何可以生成列表的内容。 如果您的插件维护了如下文件:

compile.dependencies:

 org.slf4j:slf4j-api:1.7.5 org.apache.commons:commons-lang3:3.1 

然后,您可以将依赖项包含到项目中,如:

 dependencies { compile file('compile.dependencies').readLines() } 

您的插件用户必须知道将这些行添加到他们的build.gradle中。 或者,更好的是,您可以将配置捆绑到包含文件中,例如:

 subprojects() { dependencies { if (file('compile.dependencies').exists()) { compile file('compile.dependencies').readLines() } if (file('runtime.dependencies').exists()) { runtime file('runtime.dependencies').readLines() } } } 

然后,您的用户只需使用“apply from:”来包含配置。

没有这样的工具,也没有实用性。 可以使用工具API向项目添加依赖项,但引入的更改不会序列化为配置(build.gradle)文件。