天天看點

Gradle詳解-Chapter 4. Using the Gradle Command-Line

Chapter 14 基礎腳本建構

Chapter 16. Writing Build Scripts 寫腳本

Chapter17. More about Tasks

Chapter 20. The Build Lifecycle 生命周期

第四章:使用Gradle指令行

Chapter 4. Using the Gradle Command-Line

4.1. Executing multiple tasks 多任務執行

格式:gradle task1,task2,task3。。。

Gradle按照順序執行task,如果task有依賴關系執行依賴task。每個task隻執行一次,不管有多少個依賴關系。

task compile << {
    println 'compiling source'
}

task compileTest(dependsOn: compile) << {
    println 'compiling unit tests'
}

task test(dependsOn: [compile, compileTest]) << {
    println 'running unit tests'
}

task dist(dependsOn: [compile, test]) << {
    println 'building the distribution'
}
           

結果:執行dist task

D:\GradleTest\project2>gradle dist
:project2:compile
compiling source
:project2:compileTest
compiling unit tests
:project2:test
running unit tests
:project2:dist
building the distribution

BUILD SUCCESSFUL

Total time: 2.023 secs
           

4.2. Excluding tasks 執行task過程中除去哪個task

使用 -x 指令:gradle dist -x test

結果:

D:\GradleTest>gradle -q dist -x test
compiling source
building the distribution
           

4.3. Continuing the build when a failure occurs

當一個錯誤發生時,繼續執行build。

使用–continue 指令參數

4.4. Task name abbreviation task縮寫

保證該build檔案下,隻有一個該task的縮寫。否則指令出錯

縮寫支援:1,駱駝命名法的每個單詞首字母。2 task縮寫唯一(字母個數不限)

比如:分别在D:\GradleTest和D:\GradleTest\project2> 執行gradle di指令

project2檔案夾下結果是正确的。

D:\GradleTest\project2>gradle di
:project2:compile
compiling source
:project2:compileTest
compiling unit tests
:project2:test
running unit tests
:project2:dist
building the distribution

BUILD SUCCESSFUL

Total time: 1.597 secs
           

**GradleTest檔案夾下是錯誤的。

Candidates are: ‘dist’, ‘distribution’. 兩個task都可以用di縮寫**

D:\GradleTest>gradle di

FAILURE: Build failed with an exception.

* What went wrong:
Task 'di' is ambiguous in root project 'GradleTest'. Candidates are: 'dist', 'di
stribution'.

* Try:
Run gradle tasks to get a list of available tasks. Run with --stacktrace option
to get the stack trace. Run with --info or --debug option to get more log output
.

BUILD FAILED

Total time:  secs
           

4.5. Selecting which build to execute 選擇執行腳本

執行其他project中的task 指令。 使用-b 或者 -p指令參數

執行project2項目中的dist指令:

-b參數:

D:\GradleTest>gradle -q -b project2/build.gradle di

compiling source

compiling unit tests

running unit tests

building the distribution

-p參數:

D:\GradleTest>gradle -q -p project2 di

compiling source

compiling unit tests

running unit tests

building the distribution

4.6. Forcing tasks to execute

指令參數:–rerun-tasks

D:\GradleTest>gradle –rerun-tasks -q -p project2 di

compiling source

compiling unit tests

running unit tests

building the distribution

4.7. Obtaining information about your build 擷取腳本的資訊

擷取腳本的一些資訊,比如項目結構,屬性,各種任務指令,任務詳情等等。

4.7.1. Listing projects 項目結構(當期工程和子項目)

gradle projects 指令

備注:可以通過 description 屬性為每個子項目project添加描述資訊。

D:\GradleTest>gradle -q projects

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

Root project 'GradleTest'
+--- Project ':antLoadfileResources'
+--- Project ':javaLib'
+--- Project ':project1'
\--- Project ':project2' - description information  // 這個是自己添加的描述資訊

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

4.7.2. Listing tasks

擷取project的所有任務。 還可以顯示任務組group,任務描述資訊,如果他們被設定了。

使用gradle tasks 指令

D:\GradleTest>gradle -q tasks
I'm Gradle

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

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

Build22 tasks  // 自己的任務組group
-------------
hello - task description information

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root projec
t 'GradleTest'.
components - Displays the components produced by root project 'GradleTest'. [inc
ubating]
dependencies - Displays all dependencies declared in root project 'GradleTest'.
dependencyInsight - Displays the insight into a specific dependency in root proj
ect 'GradleTest'.
help - Displays a help message.
model - Displays the configuration model of root project 'GradleTest'. [incubati
ng]
projects - Displays the sub-projects of root project 'GradleTest'.
properties - Displays the properties of root project 'GradleTest'.
tasks - Displays the tasks runnable from root project 'GradleTest' (some of the
displayed tasks may belong to subprojects).

Other tasks
-----------
checksum
configure
configure1
count
dist
getPath
hellop
intro - task description information  // task 描述資訊
lib2
loadfile
myCopy
myTask
notALib
printTaskProperties
release
ss
task0
taskX
taskY
upper

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

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

更詳細資訊可以使用:gradle -q tasks –all指令

部分code:

Other tasks
-----------
project1:checksum
project1:configure
project1:configure1
project1:count
project2:dist
    project2:compile
    project2:compileTest
    project2:test
project1:getPath
project2:hellop
project1:intro - task description information
project1:lib2
project1:loadfile
project1:myCopy
project1:myTask
project1:notALib
project1:printTaskProperties
project1:release
    project1:distribution
project1:ss
project1:task0
    project1:task1
    project1:task2
    project1:task3
project1:taskX [project2:taskY]
    project1:lib1
project2:taskY
project1:upper
           

4.7.3. Show task usage details 顯示單個task詳情

使用gradle help –task someTask指令

D:\GradleTest>gradle -q help --task hello
I'm Gradle
Detailed task information for hello

Path
     :project1:hello

Type
     Task (org.gradle.api.Task)

Description
     task description information

Group
     build22
           

4.7.4. Listing project dependencies 檢視項目依賴清單

使用gradle dependencies 指令

顯示該項目下所有task的依賴關系

檢視project1項目的依賴

gradle -q dependencies :project1:dependencies

檢視project1和project2兩個項目的依賴

gradle -q dependencies :project1:dependencies :project2:dependencies

檢視某個task的依賴:通過–configuration指令

gradle -q project1:dependencies –configuration testCompile

GradleDemo中的位置:D:\Program Files\gradle-2.14\samples\userguide\tutorial\projectReports

4.7.5. Listing project buildscript dependencies

Running gradle buildEnvironment visualises the buildscript dependencies of the selected project, similarly to how gradle dependencies visualises the dependencies of the software being built.

4.7.6. Getting the insight into a particular dependency

Running gradle dependencyInsight gives you an insight into a particular dependency (or dependencies) that match specified input

指令:gradle -q webapp:dependencyInsight –dependency groovy –configuration compile

> gradle -q webapp:dependencyInsight --dependency groovy --configuration compile
org.codehaus.groovy:groovy-all:2.4.4
\--- project :api
     \--- compile
           

4.7.7. Listing project properties

使用 gradle properties 指令:目前項目的屬性

gradle -q properties

gradle -q :project1:properties

gradle -q :project2:properties

------------------------------------------------------------
Project :project1
------------------------------------------------------------

allprojects: [project ':project1']
ant: org.gradle.api.internal.project.DefaultAntBuilder@3ce36adf
antBuilderFactory: org.gradle.api.internal.project.DefaultAntBuilderFactory@767d
b3
artifacts: org.gradle.api.internal.artifacts.dsl.DefaultArtifactHandler_Decorate
d@26ba778b
asDynamicObject: DynamicObject for project ':project1'
baseClassLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderS
cope@1bfe5a8a
buildDir: D:\GradleTest\project1\build
buildFile: D:\GradleTest\project1\build.gradle
buildScriptSource: org.gradle.groovy.scripts.UriScriptSource@57ea2861
buildscript: org.gradle.api.internal.initialization.DefaultScriptHandler@4e01255
e
checksum: task ':project1:checksum'
childProjects: {}
           

4.7.8. Profiling a build

The –profile command line option will record some useful timing information while your build is running and write a report to the build/reports/profile directory. The report will be named using the time when the build was run.

This report lists summary times and details for both the configuration phase and task execution. The times for configuration and task execution are sorted with the most expensive operations first. The task execution results also indicate if any tasks were skipped (and the reason) or if tasks that were not skipped did no work.

Builds which utilize a buildSrc directory will generate a second profile report for buildSrc in the buildSrc/build directory.