What is Gradle

EDITORIAL words, a recent study Gradle system, originally wanted to write an article on the introduction of Gradle.
But in the official website I found this introduction to Gradle, very good already described, I would be a direct translation.
Original Address https://docs.gradle.org/current/userguide/what_is_gradle.html

What Gradle that?

Gradle build automation tool is open source, it's designed to be flexible enough to build almost any software.

It has the following significant advantages:

high performance

Gradle tasks run only those input and output have changed, in order to avoid ineffective work. Further, the task switch buffer output before the reuse (cache on other machines are also possible) can also.
Gradle also implements a number of other optimization, the development team will continually improve the performance of Gradle.

Based on JVM

Gradle is run on the JDK, JDK must be installed in order to run. Because it is run on JDK, which is an added bonus for users to use Java, you can use the standard build script in Java API, such as custom tasks and plug-ins, in addition, because it is based on the JDK, it can be a good cross platforms.
Gradle build JVM is not limited to the project, and even comes with native support for the construction of the project.

Promise

Gradle learn Mavean approach, by implementing the agreement, the common types of items (such as Java project) easy to build. Use the appropriate plug-ins can easily generate build scripts for many projects.
Conventions and there is no limit, you can overwrite the original agreement, add custom tasks to achieve your building model.

Expansibility

Gradle has good scalability, you can be added to define their own mission, overwrite the existing task types or even define your own build the model. Android supports plug-ins is a good example, increasing the flavor and buildType agreed in the original basis.

IDE support

Several common mainstream IDE support import Gradle project, and you can interact with, such as Android studio, Eclipse, IntelliJ IDEA, NetBeans and so on. Gradle also the project file is loaded into the Visual studio solutions.

Insight

Build the Scan provides a wealth of information to build running, you can use it to locate building problems. It can help you locate building performance problems. You can also share it to other people, so that when the issue of the exchange is very useful.
A more detailed construction of the scan can view this document https://scans.gradle.com/?_ga=2.3508848.1142716506.1563790305-874993899.1553652374

Five important concepts of Gradle

Gradle is a flexible and powerful tool when it is easy to use just daunting. After a few core principles to understand the following Gradle will feel very good understanding, which will help you better use Gradle.

Gradle is a common building tool

Because the building does not need to do or how to build Gradle imagine doing anything, so you can build almost Gradle task software.

The biggest limitation is that only supports Maven dependency management and lvy compiled libraries and files.
In spite of this limitation, but not in time with the trouble, the project of common types are easily constructed - such as the Java compiler library, layer conventions, and pre-built functionality by adding plug-ins, you can own conventions and functions packaged as a plug-in, of course, also be released this plugin.

Gradle is based on the core task

Gradle be constructed modeling task (element work) is directed acyclic graph. It means arranged to build actually a set of tasks, and to create a directed acyclic graph according to their dependencies. Once the map is created, Gradle will determine which tasks run in what order, and then execute them.
FIG. Below are two tasks, one is simple, is a very detailed, the task dependency indicated by arrows.

task-dag-examples

几乎所有的构建过程都可以建模为任务图,这也是Gradle如此灵活的原因之一。任务图可以通过插件和你自己的脚本定义,关于任务依赖性 可以看 文档 https://docs.gradle.org/current/userguide/tutorial_using_tasks.html#sec:task_dependencies

任务由三部分构成

  • 操作 ——做一些工作,例如复制文件或者编译源码
  • 输入 ——值,行为能够操作的值,文件或者目录。
  • 输出 ——行为修改或产生的值,文件或目录

实际上根据现实情况以上都是可选的,例如在Gradle 的生命周期里任务就有的什么也没做,只是将多个任务组合起来。
另外 Gradle 的增量编译是很强大可靠的,为了快速编译除非真的需要执行 clean 任务之外没必要执行clean操作。

你可以选择运行哪些任务。通过指定执行所需任务来节省时间,但也仅此而已。如果你想运行单元测试,就应该执行 test任务。如果您想打包应用程序,大多数构建都有相应的组装任务。

最后一件事:Gradle的增量构建支持是健壮和可靠的,所以除非您真的想执行一个clean任务,否则通过避免 clean 任务来保持构建的快速运行

Gradle有几个固定的构建阶段

理解Gradle在三个阶段评估和执行构建脚本是很重要的:

  1. 初始化
    创建构建环境并确定哪些项目参与构建
  2. 配置
    为构建构造和配置任务图,根据用户想要运行的任务确定以何种顺序执行哪些任务。
  3. 执行
    运行配置阶段结束时选择的任务。

这些阶段就构成了 Gradle 的生命周期。

与Apache Maven术语的比较:
Gradle的构建阶段不像Maven的阶段。Maven的构建阶段将构建分为多个阶段。它们的作用类似于 Gradle 的任务图,尽管不那么灵活。
Maven的构建生命周期概念与Gradle的生命周期任务大致相似。

好的设计构建脚本主要由声明式配置而非命令逻辑组成。在配置阶段可以理解地评估该配置。即便如此,许多此类构建还具有任务操作 - 例如通过doLast {}和doFirst {}块 - 在执行阶段进行评估。这很重要,因为在配置阶段评估的代码不会看到在执行阶段发生的更改。

配置阶段另一个重要的方面是每次构建都会对其中涉及的所有内容做评估。这也是为什么应该在配置阶段避免多余的工作。构建扫描可以帮助你识别这样的事情。

Gradle 的扩展方式

如果Gradle 附带的构建逻辑无法满足我们的构建需求,而往往通常是这样。大部分的构建都有特定的需求,需要我们添加自己的构建逻辑。

Gradle 提供了以下几个主要方式去扩展它。

  1. 自定义任务类型

    当 Gradle 本身的任务无法满足你的时候,你可以编写自己的任务类型。最好将编写的任务源文件放在 buildSrc 目录或者打包一个插件,然后你就可以像使用其他的Gradle 任务那样使用了。
    参考 https://docs.gradle.org/current/userguide/custom_tasks.html

  2. 自定义任务操作

    你可以将自己的构建逻辑附加在任务的 开始之前 Task.doFirst() 和 结束之后 Task.doLast() 的方法里。

  3. 项目和任务的额外属性

    Gradle 允许你为项目或者任务添加自己的属性,你可以在自定义行为或者其他构建逻辑中使用。额外的属性甚至可以应用于你没有显式创建的任务,比如由Gradle的核心插件创建的任务。

  4. 自定义约定

    Convention is a powerful method for a simplified construction, so that users can more easily understand and use thereof. This can be seen in the use of standard project structure and naming conventions (such as Java build) the build.
    You can write your own plug-ins to provide agreed - they only need to configure the default values for the relevant aspects of the build.

  5. Custom models

    Gradle allows you to introduce in addition to tasks, files and dependencies concept beyond configuration. You can see this, they will be added to the concept of source set in the most languages ​​to build plug-in. To build appropriate modeling process can greatly improve the ease and efficiency of construction.

Script API operation

The build script is actually executable code. Good script describes the steps needed to build the software, rather than how these steps should work. It was custom tasks and plugins should do.

There is a common misconception is that Gradle powerful and flexible because it is the code of the script, not really. This is the ability to provide the underlying model and API. As we suggested in practice that you should not put too much imperative logic in the script. However, the build script executable code seen as one aspect is useful: to understand how to build grammar Gradle script mapped to the API. Document referenced by Groovy DSL API Javadocs composition and methods and properties are listed and references and closure operations. What this means in the context of a build script? View entry script Groovy build, understand the answer to this question, in order to be able to effectively use the API documentation. https://docs.gradle.org/current/userguide/groovy_build_script_primer.html#groovy_build_script_primer

Because Gradle to run on the JVM, so the script can use the standard Java API, Groovy scripts can use Groovy API, Kotlin script can use Kotlin API.

Guess you like

Origin www.cnblogs.com/skymxc/p/Gradle.html