+关注继续查看

http://blog.csdn.net/maosidiaoxian/article/details/79112639

# 第十三章. 编写构建脚本

### Chapter 13. Writing Build Scripts

This chapter looks at some of the details of writing a build script.

#### 13.1. The Gradle build language

Gradle provides a domain specific language, or DSL, for describing builds. This build language is based on Groovy, with some additions to make it easier to describe a build.

A build script can contain any Groovy language element. [5] Gradle assumes that each build script is encoded using UTF-8.

## 13.2. Project API

#### 13.2. The Project API

In the tutorial in Chapter 7, Java Quickstart we used, for example, the apply() method. Where does this method come from? We said earlier that the build script defines a project in Gradle. For each project in the build, Gradle creates an object of type Project and associates this Project object with the build script. As the build script executes, it configures this Project object:

### 获取有关编写构建脚本的帮助

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.

##### Getting help writing build scripts
• 在构建脚本中，你所调用的任何方法，如果在构建脚本中没有定义，那么它将被委托给Project对象。

Any method you call in your build script, which is not defined in the build script, is delegated to the Project object.

• 在构建脚本中，你所访问的任何一个属性，如果在构建脚本里没有定义，它也会被委托给Project对象。

Any property you access in your build script, which is not defined in the build script, is delegated to the Project object.

Let's try this out and try to access the name property of the Project object.

build.gradle

println name
println project.name

gradle -q check的输出结果
Output of gradle -q check

> gradle -q check
projectApi
projectApi

Both println statements print out the same property. The first uses auto-delegation to the Project object, for properties not defined in the build script. The other statement uses the project property available to any build script, which returns the associated Project object. Only if you define a property or a method which has the same name as a member of the Project object, you need to use the project property.

### 13.2.1. 标准project属性

##### 13.2.1. Standard project properties

Project对象提供了一些在构建脚本中可用的标准的属性。下表列出了几个常用的属性。
The Project object provides some standard properties, which are available in your build script. The following table lists a few of the commonly used ones.

 名称Name 类型Type 默认值Default Value project Project Project实例The Project instance name String 项目目录名称。The name of the project directory. path String 项目绝对路径。The absolute path of the project. description String 项目描述。A description for the project. projectDir File 包含构建脚本的目录。The directory containing the build script. buildDir File projectDir/build group Object 未指定unspecified version Object 未指定unspecified ant AntBuilder AntBuilder实例An AntBuilder instance

## 13.3.  Script API

#### 13.3. The Script API

When Gradle executes a script, it compiles the script into a class which implements Script. This means that all of the properties and methods declared by the Script interface are available in your script.

## 13.4. 声明变量

#### 13.4. Declaring variables

There are two kinds of variables that can be declared in a build script: local variables and extra properties.

### 13.4.1. 局部变量

##### 13.4.1. Local variables

Local variables are declared with the def keyword. They are only visible in the scope where they have been declared. Local variables are a feature of the underlying Groovy language.

build.gradle

def dest = "dest"

from "source"
into dest
}

### 13.4.2. 额外属性

##### 13.4.2. Extra properties

All enhanced objects in Gradle's domain model can hold extra user-defined properties. This includes, but is not limited to, projects, tasks, and source sets. Extra properties can be added, read and set via the owning object's extproperty. Alternatively, an ext block can be used to add multiple properties at once.

build.gradle

apply plugin: "java"

ext {
springVersion = "3.1.0.RELEASE"
}

sourceSets.all { ext.purpose = null }

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

println springVersion
sourceSets.matching { it.purpose == "production" }.each { println it.name }
}

gradle -q printProperties的输出结果
Output of gradle -q printProperties

> gradle -q printProperties
3.1.0.RELEASE
build@master.org
main
plugin

In this example, an ext block adds two extra properties to the project object. Additionally, a property named purpose is added to each source set by setting ext.purpose to null (null is a permissible value). Once the properties have been added, they can be read and set like predefined properties.

By requiring special syntax for adding a property, Gradle can fail fast when an attempt is made to set a (predefined or extra) property but the property is misspelled or does not exist. Extra properties can be accessed from anywhere their owning object can be accessed, giving them a wider scope than local variables. Extra properties on a project are visible from its subprojects.

For further details on extra properties and their API, see ExtraPropertiesExtension.

## 13.5.一些 Groovy 基础知识

#### 13.5. Some Groovy basics

Groovy provides plenty of features for creating DSLs, and the Gradle build language takes advantage of these. Understanding how the build language works will help you when you write your build script, and in particular, when you start to write custom plugins and tasks.

### 13.5.1. Groovy JDK

##### 13.5.1. Groovy JDK

Groovy对标准的Java类添加了许多有用的方法。例如，Iterable新增了each方法，可以对Iterable的元素进行遍历：
Groovy adds lots of useful methods to the standard Java classes. For example, Iterable gets an each method, which iterates over the elements of the Iterable:

build.gradle

// Iterable gets an each() method
configurations.runtime.each { File f -> println f }

Have a look at http://groovy.codehaus.org/groovy-jdk/ for more details.

### 13.5.2. 属性访问器

##### 13.5.2. Property accessors

Groovy 会自动地把属性的引用转换为适当的 getter 或 setter 方法。
Groovy automatically converts a property reference into a call to the appropriate getter or setter method.

build.gradle

// Using a getter method
println project.buildDir
println getProject().getBuildDir()

// Using a setter method
project.buildDir = 'target'
getProject().setBuildDir('target')

### 13.5.3 方法调用上的可选括号

##### 13.5.3. Optional parentheses on method calls

Parentheses are optional for method calls.

build.gradle

test.systemProperty 'some.prop', 'value'
test.systemProperty('some.prop', 'value')

### 13.5.4. List 和 Map

##### 13.5.4. List and map literals

Groovy 提供了一些定义ListMap实例的快捷写法。
Groovy provides some shortcuts for defining List and Map instances.

build.gradle

// List literal

List<String> list = new ArrayList<String>()
test.includes = list

// Map literal
apply plugin: 'java'

Map<String, String> map = new HashMap<String, String>()
map.put('plugin', 'java')
apply(map)

### 13.5.5. 作为方法最后一个参数的闭包

##### 13.5.5. Closures as the last parameter in a method

The Gradle DSL uses closures in many places. You can find out more about closures here. When the last parameter of a method is a closure, you can place the closure after the method call:

build.gradle

repositories {
println "in a closure"
}
repositories() { println "in a closure" }
repositories({ println "in a closure" })

### 13.5.6. 闭包委托（delegate）

##### 13.5.6. Closure delegate

Each closure has a delegate object, which Groovy uses to look up variable and method references which are not local variables or parameters of the closure. Gradle uses this for configuration closures, where the delegate object is set to the object to be configured.

build.gradle

dependencies {
assert delegate == project.dependencies
compile('junit:junit:4.11')
delegate.compile('junit:junit:4.11')
}

[5除语句标签之外的任何语言元素。
[5Any language element except for statement labels.

【Elastic Engineering】Elasticsearch 中的一些重要概念: cluster, node, index, document, shards 及 replica
Elasticsearch 中的一些重要概念: cluster, node, index, document, shards 及 replica
2 0
【Elastic Engineering】Elasticsearch 中的一些重要概念: cluster, node, index, document, shards 及 replica
Elasticsearch 中的一些重要概念: cluster, node, index, document, shards 及 replica
6 0
【Elastic Engineering】Elasticsearch 简介
Elasticsearch 是一个非常强大的搜索引擎。
4 0

22 0

4 0
【Elastic Engineering】如何在 Linux，MacOS 及 Windows 上进行安装 Elasticsearch

17 0

14 0

6 0

23 0
+关注

222

0

《2021云上架构与运维峰会演讲合集》

《零基础CSS入门教程》

《零基础HTML入门教程》