forked from DONGChuan/GradleUserGuide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Gradle 插件 | ||
|
||
Gradle的核心为真实世界提供了很少的自动化.所有的实用特性,类似编译java源码的能力,是由*插件*提供的.插件添加了新的任务(如:[JavaCompile](https://docs.gradle.org/current/dsl/org.gradle.api.tasks.compile.JavaCompile.html)),域对象(如:[SourceSet](https://docs.gradle.org/current/dsl/org.gradle.api.tasks.SourceSet.html)),公约(如:Java资源位置是`src/main/java`)以及来自其他插件延伸核心对象和对象。 | ||
|
||
在本章中,我们将讨论如何使用插件和关于插件的周边概念和术语。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# 应用插件 | ||
|
||
插件需要声明*被应用*,通过[Project.apply()](https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:apply(java.util.Map)方法完成.应用的插件是*idempotent*<sup>[注1]()</sup>,即相同的插件可以应用多次.如果插件先前以被应用,任何后来的应用是安全的,不会有任何影响的. | ||
|
||
|
||
[1]译注:英文直接翻译的意思是幂等(denoting an element of a set that is unchanged in value when multiplied or otherwise operated on by itself.),上下中的大意应该是不会受其他因素的影响. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# 二进制插件 | ||
|
||
**Example 21.2. Applying a binary plugin** | ||
|
||
**build.gradle** | ||
```gradle | ||
apply plugin: 'java' | ||
``` | ||
|
||
插件可以使用*插件ID*应用.插件的id作为给定的插件的唯一标识符.核心插件注册一个可以用作插件的id的短名称.在上述情况下,我们可以使用简称`java`的插件以应用[JavaPlugin](https://docs.gradle.org/current/javadoc/org/gradle/api/plugins/JavaPlugin.html).社区插件,一方面会使用一个完全合格的形式的插件id(如`com.github.foo.bar`),但还是有一些传统的插件可能仍然使用很短的,不合格的格式. | ||
|
||
不使用一个插件的id,插件也可以通过简单地指定类来应用插件: | ||
|
||
**Example 21.3. Applying a binary plugin by type** | ||
|
||
**build.gradle** | ||
|
||
```gradle | ||
apply plugin: JavaPlugin | ||
``` | ||
|
||
在上面的例子中,JavaPlugin是指[JavaPlugin](https://docs.gradle.org/current/javadoc/org/gradle/api/plugins/JavaPlugin.html),此类不是严格需要导入org.gradle.api.plugins包中的所有自动导入构建脚本(见:[附录E,现有的IDE支持,以及如何没有它应付](https://docs.gradle.org/current/userguide/ide_support.html)).此外,这是没有必要追加的.class以识别一个类常量在Groovy,因为它是在Java中。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# 脚本插件 | ||
|
||
**Example 21.1. Applying a script plugin** | ||
|
||
**build.gradle** | ||
|
||
```gradle | ||
apply from: 'other.gradle' | ||
``` | ||
|
||
脚本插件可以从本地文件系统或在远程位置的脚本中*应用*.文件系统的位置是相对于项目目录,而远程脚本位置的是由一个`HTTP URL`指定的.多个脚本插件(两种形式之一)可以被*应用*到给定的构建。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# 插件的类型 | ||
|
||
在Gradle中一般有两种类型的插件,*脚本*插件和*二进制*插件.*脚本*插件是额外的构建脚本,它会进一步配置构建,通常实行声明的方式操纵的构建.尽管他们可以外部化并且从远程位置访问,它们通常还是会在构建内部中使用.*二进制*插件是实现了[Plugin](https://docs.gradle.org/current/javadoc/org/gradle/api/Plugin.html)接口的类,并且采用编程的方式来操纵构建.*二进制*插件可以驻留在构建脚本,项目层级内或外部的插件jar. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# 插件的作用是什么 | ||
|
||
应用插件到项目允许插件来扩展项目的能力。它可以做的事情,如: | ||
|
||
* 扩展摇篮模型(如:添加可配置新的DSL元素) | ||
* 按照惯例配置项目(如:添加新的任务或配置合理的默认值) | ||
* 应用特定的配置(如:增加组织库或执行标准) | ||
|
||
通过应用插件,而不是向项目构建脚本添加逻辑,我们可以收获很多好处.应用插件: | ||
|
||
* 促进重用和减少维护在多个项目类似的逻辑的开销 | ||
* 允许更高程度的模块化,提高综合性和组织 | ||
* 封装必要的逻辑,并允许构建脚本尽可能是声明性地 | ||
|
||
|