Fabricio Maia
Fabricio Maia

Reputation: 13

How can I set gradle dependencies programatically

How can I set gradle dependencies programatically from a file i.e I have this in my 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"
  ...
}

and I would like to add more dependencies from a file like as below

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

Could someone help me, I'm a beginner with gradle and I need a task to do it, but I don't no how to do

Upvotes: 1

Views: 1175

Answers (2)

Luan James
Luan James

Reputation: 221

You could do this:

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"

    ...
}

Upvotes: 4

lance-java
lance-java

Reputation: 27994

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

Upvotes: 5

Related Questions