1. 程式人生 > >Gradle基礎:2:Gradle的HelloWorld

Gradle基礎:2:Gradle的HelloWorld

gradle
這篇文章使用最簡單的HelloWorld例子對Gradle的使用進行概要性的說明。

事前準備

事前安裝和設定好gradle

liumiaocn:hello liumiao$ gradle --version

------------------------------------------------------------
Gradle 4.10.2
------------------------------------------------------------

Build time:   2018-09-19 18:10:15 UTC
Revision:     b4d8d5d170bb4ba516e88d7fe5647e2323d791dd

Kotlin DSL:   1.0-rc-6
Kotlin:       1.2.61
Groovy:       2.4.15
Ant:          Apache Ant(TM) version 1.9.11 compiled on March 23 2018
JVM:          1.8.0_191 (Oracle Corporation 25.191-b12)
OS:           Mac OS X 10.14 x86_64

liumiaocn:hello liumiao$ 

第一個HelloWorld

  • 準備一個groovy檔案,具體資訊如下
liumiaocn:hello liumiao$ ls
build.gradle
liumiaocn:hello liumiao$ cat build.gradle 
println "hello gradle"
liumiaocn:hello liumiao$
  • 執行gradle命令
liumiaocn:hello liumiao$ gradle

> Configure project :
hello gradle

> Task :help

Welcome to Gradle 4.10.2.

To run a build, run gradle <task> ...

To see a list of available tasks, run gradle tasks

To see a list of command-line options, run gradle --help

To see more detail about a task, run gradle help --task <task>

For troubleshooting, visit https://help.gradle.org

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
liumiaocn:hello liumiao$ 

這個例子看起來更像是groovy的hello world,接下來先對gradle的基本概念等資訊進行展開後,對此例子再進行進一步地解釋。

基礎概念

Gradle有兩個最基礎的概念:

  • Task:在構建中Gradle執行和管理的原子粒度的最小單元,比如上例中的輸出hello的語句,在實際的使用中比如編譯或者執行單體測試等操作均可作為task。
  • Project:可以理解為Project就是存放Task的場所,Project可由一個或多個Task所組成。而構建也可以由多個Project所組成,這樣就能實現對不同專案的適應。

兩個gradle檔案

在例子中使用了一個build.gradle檔案,還有一個settings.gradle檔案在最初使用時會經常見到:

  • build.gradle: 用於存放構建相關的Task,單個工程(Project)與build.gradle的關係是一對一的關係,如果是多個工程的情況下,類似與maven的module的方式,構建根目錄下的build.gradle起到總綱的作用。
  • settings.gradle: 用於存放設定相關的資訊,單工程時不是必須的,多工程是必須的,一般用於引入多個工程,在專案初期化的時候會根據此檔案生成一個Settings例項用於執行。

Projects的確認

使用gradle projects可以檢視工程下的詳細資訊

liumiaocn:hello liumiao$ gradle projects

> Configure project :
hello gradle

> Task :projects

------------------------------------------------------------
Root project
------------------------------------------------------------

Root project 'hello'
No sub-projects

To see a list of the tasks of a project, run gradle <project-path>:tasks
For example, try running gradle :tasks

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
liumiaocn:hello liumiao$ 

可以看到,Root project 'hello’的提示,以及沒有子工程(No sub-projects)的提示。

Tasks的確認

通過gradle tasks可以檢視task的詳細資訊

liumiaocn:hello liumiao$ gradle tasks

> Configure project :
hello gradle

> Task :tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'hello'.
components - Displays the components produced by root project 'hello'. [incubating]
dependencies - Displays all dependencies declared in root project 'hello'.
dependencyInsight - Displays the insight into a specific dependency in root project 'hello'.
dependentComponents - Displays the dependent components of components in root project 'hello'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'hello'. [incubating]
projects - Displays the sub-projects of root project 'hello'.
properties - Displays the properties of root project 'hello'.
tasks - Displays the tasks runnable from root project 'hello'.

To see all tasks and more detail, run gradle tasks --all

To see more detail about a task, run gradle help --task <task>

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
liumiaocn:hello liumiao$ 

改進的HelloWorld

上述的HelloWorld例子過於簡單, 在settings.gradle中新增一句println來確認一下執行順序,然後設定project的名稱,而不是使用預設被指定的hello

liumiaocn:hello liumiao$ cat settings.gradle 
println "setting gradle ..."
rootProject.name='helloPorject'
liumiaocn:hello liumiao$

同樣,對task也做一些簡單的設定,詳細後續在task的使用方式中展開

liumiaocn:hello liumiao$ cat build.gradle 
task helloGradle {
  println "hello gradle"
}
project.task('helloTask',group:'helloGradle',description:'hello gradle task')
liumiaocn:hello liumiao$ 

確認project

liumiaocn:hello liumiao$ gradle projects
setting gradle ...

> Configure project :
hello gradle

> Task :projects

------------------------------------------------------------
Root project
------------------------------------------------------------

Root project 'helloPorject'
No sub-projects

To see a list of the tasks of a project, run gradle <project-path>:tasks
For example, try running gradle :tasks

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
liumiaocn:hello liumiao$

確認task

liumiaocn:hello liumiao$ gradle tasks
setting gradle ...

> Configure project :
hello gradle

> Task :tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

HelloGradle tasks
-----------------
helloTask - hello gradle task

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'helloPorject'.
components - Displays the components produced by root project 'helloPorject'. [incubating]
dependencies - Displays all dependencies declared in root project 'helloPorject'.
dependencyInsight - Displays the insight into a specific dependency in root project 'helloPorject'.
dependentComponents - Displays the dependent components of components in root project 'helloPorject'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'helloPorject'. [incubating]
projects - Displays the sub-projects of root project 'helloPorject'.
properties - Displays the properties of root project 'helloPorject'.
tasks - Displays the tasks runnable from root project 'helloPorject'.

To see all tasks and more detail, run gradle tasks --all

To see more detail about a task, run gradle help --task <task>

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
liumiaocn:hello liumiao$ 

執行task

task既然有名稱了,就可以使用名稱來執行了

liumiaocn:hello liumiao$ gradle helloGradle
setting gradle ...

> Configure project :
hello gradle

BUILD SUCCESSFUL in 0s
liumiaocn:hello liumiao$ 

總結

Task和Project兩個基本概念,settings.gradle和build.gradle兩個預設配置檔案,這是需要首先掌握的。