简体   繁体   English

如何自动添加gradle依赖项

[英]How to add gradle dependency automatically

I'm wondering if there is any gradle task or any other util that will add dependency so eg 我想知道是否有任何gradle任务或任何其他util将添加依赖项 ,例如

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

in your build.gradle file instead you manually type it. 可以build.gradle文件中手动键入它。

I'm trying to write IntelliJ plugin that will automatically add a library to any project. 我正在尝试编写IntelliJ插件,该插件会自动将库添加到任何项目。 So far I need to analyze/parse document content and it's tedious. 到目前为止,我需要分析/解析文档内容,这很繁琐。

For example when you go to project setting in IntelliJ and add library the code is automatically added. 例如,当您进入IntelliJ中的项目设置并添加库时,代码将自动添加。

You can specify collections as your dependencies, like: 您可以将集合指定为依赖项,例如:

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

Which means you can use anything that can produce a list. 这意味着您可以使用任何可以产生列表的东西。 If your plugin maintained a file like: 如果您的插件维护如下文件:

compile.dependencies: compile.dependencies:

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

Then you could include the dependencies into the project like: 然后,您可以将依赖项包含到项目中,例如:

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

Users of your plugin would have to know to add these lines to their build.gradle. 您插件的用户必须知道将这些行添加到他们的build.gradle中。 Or, better, you could bundle the configuration into an include file, like: 或者,更好的是,您可以将配置捆绑到一个包含文件中,例如:

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

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

Then your users would only have to use "apply from:" to include the config. 然后,您的用户只需使用“ apply from:”(应用来自:)即可包含配置。

There's no such tool, nor utility. 没有这样的工具,也没有实用程序。 There's a possibility to add dependencies to project using tooling API but the changes introduced will not be serialized to configuration (build.gradle) file. 可以使用工具API向项目添加依赖项,但是引入的更改不会序列化到配置(build.gradle)文件中。

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

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