【问题标题】:ext and code block's meaning in the gradle filegradle文件中ext和代码块的含义
【发布时间】:2014-03-08 22:12:11
【问题描述】:
ext {
    springVersion = "3.1.0.RELEASE"
    emailNotification = "build@master.org"
}

以上代码是build.gradle的sn-p

我了解使用 { } 闭包参数调用 ext 方法。 这是正确的? 所以我认为gradle正在访问springVersion和emailNotification。 我将用下面的代码验证我的假设

def ext(data) {
    println data.springVersion
}

ext {
    springVersion = "3.1.0.RELEASE"
    emailNotification = "build@master.org"
}

但运行该代码 发生以下错误。

groovy.lang.MissingPropertyException: No such property: springVersion for class: Test

你具体解释一下ext和code block吗?

【问题讨论】:

  • 你如何调用方法 ext()?你能粘贴完整的 gradle 文件吗?

标签: groovy gradle


【解决方案1】:

extproject.ext 的简写,用于定义project 对象的额外属性。 (也可以为许多其他对象定义额外的属性。)当读取额外的属性时,ext. 被省略(例如println project.springVersionprintln springVersion)。在方法内部也是如此。声明一个名为 ext 的方法是没有意义的。

【讨论】:

  • 嗨,peter。你知道 ext 涉及 groovy 的哪种语法?还是只在年级语法中?@Peter Niederwieser
  • 这是最简洁的答案!
  • 如果有人能指出我在哪里可以找到关于“ext block”使用的文档,tks.
【解决方案2】:

以下是问题中的示例代码产生错误的原因的解释。

在代码中:

ext {
    springVersion = "3.1.0.RELEASE"
    emailNotification = "build@master.org"
}

不将具有 springVersion 和 emailNotification 属性的对象传递给函数“ext”。花括号并不意味着 POJO,而是一个闭包。 这就是“ext”函数抱怨它无法访问属性的原因。

传递这样一个闭包(称为配置闭包)的想法是接收函数将:

  1. 修改闭包的委托属性以指向闭包属性/方法应该作用的对象。

  2. 执行闭包()

因此闭包执行,当它引用方法/属性时,这些将在要配置的对象上执行。

因此,对您的代码进行以下修改将使其工作:

class DataObject {
   String springVersion;
   String emailNotification;
}

def ext(closure) {  
    def data = new DataObject() // This is the object to configure.
    closure.delegate = data;
    // need this resolve strategy for properties or they just get
    // created in the closure instead of being delegated to the object
    // to be configured. For methods you don't need this as the default
    // strategy is usually fine.
    closure.resolveStrategy = Closure.DELEGATE_FIRST 
    closure() // execute the configuration closure
    println data.springVersion
}

ext {
    springVersion = "3.1.0.RELEASE"
    emailNotification = "build@master.org"
}

​ 希望这可以帮助。 Groovy 闭包需要一些时间来适应......

【讨论】:

    【解决方案3】:

    ExtraPropertiesExtension 对 get() 和 set() 的覆盖是使以前从未定义的属性的配置语法起作用的关键。

    class DataObject {
        HashMap<String, Object> props = new HashMap<String, Object>()
    
        Object get(String name) {
            return props.get(name)
        }
    
        void set(String name, @Nullable Object value) {
            props.put(name, value)
        }
    }
    
    def myExtInstance = new DataObject()
    
    def myExt = { Closure closure ->
        def data = myExtInstance
        closure.delegate = data;
        // need this resolve strategy for properties or they just get
        // created in the closure instead of being delegated to the object
        // to be configured. For methods you don't need this as the default
        // strategy is usually fine.
        closure.resolveStrategy = Closure.DELEGATE_FIRST
        closure() // execute the configuration closure
        println data.springVersion
    }
    
    myExt {
        springVersion = "3.1.0.RELEASE"
        emailNotification = "build@master.org"
    }
    
    println "myExtInstance.springVersion" + myExtInstance.springVersion
    

    ExtraPropertiesExtension docs

    【讨论】:

    • 在这个例子中你不需要myExt方法,比如ext也是ExtraPropertiesExtension的实例。
    猜你喜欢
    • 1970-01-01
    • 2017-01-29
    • 2011-04-07
    • 1970-01-01
    • 2021-03-29
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多