如何以编程方式设置gradle依赖项

时间:2017-04-27 18:05:39

标签: grails gradle groovy dependency-management

如何从文件中以编程方式设置gradle依赖项,即 我在build.gradle中有这个

dependencies {
  compile "org.springframework.boot:spring-boot-starter-logging"
  compile "org.springframework.boot:spring-boot-autoconfigure"
  compile "org.grails:grails-core"
  compile "org.springframework.boot:spring-boot-starter-actuator"
  compile "org.springframework.boot:spring-boot-starter-tomcat"
  compile "org.grails:grails-dependencies"
  ...
}

我希望从文件中添加更多依赖项,如下所示

[
  {env: "runtime", lib : "com.h2database:h2"},
  {env: "runtime", lib : "mysql:mysql-connector-java:5.1.29"}
 ]

有人可以帮助我,我是一个有格斗的初学者,我需要一个任务来完成它,但我不知道怎么做

2 个答案:

答案 0 :(得分:3)

你可以这样做:

dependencies {

    def configFile = file('db.config.json');
    def json = new groovy.json.JsonSlurper().parseText(configFile.text)
    json.each {
      "$it.env"(it.lib)
    }

    compile "org.springframework.boot:spring-boot-starter-logging"
    compile "org.springframework.boot:spring-boot-autoconfigure"

    ...
}

答案 1 :(得分:1)

Configuration config = project.configurations.getByName('compile') 
Dependency dep = project.dependencies.create('foo:bar:1.0')
config.add(foo)