简体   繁体   English

Gradle插件扩展设置Map类型的属性

[英]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. Gradle告诉我无法设置属性设置。 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 但是然后我不知道如何处理闭包以及如何访问内部内容,因此我宁愿使用Map而不是闭包

Regards Mathias 关于Mathias

Ok, you just have to write the map properly :/ 好的,您只需要正确编写地图即可:/

envVars = [test: 'test']

and everything is fine 一切都很好

I am using the following to read a map of values from build.gradle 我正在使用以下内容从build.gradle读取值映射

reference: https://github.com/liquibase/liquibase-gradle-plugin 参考: 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: 示例build.gradle:

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

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

相关问题 无法为 org.springframework.boot.gradle.dsl.SpringBootExtension 类型的扩展“springBoot”设置未知属性“mainClassName” - Could not set unknown property 'mainClassName' for extension 'springBoot' of type org.springframework.boot.gradle.dsl.SpringBootExtension 使用Java定义Gradle插件属性扩展的正确方法? - Correct way to define a Gradle plugin property extension with Java? Gradle 设置插件扩展 - Gradle Settings plugin extension Gradle 在自定义任务中设置属性 - Gradle set property in custom task Gradle应用程序插件:修改workingDir属性 - Gradle application plugin: Modify workingDir property 无法为 org.springframework.boot.gradle.dsl.SpringBootExtension 类型的对象设置未知属性“mainClass” - Could not set unknown property 'mainClass' for object of type org.springframework.boot.gradle.dsl.SpringBootExtension 无法为类型为 org.gradle.process.internal.DefaultJavaExecAction_Decorated 的 object 设置未知属性“args” - Could not set unknown property 'args' for object of type org.gradle.process.internal.DefaultJavaExecAction_Decorated Gradle:无法创建AppPlugin类型的插件 - Gradle: Could not create plugin of type AppPlugin 仅设置如果依赖于Gradle插件任务 - Set onlyIf dependency on Gradle plugin task 如何为 FreeFair AspectJ Gradle 插件设置“-aspectpath”? - How to set "-aspectpath" for the FreeFair AspectJ Gradle Plugin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM