美文网首页
【笔记】关于 Gradle 你需要知道的 5 件事

【笔记】关于 Gradle 你需要知道的 5 件事

作者: 你可记得叫安可 | 来源:发表于2019-10-29 19:52 被阅读0次

1. gradle 是一个通用的构建工具

但是目前 gradle 的依赖管理(dependency management)只支持兼容 Maven、Ivy 和 文件系统(本地文件)

2. gradle 的构建模型是基于任务(task)的

gradle 将所有的任务构造成一个有向无环图(Directed Acyclic Graphs - DAGs)。任务图是由 plugin 和你自己的 build script 定义出来的。

Two examples of Gradle task graphs.png
Task 由以下部分组成:
  • Actions - 这个任务所需要做的工作,比如拷贝文件、编译源码
  • Inputs - action 所需输入,values, files and directories
  • Outputs - action 修改或生成的 files, directories

3. Gradle 的几个固定阶段

3.1 Initialization

设置构建环境,决定哪些 project 需要被引入

3.2 Configuration

构建任务图(DAG),然后决定哪些任务需要被执行,按照什么顺序执行。这个阶段是每次 build 都会执行的。避免在这个阶段执行耗时任务。

3.3 Execution

执行这些 task

This is important because code evaluated during the configuration phase won’t see changes that happen during the execution phase. 这个没看太懂?

4. Gradle 提供多种方式的扩展

4.1 自定义 taskType

可以将自定义任务的源码放在 buildSrc 目录下,或者一个打包好的 plugin 中。

4.2 自定义任务 action

可以通过 Task.doFirst()Task.doLast() 来添加编译逻辑

4.3 Extra properties on projects and tasks

就是我们经常看到的 ext 写法。例如下面 projectext 例子:

plugins {
    id 'java'
}

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

sourceSets.all { ext.purpose = null }

sourceSets {
    main {
        purpose = "production"
    }
    test {
        purpose = "test"
    }
    plugin {
        purpose = "production"
    }
}

task printProperties {
    doLast {
        println springVersion
        println emailNotification
        sourceSets.matching { it.purpose == "production" }.each { println it.name }
    }
}

添加了 springVersionemailNotification 两个 extra 属性到 project 中。通过 sourceSets.all { ext.purpose = null } 向每一个 sourceSets 添加一个 purpose 属性,初值为 null。之后再给 main, test, plugin 这三个 sourceSetspurpose 属性分别赋值。

4.4 自定义惯例

就像 Java 工程的目录结构,这些都是 Java build 的惯例。这些也可以自定义。

4.5 自定义 model

这个我没什么实践

5. Build scripts operate against an API

相关文章

网友评论

      本文标题:【笔记】关于 Gradle 你需要知道的 5 件事

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