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

荣晨朗
2023-12-01

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: 1.676 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
75b3
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.

 类似资料: