Gradle笔记2-构建脚本
来源:互联网 发布:六十甲子单双计算法 编辑:程序博客网 时间:2024/06/01 07:26
1. Every Gradle build consists of three basic building blocks: projects, tasks, and properties.
2. Each build contains at least one project, which in turn contains one or more tasks.
3. 当访问project的属相和方法时,不需要使用变量project.
4. setDescription("myProject") 未显性使用project变量(实际为project.setDescription("myProject"))
println "Description of project $name: " + project.description $name为groovy语法访问方式
5. 添加自定义属性,需要使用ext命名空间
只有在自定义属性初始化声明时必须要用ext命名空间
project.ext.myProp = 'myValue'
ext {
someOtherProp = 123
}
读取和修改自定义属性时,ext命名空间是可选的
assert myProp == 'myValue'
println project.someOtherProp
ext.someOtherProp = 567
6. Gradle属性:在属性文件gradle.properties中声明属性,可以直接注入到project中,属性文件需要放在<USER_HOME>/.gradle下或者项目的根目录下,这些属性可以通过project实例访问。
假设在gradle.properties中声明如下:
exampleProp = myValue
someOtherProp = 455
则在项目中可以如下访问:
assert project.exampleProp == 'myValue'
task printGradleProperty << {
println "Second property: $someOtherProp"
}
7. 给已存在的task添加action
task printVersion { //一个task初始化声明时可以包含一个first和一个last action
doFirst {
println "Before reading the project version"
}
doLast {
println "Version: $version"
}
}
printVersion.doFirst { println "First action" } //插入一个action到action列表头位置
printVersion << { println "Last action" } //追加一个action到action列表尾
8. Gradle提供了一个logger实现,以日志库SLF4J为基础
task printVersion << {
logger.quiet "Version: $version"
}
9. group属性:定义了task所属的一个逻辑组
description属性:对task的目的做一个简短的概括
task printVersion(group: 'versioning', description: 'Prints project version.') << {
logger.quiet "Version: $version"
}
等价于:
task printVersion {
group = 'versioning' //调用setter方法
description = 'Prints project version.'
doLast {
logger.quiet "Version: $version"
}
}
10. task依赖:使用dependsOn方法
task first << { println "first" }
task second << { println "second" }
task printVersion(dependsOn: [second, first]) << {
logger.quiet "Version: $version"
}
task third << { println "third" }
third.dependsOn('printVersion') //当声明依赖时,通过名字引用task
11. finalizer task
使用场景:当依赖于某项资源的任务执行完时,这个资源需要释放
task first << { println "first" }
task second << { println "second" }
first.finalizedBy second //声明一个任务被另一个任务终结
12. groovy语法声明类:类的属性不需要写修饰符,不需要加getter和setter方法,产生字节码时会自动产生
version = new ProjectVersion(0, 1)
class ProjectVersion {
Integer major
Integer minor
Boolean release
ProjectVersion(Integer major, Integer minor) { //构造方法
this.major = major
this.minor = minor
this.release = Boolean.FALSE
}
ProjectVersion(Integer major, Integer minor, Boolean release) {
this(major, minor)
this.release = release
}
@Override
String toString() {
"$major.$minor${release ? '' : '-SNAPSHOT'}"
}
}
13. task configuration:task中未使用action,总是会在所有action执行前就执行
ext.versionFile = file('version.properties') //自定义属性versionFile
task loadVersion {
project.version = readVersion()
}
ProjectVersion readVersion() {
logger.quiet 'Reading the version file.'
if(!versionFile.exists()) {
throw new GradleException("Required version file does not exist: $versionFile.canonicalPath")
}
Properties versionProps = new Properties()
versionFile.withInputStream {
stream -> versionProps.load(stream)
}
//当返回的对象是方法中的最后一条语句时,可以忽略return关键字
new ProjectVersion(versionProps.major.toInteger(), versionProps.minor.toInteger(), versionProps.release.toBoolean())
}
14. gradle构建生命周期:
初始化阶段 -> 配置阶段 -> 执行阶段
配置阶段执行task配置等,执行阶段执行task action
15. Keep in mind that any configuration code is executed with every build of your project—even if you just execute gradle tasks.
16. 项目版本切换为生产版本
task makeReleaseVersion(group: 'versioning', description: 'Makes project a release version.') << {
inputs.property('release', version.release)
outputs.file versionFile
doLast {
version.release = true
ant.propertyfile(file: versionFile) {
entry(key: 'release', type: 'string', operation: '=', value: 'true')
}
}
}
17. 自定义task类
class ReleaseVersionTask extends DefaultTask { //自定义task需要继承DefaultTask
@Input Boolean release //声明自定义task的输入、输出属性
@OutputFile File destFile
ReleaseVersionTask() { //构造方法
group = 'versioning'
description = 'Makes project a release version.'
}
@TaskAction
void start() { //声明方法
project.version.release = true
ant.propertyfile(file: destFile) {
entry(key: 'release', type: 'string', operation: '=', value: 'true')
}
}
}
通过自定义task类构建task:
task makeReleaseVersion(type: ReleaseVersionTask) {
release = version.release //设置自定义task的属性
destFile = versionFile
}
18. 声明一个task rule
tasks.addRule("Pattern: increment<Classifier>Version – Increments the project version classifier.") {
String taskName ->
if(taskName.startsWith('increment') && taskName.endsWith('Version')) {
task(taskName) << { //添加一个task
String classifier = (taskName - 'increment' - 'Version').toLowerCase()
String currentVersion = version.toString()
switch(classifier) {
case 'major': ++version.major
break
case 'minor': ++version.minor
break
default: throw new GradleException("Invalid version type '$classifier. Allowed types: ['Major', 'Minor']")
}
String newVersion = version.toString()
logger.info "Incrementing $classifier project version: $currentVersion -> $newVersion"
ant.propertyfile(file: versionFile) {
entry(key: classifier, type: 'int', operation: '+', value: 1)
}
}
}
}
19. Groovy类结构
.
├── build.gradle
├── buildSrc
│ └── src
│ └── main
│ └── groovy
│ └── com
│ └── manning
│ └── gia
│ ├── ProjectVersion.groovy
│ └── ReleaseVersionTask.groovy
├── src
│ └── ...
└── version.properties
2. Each build contains at least one project, which in turn contains one or more tasks.
3. 当访问project的属相和方法时,不需要使用变量project.
4. setDescription("myProject") 未显性使用project变量(实际为project.setDescription("myProject"))
println "Description of project $name: " + project.description $name为groovy语法访问方式
5. 添加自定义属性,需要使用ext命名空间
只有在自定义属性初始化声明时必须要用ext命名空间
project.ext.myProp = 'myValue'
ext {
someOtherProp = 123
}
读取和修改自定义属性时,ext命名空间是可选的
assert myProp == 'myValue'
println project.someOtherProp
ext.someOtherProp = 567
6. Gradle属性:在属性文件gradle.properties中声明属性,可以直接注入到project中,属性文件需要放在<USER_HOME>/.gradle下或者项目的根目录下,这些属性可以通过project实例访问。
假设在gradle.properties中声明如下:
exampleProp = myValue
someOtherProp = 455
则在项目中可以如下访问:
assert project.exampleProp == 'myValue'
task printGradleProperty << {
println "Second property: $someOtherProp"
}
7. 给已存在的task添加action
task printVersion { //一个task初始化声明时可以包含一个first和一个last action
doFirst {
println "Before reading the project version"
}
doLast {
println "Version: $version"
}
}
printVersion.doFirst { println "First action" } //插入一个action到action列表头位置
printVersion << { println "Last action" } //追加一个action到action列表尾
8. Gradle提供了一个logger实现,以日志库SLF4J为基础
task printVersion << {
logger.quiet "Version: $version"
}
9. group属性:定义了task所属的一个逻辑组
description属性:对task的目的做一个简短的概括
task printVersion(group: 'versioning', description: 'Prints project version.') << {
logger.quiet "Version: $version"
}
等价于:
task printVersion {
group = 'versioning' //调用setter方法
description = 'Prints project version.'
doLast {
logger.quiet "Version: $version"
}
}
10. task依赖:使用dependsOn方法
task first << { println "first" }
task second << { println "second" }
task printVersion(dependsOn: [second, first]) << {
logger.quiet "Version: $version"
}
task third << { println "third" }
third.dependsOn('printVersion') //当声明依赖时,通过名字引用task
11. finalizer task
使用场景:当依赖于某项资源的任务执行完时,这个资源需要释放
task first << { println "first" }
task second << { println "second" }
first.finalizedBy second //声明一个任务被另一个任务终结
12. groovy语法声明类:类的属性不需要写修饰符,不需要加getter和setter方法,产生字节码时会自动产生
version = new ProjectVersion(0, 1)
class ProjectVersion {
Integer major
Integer minor
Boolean release
ProjectVersion(Integer major, Integer minor) { //构造方法
this.major = major
this.minor = minor
this.release = Boolean.FALSE
}
ProjectVersion(Integer major, Integer minor, Boolean release) {
this(major, minor)
this.release = release
}
@Override
String toString() {
"$major.$minor${release ? '' : '-SNAPSHOT'}"
}
}
13. task configuration:task中未使用action,总是会在所有action执行前就执行
ext.versionFile = file('version.properties') //自定义属性versionFile
task loadVersion {
project.version = readVersion()
}
ProjectVersion readVersion() {
logger.quiet 'Reading the version file.'
if(!versionFile.exists()) {
throw new GradleException("Required version file does not exist: $versionFile.canonicalPath")
}
Properties versionProps = new Properties()
versionFile.withInputStream {
stream -> versionProps.load(stream)
}
//当返回的对象是方法中的最后一条语句时,可以忽略return关键字
new ProjectVersion(versionProps.major.toInteger(), versionProps.minor.toInteger(), versionProps.release.toBoolean())
}
14. gradle构建生命周期:
初始化阶段 -> 配置阶段 -> 执行阶段
配置阶段执行task配置等,执行阶段执行task action
15. Keep in mind that any configuration code is executed with every build of your project—even if you just execute gradle tasks.
16. 项目版本切换为生产版本
task makeReleaseVersion(group: 'versioning', description: 'Makes project a release version.') << {
inputs.property('release', version.release)
outputs.file versionFile
doLast {
version.release = true
ant.propertyfile(file: versionFile) {
entry(key: 'release', type: 'string', operation: '=', value: 'true')
}
}
}
17. 自定义task类
class ReleaseVersionTask extends DefaultTask { //自定义task需要继承DefaultTask
@Input Boolean release //声明自定义task的输入、输出属性
@OutputFile File destFile
ReleaseVersionTask() { //构造方法
group = 'versioning'
description = 'Makes project a release version.'
}
@TaskAction
void start() { //声明方法
project.version.release = true
ant.propertyfile(file: destFile) {
entry(key: 'release', type: 'string', operation: '=', value: 'true')
}
}
}
通过自定义task类构建task:
task makeReleaseVersion(type: ReleaseVersionTask) {
release = version.release //设置自定义task的属性
destFile = versionFile
}
18. 声明一个task rule
tasks.addRule("Pattern: increment<Classifier>Version – Increments the project version classifier.") {
String taskName ->
if(taskName.startsWith('increment') && taskName.endsWith('Version')) {
task(taskName) << { //添加一个task
String classifier = (taskName - 'increment' - 'Version').toLowerCase()
String currentVersion = version.toString()
switch(classifier) {
case 'major': ++version.major
break
case 'minor': ++version.minor
break
default: throw new GradleException("Invalid version type '$classifier. Allowed types: ['Major', 'Minor']")
}
String newVersion = version.toString()
logger.info "Incrementing $classifier project version: $currentVersion -> $newVersion"
ant.propertyfile(file: versionFile) {
entry(key: classifier, type: 'int', operation: '+', value: 1)
}
}
}
}
19. Groovy类结构
.
├── build.gradle
├── buildSrc
│ └── src
│ └── main
│ └── groovy
│ └── com
│ └── manning
│ └── gia
│ ├── ProjectVersion.groovy
│ └── ReleaseVersionTask.groovy
├── src
│ └── ...
└── version.properties
0 0
- Gradle笔记2-构建脚本
- Gradle学习笔记(二)构建脚本
- Gradle--第六章、基本构建脚本2
- Gradle:构建脚本概要
- Gradle构建脚本基础
- 《gradle教程系列》二.构建gradle脚本
- Gradle之构建脚本入门
- Android Studio Gradle构建脚本
- 【Gradle教程】第六章 构建脚本基础
- gradle学习(11)-编写构建脚本
- Gradle第十三章:编写构建脚本
- Gradle--第六章、基本构建脚本
- gradle--第六章、基本构建脚本3
- Gradle——构建脚本基础
- 学习写Gradle构建脚本(一)
- Android Studio 构建脚本-gradle详解
- Gradle笔记——构建基础
- Gradle 笔记——Java构建入门
- parse第三方服务好像要不能使用了
- 第三方开源库:个推
- 关于C++域作用符详解
- 在Ubuntu 16.04 LTS 下用Vim 打造自己的Python IDE 问题总结
- Android中dp px sp之间转化
- Gradle笔记2-构建脚本
- openH264编码流程
- 手机视频直播系统开发技术难点分析
- 告别Spinner下拉列表,用popupWindow 更美好
- linux下的编程问题
- bat-1
- 通讯录的获取
- android-async-http-master这个开源框架 post请求头怎么设置
- 关于Linux中创建软连接ln -s出现红色闪烁文件名