不太明白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.
Usingext.outDir
means that you add propertyoutDir
to ExtraPropertiesExtension, think of it like project has a map of properties with nameext
, 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
网友评论