Gradle ext 和 def的区别

作者: 赵阳_c149 | 来源:发表于2019-05-19 02:42 被阅读0次

不太明白gradle中ext和def的区别,stackoverflow【1】中有一段话解释了他们取得区别:

What is the difference between using ext.varname and def varname. E.g. the following code seems to work the same:

使用ext.varname和使用def varname有什么区别呢,他们的作用似乎是一样的。

task copyLicenses {
    def outDir = project.buildDir.absolutePath + '/reports/license/'

    doLast {
        copy {
            from 'licenses'
            into outDir
            include '*'
        }

seems to work exactly the same as

task copyLicenses {
    ext.outDir = project.buildDir.absolutePath + '/reports/license/'

    doLast {
        copy {
            from 'licenses'
            into outDir
            include '*'
        }

Keyword def comes from Groovy and means that variable has local scope.
Using ext.outDir means that you add property outDir to ExtraPropertiesExtension, think of it like project has a map of properties with name ext, and you put your property in this map for later access.

关键字def来自于Groovy,定义了一个local scope的变量。而ext.outDir是新加了一个property:outDir。可以想像成project有一个properties的map,这个map里面有一个键值ext,你把你的property放到这个map里面以便之后引用。
【1】gradle-def-vs-ext

相关文章

网友评论

    本文标题:Gradle ext 和 def的区别

    本文链接:https://www.haomeiwen.com/subject/wndxzqtx.html