将对象实例添加到gradle插件扩展 [英] Adding object instance to gradle plugin extension

查看:122
本文介绍了将对象实例添加到gradle插件扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些像下面这样的插件,我有一个外部命名空间,其中有一个对象加上另一个集合的一个具体实例( mother children )。

  family {
mother {
firstname ='John'
lastname ='Cleese'
}
children {
son {
firstName ='John'
lastName ='Cleese'
}
女儿{
firstName ='Jane'
lastName ='Cleese'
}
}
}

我可以添加集合对象并根据各种示例读取变量看到但不知道如何添加具体实例。

如何在扩展对象上定义它?



显示问题的代码 - 我想将 mother 作为一个单独的实例添加到pl中

import org.gradle.api.DefaultTask
import org.gradle。 api.tasks.TaskAction
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api。*

class Person
{
final String名称
字符串firstName
字符串lastName
Person(字符串名称){this.name = name}
}

类FamilyExtension {

final NamedDomainObjectContainer< Person>儿童
人母亲
人父亲

FamilyExtension(儿童){
this.children =儿童
}

def儿童(闭包关闭){
children.configure(闭包)
}
}

类FamilyPlugin实现插件< Project> {

void apply(Project project){
project.task('sayHello',type:DefaultTask)<< {
println(
Hello $ {project.family.children [son]。firstName}+
and hello $ {project.family.children [daughter]]。 firstName})
}

def children = project.container(Person)

project.extensions.create(family,FamilyExtension,children)




 应用插件:FamilyPlugin 

family {
//如何添加对此的支持?
// mother {
// firstname ='John'
// lastname ='Cleese'
//}
children {
son {
firstName ='John'
lastName ='Cleese'
}
女儿{
firstName ='Jane'
lastName ='Cleese'
}
}
}


解决方案

那么,为 family 添加 mother 闭包,将以下代码添加到 FamilyExtension class

Person mother(Closure closure){
mother = new Person()
project.configure(mother,closure)
return mother
}

如果您希望在家庭成员中有 children 扩展名,您可以将以下代码添加到 FamilyPlugin中。应用方法

  project.extensions.create(family,FamilyExtension,instantiator,project)
project.family.extensions.create(children,FamilyExtension.Children,project)

然后添加

  void son(Closure closure){
Person son = new Person()
project.configure(son,closure)
children.add (儿子)
}

可以添加儿子子元素中添加一个。



查看整个项目并解释更多细节请访问Github项目 https://github.com/andriipanasiuk/family-gradle-plugin


I have something like the plugin below where I have one outer namespace and within it there is a single 'concrete' instance (mother) of an object plus another collection (children).

family {
  mother {
      firstname = 'John'
      lastname  = 'Cleese'
  }
  children {
      son {
        firstName = 'John'
        lastName  = 'Cleese'
      }
      daughter {
        firstName = 'Jane'
        lastName  = 'Cleese'
      }
  }
}

I am able to add the collection object and read the variables based on various examples I've seen but not sure how I add the concrete instance in addition.
How do I define it on the extension object?

Code which shows the issue - I would like to add mother as a single instance withing the plugin.

import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.*

class Person
{
  final String name
  String firstName
  String lastName
  Person(String name) { this.name = name }
}

class FamilyExtension {

  final NamedDomainObjectContainer<Person> children
  Person mother
  Person father

  FamilyExtension(children) {
    this.children = children
  }

  def children(Closure closure) {
    children.configure(closure)
  }
}

class FamilyPlugin implements Plugin<Project> {

  void apply(Project project) {
    project.task('sayHello', type: DefaultTask) << { 
      println(
        "Hello ${project.family.children["son"].firstName} " +
        "and hello ${project.family.children["daughter"].firstName} ")
    }

    def children  = project.container(Person)

    project.extensions.create("family", FamilyExtension, children)
  }
}

apply plugin: FamilyPlugin

family {
  // How do I add support for this?
  // mother {
  //     firstname = 'John'
  //     lastname  = 'Cleese'
  // }
  children {
      son {
        firstName = 'John'
        lastName  = 'Cleese'
      }
      daughter {
        firstName = 'Jane'
        lastName  = 'Cleese'
      }
  }
}

解决方案

Well, to add mother closure to the family you add the following code to the FamilyExtension class

Person mother(Closure closure) {
   mother = new Person()
   project.configure(mother, closure)
   return mother
}

If, at the same time you want to have children extension inside family one you add the following code into the FamilyPlugin.apply method

project.extensions.create("family", FamilyExtension, instantiator, project)
project.family.extensions.create("children", FamilyExtension.Children, project)

And then you add

void son(Closure closure) {
    Person son = new Person()
    project.configure(son, closure)
    children.add(son)
}

to be able to add son closure inside the children one.

To see the whole project and explanation with more details please visit Github project https://github.com/andriipanasiuk/family-gradle-plugin

这篇关于将对象实例添加到gradle插件扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆