Mathias Maerker
Mathias Maerker

Reputation: 530

Gradle plugin extension set a property of type Map

I want to set a Map of attributes to my plugin extension. So basically I want to write something like

settings {
  envVars = {
    a = "abc"
    b = "dec"
    ...
    n = "sdf"
  }
}

When I use an attribute in my Extension class

private Map<?,?> envVars;

Gradle tells me that it can not set the property settings. So what I would like to achieve is to set a map of values in my extension class.

What I did achieve is to get the closure when i write the following:

settings {
  envVars {
    a = "abc"
    b = "dec"
    ...
    n = "sdf"
  }
}

public class extension {
....
    public envVars(Closure c){}
}

But then I have no clue what to do with the closure and how to access what is inside, so I would rather have a Map instead of the closure

Regards Mathias

Upvotes: 4

Views: 4135

Answers (2)

javid khan
javid khan

Reputation: 21

I am using the following to read a map of values from build.gradle

reference: https://github.com/liquibase/liquibase-gradle-plugin

Container class:

class Database {
  def name
  def arguments = [logLevel: 'info']

  Database(String name) {
    this.name = name
  }

Extension Class:

class MyExtension {
  final NamedDomainObjectContainer<Database> databases
  def databases(Closure closure){
    databases.configure(closure)
  }

  def methodMissing(String name, args) {
    arguments[name] = args[0]
  }
}

Load Extensions

  def databases = project.container(Database) { name ->
      new Database(name)
    }
  project.configure(project) {
      extensions.create("extensionName", MyExtension, databases)
  }

Sample build.gradle:

dbDiff{
  databases{
    db1{
      url  'testUrl'
    }
   }
 }

Upvotes: 2

Mathias Maerker
Mathias Maerker

Reputation: 530

Ok, you just have to write the map properly :/

envVars = [test: 'test']

and everything is fine

Upvotes: 6

Related Questions