Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 683
Mathias Maerker Avatar asked Oct 26 '25 05:10

Mathias Maerker


2 Answers

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

envVars = [test: 'test']

and everything is fine

like image 189
Mathias Maerker Avatar answered Oct 28 '25 19:10

Mathias Maerker


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'
    }
   }
 }
like image 20
javid khan Avatar answered Oct 28 '25 20:10

javid khan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!