Chapter 1. Hello, Gradle!
- ANT:灵活,没有任何的约定俗成,没有依赖管理,虽然后来加入了依赖管理,但是还是不支持约定
- Maven:提供了严格的标准和依赖管理,但是使用麻烦
- Gradle:提供开箱即用的约定即约定大于配置,并且也可以灵活的改变
1.1.Build Files in Groovy
- XML:适合机器阅读,人读很疼苦;方便表达嵌套关系,不利于描述程序流程,数据访问;构建工具选用XML是错误的。
- Groovy:Gradle选用Groovy,一种基于JVM的动态语言;每个Gradle构建文件都是一个可执行的Groovy脚本;
1.2. Domain-Specific Build Languages
1.3. Getting Started
-
解压
-
添加环境变量
GRADLE_HOME
, 指向上面的路径 (可选) -
添加环境变量到path中
$GRADLE_HOME/bin
1.4. The Hello World Build File
task helloWorld << { println 'hello, world' }
$ gradle -q helloWorld
task hello << { print 'hello, ' } task world(dependsOn: hello) << { println 'world' }
$ gradle -q world
1.5. Building a Java Program
apply plugin: 'java'
$ gradle build
Chapter 2. Gradle Tasks
2.1. Declaring a Task
Appending a task’s actions one at a time
2.3. Task Configuration
任务配置和任务动作定义
$ gradle -b scratch.gradle initializeDatabase configuring database connection :initializeDatabase connect to database update database schema
initializeDatabase { println 'configuring database connection' }称为配置块(和Action相比少了<<),采用了闭包的方式实现,闭包函数像一个对象一样可以被作为参数传递或者赋值到变量上,然后被执行。 |
2.4. Tasks Are Objects
2.4.1. DefaultTask中的方法
dependsOn(task)
Different ways of calling the dependsOn method // Declare that world depends on hello // Preserves any previously defined dependencies as well task loadTestData { dependsOn createSchema } // An alternate way to express the same dependency task loadTestData { dependsOn << createSchema } // Do the same using single quotes (which are usually optional) task loadTestData { dependsOn 'createSchema' } // Explicitly call the method on the task object task loadTestData loadTestData.dependsOn createSchema // A shortcut for declaring dependencies task loadTestData(dependsOn: createSchema) Different ways of calling the dependsOn method for multiple dependencies // Declare dependencies one at a time task loadTestData { dependsOn << compileTestClasses dependsOn << createSchema } // Pass dependencies as a variable-length list task world { dependsOn compileTestClasses, createSchema } // Explicitly call the method on the task object task world world.dependsOn compileTestClasses, createSchema // A shortcut for dependencies only // Note the Groovy list syntax task world(dependsOn: [ compileTestClasses, createSchema ]) |
doFirst(closure)
Calling the doFirst method on the task object task setupDatabaseTests << { // This is the task's existing action println 'load test data' } setupDatabaseTests.doFirst { println 'create schema' } Calling the doFirst method inside the task’s configuration block task setupDatabaseTests << { println 'load test data' } setupDatabaseTests { doFirst { println 'create schema' } } Repeated calls to doFirst are cumulative task setupDatabaseTests << { println 'load test data' } setupDatabaseTests.doFirst { println 'create database schema' } setupDatabaseTests.doFirst { println 'drop database schema' } Repeated calls to doFirst, refactored |
doLast(closure)
onlyIf(closure)
A build file making use of the onlyIf method. |
2.4.2. DefaultTask中的属性
didWork
编译成功发送邮件 |
enabled
设置任务不可用 |
path
A single-level build file that echoes its only task’s path |
logger
DEBUG INFO LIFECYCLE WARN QUIET ERROR
logging
description
设置任务动作的同时设置任务描述 |
temporaryDir
2.4.3. Dynamic Properties
任务的动态属性 task copyFiles { // Find files from wherever, copy them // (then hardcode a list of files for illustration) fileManifest = [ 'data.csv', 'config.json' ] } task createArtifact(dependsOn: copyFiles) << { println "FILES IN MANIFEST: ${copyFiles.fileManifest}" } |
2.5. Task Types
Copy
拷贝任务 task copyFiles(type: Copy) { from 'resources' into 'target' include '**/*.xml', '**/*.txt', '**/*.properties' } |