Gradle学习笔记(二)构建脚本

所有的gradle脚本都会实现script接口,所有脚本都可以使用script接口的属性和方法;通常每一个gradle脚本对象都会有一个委托对象,构建脚本对应的是project对象,任何不是脚本中定义的属性和方法都可以 到对应的委托对象找到。
构建脚本也是groovy脚本,因此也可以在构建脚本中进行方法和类的定义,build脚本其实也是使用Gradle APi的Groovy代码,脚本中的script block对应着project对象的方法。

Don’t forget that your build script is simply Groovy code that drives the Gradle API. And the Project interface is your starting point for accessing everything in the Gradle API. So, if you’re wondering what ‘tags’ are available in your build script, you can start with the documentation for the Project interface
构建脚本基本结构
Build script blocks
Gradle API project
project基本构成包括属性,方法,task,plugin,dependencies,artifects。从语言层面project包含一些列的属性和方法供我们在编写脚本是使用;从构建构成上来说project是一系列task的集合,每个task完成一项基本工作,比如编译(compile),归档;从构建功能上来说,构建要有输入和输出即dependencies and artifacts。
属性与方法
project对象有5个属性域,构建时会在这五个域查找脚本使用的属性
1.project对象的成员变量
2.extra properties 每一个project都会维护一组键值对属性,所有的extra properties的定义都要使用ext关键字,一旦定义过之后就可以直接使用该属性从属的对象获取属性值,如:

project.ext.prop1 = "foo"
task doStuff {
    ext.prop2 = "bar"
}
subprojects { ext.${prop3} = false }

3.由plugin添加到project的extensions。ExtensionAware接口使gradle对象可以在运行时使用其他对象(Extensions)扩展自身。project对象实现了ExtensionAware接口。Extensions是普通的实体类,创建时会指定名称,在被扩展对象中可以使用象属性一样用名称获取,,或是用命名空间如下:

 // Extensions are just plain objects, there is no interface/type
 class MyExtension {
   String foo
   MyExtension(String foo) {
     this.foo = foo
   }
 }
 // Add new extensions via the extension container
 project.extensions.create('custom', MyExtension, "bar")
 //                       («name»,   «type»,       «constructor args», …)
 // extensions appear as properties on the target object by the given name
 assert project.custom instanceof MyExtension
 assert project.custom.foo == "bar"
 // also via a namespace method
 project.custom {
   assert foo == "bar"
   foo = "other"
 }
 assert project.custom.foo == "other"

 // Extensions added with the extension container's create method are themselves extensible
 assert project.custom instanceof ExtensionAware
 project.custom.extensions.create("nested", MyExtension, "baz")
 assert project.custom.nested.foo == "baz"

 // All extension aware objects have a special “ext” extension of type ExtraPropertiesExtension
 assert project.hasProperty("myProperty") == false
 project.ext.myProperty = "myValue"

 // Properties added to the “ext” extension are promoted to the owning object
 assert project.myProperty == "myValue"

4.由plugin添加到project的convention(惯例)属性。例如JavaPlugin会向使用插件的project添加JavaPluginConvention,project中可以访问JavaPluginConvention的sourceSets属性。
5.project中的Task。可以使用Task名称作为属性使用,这种属性是只读的。
6.从project的上一级继承来的extra属性和convention属性

同样project的方法有对应的5种方法域:
需要注意的是:
1.project中的Task既有对应的属性又有对应的方法;
2.值是闭包的project属性,闭包可以视为方法。如:allprojects,subprojects,repositories。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值