內(nèi)容
跟隨本教程,您將創(chuàng)建出一個(gè)簡單的Gradle項(xiàng)目、調(diào)用一些基礎(chǔ)的Gradle命令,并對(duì)Gradle如何進(jìn)行項(xiàng)目管理有初步的了解。
準(zhǔn)備工作
大約12分鐘的時(shí)間
控制臺(tái)或IDE應(yīng)用程序
Java開發(fā)套件(JDK),如果要使用Gradle Groovy DSL需要1.7或更高版本,如果要使用Gradle Kotlin
DSL則需要1.8或更高版本(僅在運(yùn)行Gradle時(shí)需要)Gradle的發(fā)行版,4.10-rc-2以上版本
shell命令行會(huì)以類Unix操作系統(tǒng)的樣子展示。每條命令在Windows下都有對(duì)應(yīng)的等效命令。
初始化一個(gè)項(xiàng)目
首先,我們?yōu)轫?xiàng)目創(chuàng)建一個(gè)目錄。
? mkdir basic-demo
? cd basic-demo
現(xiàn)在,我們可以使用Gradle的 init 命令簡單地創(chuàng)建一個(gè)項(xiàng)目。我們會(huì)探尋所有生成出來的東西從而使您完全明白都發(fā)生了些什么。
? gradle init ①
Starting a Gradle Daemon (subsequent builds will be faster)
BUILD SUCCESSFUL in 3s
2 actionable tasks: 2 executed
① 如果您想使用Kotlin DSL,那么請(qǐng)使用
gradle init --dsl kotlin。更多詳情參考文檔。
命令行會(huì)顯示“BUILD SUCCESSFUL”并生成下文所示的“空”項(xiàng)目。如果不是這樣,那么請(qǐng)確認(rèn)Gradle是不是
正確安裝了,以及 JAVA_HOME 環(huán)境變量是否正確地設(shè)置了。
Gradle為你生成了這些東西:
Groovy
├── build.gradle (1)
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar (2)
│ └── gradle-wrapper.properties (3)
├── gradlew (4)
├── gradlew.bat (5)
└── settings.gradle (6)
Kotlin
├── build.gradle.kts (1)
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar (2)
│ └── gradle-wrapper.properties (3)
├── gradlew (4)
├── gradlew.bat (5)
└── settings.gradle.kts (6)
- 配置了當(dāng)前項(xiàng)目任務(wù)的項(xiàng)目配置腳本
- Gradle包裝器可執(zhí)行JAR包
- Gradle包裝器配置信息
- Gradle包裝器在類Unix操作系統(tǒng)下的腳本
- Gradle包裝器在Windows操作系統(tǒng)下的腳本
- 配置構(gòu)建中參與項(xiàng)目的配置腳本
gradle init可以生成多種不同類型的項(xiàng)目,甚至知道如何將簡單的pom.xml文件翻譯成Gradle支持的方式。
當(dāng)當(dāng)當(dāng)當(dāng)!我們的教程到這里就可以結(jié)束了。但你肯定要知道如何在項(xiàng)目中 使用 Gradle,所以讓我們繼續(xù)吧。
創(chuàng)建一個(gè)任務(wù)
Gradle提供了通過基于Groovy或Kotlin的DSL創(chuàng)建和配置任務(wù)的API。Project 包含了一系列Task,每個(gè)任務(wù)都會(huì)執(zhí)行一些基礎(chǔ)操作。
Gradle附帶一個(gè)可以用來配置自有項(xiàng)目的任務(wù)庫。例如叫做 Copy 的一個(gè)核心種類,可以將文件從某個(gè)位置復(fù)制到另一處。Copy任務(wù)是非常有用的(參考相關(guān)文檔獲取更多信息),但是現(xiàn)在,再一次,讓我們保持簡單。執(zhí)行如下步驟:
- 創(chuàng)建名為
src的目錄。 - 在
src文件夾添加名為myfile.txt的文件。其內(nèi)容隨意(甚至可以為空),但現(xiàn)在為了方便,就添加一行Hello, World!吧。 - 在構(gòu)建文件中定義一個(gè)
Copy類型的copy任務(wù)(注意大小寫),用于將所有src目錄下的文件復(fù)制到一個(gè)名為dest的新文件夾里。(開發(fā)者無需自行創(chuàng)建dest文件夾,任務(wù)會(huì)自動(dòng)幫你創(chuàng)建。)
Groovy
task copy(type: Copy, group: "Custom", description: "Copies sources to the dest directory") {
from "src"
into "dest"
}
Kotlin
tasks.create<Copy>("copy") {
description = "Copies sources to the dest directory"
group = "Custom"
from("src")
into("dest")
}
在這里,group 和 description 可以換成任何你需要的。甚至你也可以省略它們,但這樣做會(huì)導(dǎo)致它們在稍后我們將要使用的tasks 報(bào)告中被省略。
現(xiàn)在讓我們來執(zhí)行這個(gè) copy 任務(wù):
? ./gradlew copy
> Task :copy
BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
檢查 dest 目錄下有沒有 myfile.txt 文件,并驗(yàn)證內(nèi)容是否一致。
使用插件
Gradle包含一大堆插件,而且還有很多很多插件在Gradle插件門戶提供。跟隨發(fā)行版提供的插件之一是base 插件。結(jié)合 Zip核心類型,開發(fā)者就可以配置名稱、位置來給項(xiàng)目打zip包了。
使用 plugins 語法來給你的構(gòu)建文件添加 base 插件。確保要在文件頭部添加 plugins {} 代碼塊。
Gradle
plugins {
id "base"
}
... 構(gòu)建文件的其他部分 ...
Kotlin
plugins {
id("base")
}
... 構(gòu)建文件的其他部分 ...
現(xiàn)在,添加將 src 目錄打成zip包的任務(wù):
Gradle
task zip(type: Zip, group: "Archive", description: "Archives sources in a zip file") {
from "src"
setArchiveName "basic-demo-1.0.zip"
}
Kotlin
tasks.create<Zip>("zip") {
description = "Archives sources in a zip file")
group = "Archive"
from("src")
setArchiveName("basic-demo-1.0.zip")
}
base 插件和設(shè)置協(xié)同工作,可以在 build/distributions 文件夾下創(chuàng)建打包后的文件basic-demo-1.0.zip。
現(xiàn)在,簡單地運(yùn)行新添加的 zip任務(wù),就能在對(duì)應(yīng)位置看到生成的zip文件了。
? ./gradlew zip
> Task :zip
BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
構(gòu)建的瀏覽或調(diào)試
讓我們來看看Gradle在我們的新項(xiàng)目中還能做些什么。我們也提供了完整的命令行接口參考手冊。
發(fā)掘可用的任務(wù)
tasks 命令會(huì)列出所有可用的Gradle任務(wù),包括通過 base 插件引入的任務(wù)和我們剛剛添加的那些自定義任務(wù)。
? ./gradlew tasks
> Task :tasks
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
Archive tasks
-------------
zip - Archives sources in a zip file
Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
clean - Deletes the build directory.
Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.
Custom tasks
------------
copy - Simply copies sources to a the build directory
Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'basic-demo'.
components - Displays the components produced by root project 'basic-demo'. [incubating]
dependencies - Displays all dependencies declared in root project 'basic-demo'.
dependencyInsight - Displays the insight into a specific dependency in root project 'basic-demo'.
dependentComponents - Displays the dependent components of components in root project 'basic-demo'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'basic-demo'. [incubating]
projects - Displays the sub-projects of root project 'basic-demo'.
properties - Displays the properties of root project 'basic-demo'.
tasks - Displays the tasks runnable from root project 'basic-demo'.
Verification tasks
------------------
check - Runs all checks.
Rules
-----
Pattern: clean<TaskName>: Cleans the output files of a task.
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.
To see all tasks and more detail, run gradlew tasks --all
To see more detail about a task, run gradlew help --task <task>
BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
構(gòu)建的分析和調(diào)試
Gradle同時(shí)也提供了一個(gè)豐富的、基于web的頁面用于展示你的構(gòu)建,其名為構(gòu)建掃描。

通過使用 --scan參數(shù)或明確地在項(xiàng)目中啟用構(gòu)建掃描插件,你可以在scans.gradle.com免費(fèi)地創(chuàng)建一個(gè)構(gòu)建掃描。將構(gòu)建掃描發(fā)布到scans.gradle.com會(huì)將這些數(shù)據(jù)發(fā)送至Gradle服務(wù)器。要想使數(shù)據(jù)停留在您自己的服務(wù)器上,請(qǐng)考慮Gradle企業(yè)版。
嘗試在執(zhí)行一個(gè)任務(wù)的時(shí)候加入 --scan 選項(xiàng)。
? ./gradlew zip --scan
BUILD SUCCESSFUL in 0s
1 actionable task: 1 up-to-date
Publishing a build scan to scans.gradle.com requires accepting the Terms of Service defined at https://scans.gradle.com/terms-of-service. Do you accept these terms? [yes, no]
Gradle Cloud Services license agreement accepted.
Publishing build scan...
https://gradle.com/s/repnge6srr5qs
通過瀏覽構(gòu)建掃描結(jié)果,你可以輕松地看到那些任務(wù)被執(zhí)行了、每個(gè)任務(wù)執(zhí)行的耗時(shí)、使用了哪些插件等等。當(dāng)下次你在StackOverflow上調(diào)試什么東西的時(shí)候,考慮分享一下構(gòu)建掃描。
在構(gòu)建掃描插件用戶手冊上可以學(xué)到更多關(guān)于構(gòu)建掃描插件的配置和使用信息。
列舉可用屬性
properties 命令會(huì)告訴你項(xiàng)目的參數(shù)。
? ./gradlew properties
輸出結(jié)果非常長。下面是可用屬性的一部分:
> Task :properties
------------------------------------------------------------
Root project
------------------------------------------------------------
buildDir: /Users/.../basic-demo/build
buildFile: /Users/.../basic-demo/build.gradle.kts
description: null
group:
name: basic-demo
projectDir: /Users/.../basic-demo
version: unspecified
BUILD SUCCESSFUL
項(xiàng)目的 name 默認(rèn)和所在文件夾的名稱是一致的。你也可以給 group 和 version 屬性指定值,但現(xiàn)在它們和
description 一樣是使用的默認(rèn)值。
buildFile 屬性是構(gòu)建腳本的全名限定路徑,默認(rèn)位于 projectDir 下。
一些屬性是可以被修改的。例如在構(gòu)建腳本文件中嘗試加入如下代碼行,然后重新執(zhí)行 gradle properties。
description = "A trivial Gradle build"
version = "1.0"
接下來的步驟
恭喜!你已經(jīng)學(xué)會(huì)如何創(chuàng)建一個(gè)新的Gradle構(gòu)建并檢驗(yàn)它!
你肯定希望創(chuàng)建針對(duì)特定平臺(tái)的庫或應(yīng)用,所以這里給出一些教程,它們會(huì)知道你如何在選定的平臺(tái)上進(jìn)行構(gòu)建:
- 構(gòu)建Android應(yīng)用
- 構(gòu)建C++可執(zhí)行程序
- 構(gòu)建Groovy庫
- 構(gòu)建Java庫
- 構(gòu)建Kotlin JVM庫
- 構(gòu)建Scala庫
同時(shí)你也可以簽出一些GitHub上的Gradle構(gòu)建示例。
幫忙改進(jìn)本教程
尋求反饋或具有疑問?發(fā)現(xiàn)拼寫錯(cuò)誤?和所有的Gradle教程一樣,幫助就是一個(gè)GitHub的issue。請(qǐng)?jiān)?a target="_blank" rel="nofollow">gradle-guides/creating-new-gradle-builds添加issue或拉取請(qǐng)求,我們會(huì)給您幫助。