《gradle 用户指南》中文版 第4章 使用Gradle命令行

第4章 使用Gradle命令行

目录

4.1、执行多个任务 4.2、排除任务 4.3、发生故障时继续构建 4.4、任务名缩写 4.5、选择要执行的构建 4.6、强制执行任务 4.7、获取有关您的构建的信息 4.8、演习 4.9、总结 本章介绍了Gradle命令行的基础知识。 您使用gradle命令运行构建, 像在前面的章节中已经看到的。

4.1、执行多个任务

您可以通过在命令行中列出每个任务来在单个构建中执行多个任务。 例如,命令gradle compile test将执行编译和测试任务。 Gradle将按照命令行中列出的顺序执行任务,并且还将为每个任务执行依赖关系。 每个任务只执行一次,无论它是如何被包括在构建中:无论是在命令行中指定还是作为另一个任务的依赖,或两者兼而有之。 我们来看一个例子。 下面定义了四个任务。 dist和test都取决于编译任务。 为此构建脚本运行gradle dist测试会导致编译任务只执行一次。 图4.1。任务依赖
例4.1。执行多个任务 build.gradle

task compile {
    doLast {
        println 'compiling source'
    }
}

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

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

task dist(dependsOn: [compile, test]) {
    doLast {
        println 'building the distribution'
    }
}
gradle dist test 命令输出
> gradle dist test
:compile
compiling source
:compileTest
compiling unit tests
:test
running unit tests
:dist
building the distribution

BUILD SUCCESSFUL in 0s
4 actionable tasks: 4 executed
每个任务只执行一次,所以 “gradle test test” 与 “gradle test ”是一样的。

4.2、排除任务

您可以使用-x命令行选项排除执行任务,并提供要排除的任务的名称。 我们来试试这个与上面的示例构建文件。 gradle dist -x test 命令输出
> gradle dist -x test
:compile
compiling source
:dist
building the distribution

BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed
您可以从本示例的输出中看到,测试任务不执行,即使它是dist任务的依赖。 您还将注意到,测试任务的依赖关系(如compileTest)也不会执行。 另一个任务所需的测试依赖关系,如编译,仍然被执行。

4.3、发生故障时继续构建

默认情况下,任何任务失败时,Gradle将中止执行并失败构建。 这允许构建更快地完成,但会隐藏将发生的其他故障。 为了在单个构建执行中发现尽可能多的故障,可以使用--continue选项。 当使用--continue执行时,Gradle将执行每个要执行的任务,其中该任务的所有依赖关系完成而不会失败,而不是在遇到第一个失败时停止。 每个遇到的故障将在构建结束时报告。 如果任务失败,则依赖于任何后续任务将不会被执行,因为这样做是不安全的。 例如,如果在测试代码中存在编译失败,测试将不会运行; 因为测试任务将取决于编译任务(直接或间接)。

4.4、任务名缩写

在命令行中指定任务时,不必提供任务的全名。 您只需要提供足够的任务名称来唯一标识任务。 例如,在上面的示例构建中,您可以通过运行gradle d执行任务dist: 例4.3。缩写任务名称 gradle di 命令输出:
> gradle di
:compile
compiling source
:compileTest
compiling unit tests
:test
running unit tests
:dist
building the distribution

BUILD SUCCESSFUL in 0s
4 actionable tasks: 4 executed
您还可以在骆驼案例任务名称中缩写每个单词。 例如,您可以通过运行gradle compTest或甚至gradle cT执行任务compileTest 例4.4。缩略语骆驼案例任务名称 gradle cT 命令输出:
> gradle cT
:compile
compiling source
:compileTest
compiling unit tests

BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed
您还可以使用这些缩写与-x命令行选项。

4.5、选择要执行的构建

当您运行gradle命令时,它会在当前目录中查找一个构建文件。 您可以使用-b选项来选择另一个构建文件。 如果使用-b选项,则不会使用settings.gradle文件。 例4.5。使用构建文件选择项目 gradle -q -b subdir/myproject.gradle hello
task hello {
    doLast {
        println "using build file '$buildFile.name' in '$buildFile.parentFile.name'."
    }
}
gradle -q -b subdir/myproject.gradle hello 命令输出:
> gradle -q -b subdir/myproject.gradle hello
using build file 'myproject.gradle' in 'subdir'.
或者,您可以使用-p选项指定要使用的项目目录。 对于多项目构建,您应该使用-p选项而不是-b选项。 例4.6。使用项目目录选择项目 gradle -q -p subdir hello 命令输出:
> gradle -q -p subdir hello
using build file 'build.gradle' in 'subdir'.

4.6、强制执行任务

许多任务,特别是Gradle本身提供的任务都支持增量版本。 这些任务可以根据他们的输入或输出自上次运行以来是否发生变化来确定它们是否需要运行。 当Gradle在构建运行期间在其名称旁边显示文本UP-TO-DATE时,您可以轻松识别参与增量构建的任务。 您可能偶尔想强制Gradle运行所有的任务,忽略任何最新的检查。 如果是这样,只需使用--rerun-tasks选项即可。 以下是运行任务的输出,而不是使用--rerun-tasks: 例4.7。强制执行任务 gradle doIt 命令输出:
> gradle doIt
:doIt UP-TO-DATE
gradle --rerun-tasks doIt 命令输出:
> gradle --rerun-tasks doIt
:doIt
请注意,这将强制执行所有必需的任务,而不仅仅是您在命令行中指定的任务。 这有点像运行一个干净,但没有生成的生成的输出被删除。

4.7、获取有关您的构建的信息

Gradle提供了几个内置的任务,其中显示了您的构建的特定细节。 这对于了解构建的结构和依赖性以及调试问题可能很有用。 除了下面显示的内置任务之外,您还可以使用项目报告插件将任务添加到项目中,以生成这些报告。

4.7.1。列出再有项目

运行 gradle projects 可以显示所选项目的子项目列表,显示在层次结构中。

这是一个例子: 例4.8。获取有关项目的信息

gradle -q projects 命令输出:
> gradle -q projects

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

Root project 'projectReports'
+--- Project ':api' - The shared API for the application
\--- Project ':webapp' - The Web application implementation

To see a list of the tasks of a project, run gradle <project-path>:tasks
For example, try running gradle :api:tasks
报告显示每个项目的描述(如果指定)。 您可以通过设置description属性来提供项目的描述: 例4.9。提供项目描述 build.gradle
description = 'The shared API for the application'

4.7.2。列出任务

运行 gradle tasks 可以给出所选项目的主要任务列表。 此报告显示项目的默认任务(如果有)以及每个任务的说明。 以下是此报告的示例: 例4.10。获取有关任务的信息

gradle -q tasks 命令输出:

> gradle -q tasks

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

Default tasks: dists

Build tasks
-----------
clean - Deletes the build directory (build)
dists - Builds the distribution
libs - Builds the JAR

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 'projectReports'.
components - Displays the components produced by root project 'projectReports'. [incubating]
dependencies - Displays all dependencies declared in root project 'projectReports'.
dependencyInsight - Displays the insight into a specific dependency in root project 'projectReports'.
dependentComponents - Displays the dependent components of components in root project 'projectReports'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'projectReports'. [incubating]
projects - Displays the sub-projects of root project 'projectReports'.
properties - Displays the properties of root project 'projectReports'.
tasks - Displays the tasks runnable from root project 'projectReports' (some of the displayed tasks may belong to subprojects).

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

To see more detail about a task, run gradle help --task <task>
默认情况下,此报告仅显示已分配给任务组的任务,即所谓的可见任务。 您可以通过设置任务的组属性来执行此操作。 您还可以设置描述属性,以提供要包含在报告中的说明。 例4.11。更改任务报告的内容 build.gradle
dists {
    description = 'Builds the distribution'
    group = 'build'
}
您可以使用--all选项在任务列表中获取更多信息。 使用此选项,任务报告会列出项目中的所有任务,包括尚未分配给任务组的任务,即所谓的隐藏任务。 例4.12。获取有关任务的更多信息
  gradle -q tasks --all 命令输出

> gradle -q tasks --all

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

Default tasks: dists

Build tasks
-----------
clean - Deletes the build directory (build)
api:clean - Deletes the build directory (build)
webapp:clean - Deletes the build directory (build)
dists - Builds the distribution
api:libs - Builds the JAR
webapp:libs - Builds the JAR

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 'projectReports'.
api:buildEnvironment - Displays all buildscript dependencies declared in project ':api'.
webapp:buildEnvironment - Displays all buildscript dependencies declared in project ':webapp'.
components - Displays the components produced by root project 'projectReports'. [incubating]
api:components - Displays the components produced by project ':api'. [incubating]
webapp:components - Displays the components produced by project ':webapp'. [incubating]
dependencies - Displays all dependencies declared in root project 'projectReports'.
api:dependencies - Displays all dependencies declared in project ':api'.
webapp:dependencies - Displays all dependencies declared in project ':webapp'.
dependencyInsight - Displays the insight into a specific dependency in root project 'projectReports'.
api:dependencyInsight - Displays the insight into a specific dependency in project ':api'.
webapp:dependencyInsight - Displays the insight into a specific dependency in project ':webapp'.
dependentComponents - Displays the dependent components of components in root project 'projectReports'. [incubating]
api:dependentComponents - Displays the dependent components of components in project ':api'. [incubating]
webapp:dependentComponents - Displays the dependent components of components in project ':webapp'. [incubating]
help - Displays a help message.
api:help - Displays a help message.
webapp:help - Displays a help message.
model - Displays the configuration model of root project 'projectReports'. [incubating]
api:model - Displays the configuration model of project ':api'. [incubating]
webapp:model - Displays the configuration model of project ':webapp'. [incubating]
projects - Displays the sub-projects of root project 'projectReports'.
api:projects - Displays the sub-projects of project ':api'.
webapp:projects - Displays the sub-projects of project ':webapp'.
properties - Displays the properties of root project 'projectReports'.
api:properties - Displays the properties of project ':api'.
webapp:properties - Displays the properties of project ':webapp'.
tasks - Displays the tasks runnable from root project 'projectReports' (some of the displayed tasks may belong to subprojects).
api:tasks - Displays the tasks runnable from project ':api'.
webapp:tasks - Displays the tasks runnable from project ':webapp'.

Other tasks
-----------
api:compile - Compiles the source files
webapp:compile - Compiles the source files
docs - Builds the documentation

4.7.3。显示任务使用细节

运行 gradle help --task someTask 为您提供有关特定任务的详细信息或与多项目构建中的给定任务名称匹配的多个任务。 以下是此详细信息的示例: 例4.13。获取任务的详细帮助 gradle -q help --task libs 命令输出
> gradle -q help --task libs
Detailed task information for libs

Paths
     :api:libs
     :webapp:libs

Type
     Task (org.gradle.api.Task)

Description
     Builds the JAR

Group
     build
该信息包括完整的任务路径,任务类型,可能的命令行选项以及给定任务的描述。

4.7.4。列出项目依赖关系

运行 gradle dependencies 为您列出所选项目的依赖关系,按配置细分。 对于每个配置,该配置的直接和传递依赖关系显示在树中。 以下是此报告的示例:

4.14。获取关于依赖关系的信息 gradle -q dependencies api:dependencies webapp:dependencies 命令输出:

> gradle -q dependencies api:dependencies webapp:dependencies

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

No configurations

------------------------------------------------------------
Project :api - The shared API for the application
------------------------------------------------------------

compile
\--- org.codehaus.groovy:groovy-all:2.4.10

testCompile
\--- junit:junit:4.12
     \--- org.hamcrest:hamcrest-core:1.3

------------------------------------------------------------
Project :webapp - The Web application implementation
------------------------------------------------------------

compile
+--- project :api
|    \--- org.codehaus.groovy:groovy-all:2.4.10
\--- commons-io:commons-io:1.2

testCompile
No dependencies
由于依赖关系报告可能变大,将报告限制为特定配置可能会很有用。 这是通过可选的--configuration参数实现的: 例4.15。通过配置过滤依赖关系报告   gradle -q api:dependencies --configuration testCompile 命令输出:
> gradle -q api:dependencies --configuration testCompile

------------------------------------------------------------
Project :api - The shared API for the application
------------------------------------------------------------

testCompile
\--- junit:junit:4.12
     \--- org.hamcrest:hamcrest-core:1.3

4.7.5。列出项目构建脚本依赖项

运行 gradle buildEnvironment 可以显示所选项目的构建脚本依赖关系,与 gradle dependencies 如何可视化正在构建的软件的依赖关系一样。

4.7.6。了解具体的依赖关系

运行gradle dependencyInsight可以让您深入了解与指定输入匹配的特定依赖关系(或依赖关系)。 以下是此报告的示例: 例4.16。了解具体的依赖关系 gradle -q webapp:dependencyInsight --dependency groovy --configuration compile 命令输出:
> gradle -q webapp:dependencyInsight --dependency groovy --configuration compile
org.codehaus.groovy:groovy-all:2.4.10
\--- project :api
     \--- compile
这个任务对于调查依赖性解析非常有用,找出某些依赖关系来自何处,以及为什么选择某些版本。 有关更多信息,请参阅API文档中的DependencyInsightReportTask类。 内置的dependencyInsight任务是“帮助”任务组的一部分。 该任务需要配置依赖和配置。 该报告将查找与指定配置中指定的依赖关系规范相匹配的依赖关系。 如果应用了Java相关的插件,则dependencyInsight任务是通过“编译”配置进行预配置的,因为通常是我们感兴趣的编译依赖关系,您应该通过命令行“ - 依赖”选项指定感兴趣的依赖关系 。 如果您不喜欢默认值,您可以通过“--configuration”选项选择配置。 有关更多信息,请参阅API文档中的DependencyInsightReportTask类。

4.7.7。 列出项目属性

运行   gradle properties  为您提供所选项目的属性列表。 这是输出的代码段: 例4.17。 有关属性的信息  gradle -q api:properties 命令输出:
> gradle -q api:properties

------------------------------------------------------------
Project :api - The shared API for the application
------------------------------------------------------------

allprojects: [project ':api']
ant: org.gradle.api.internal.project.DefaultAntBuilder@12345
antBuilderFactory: org.gradle.api.internal.project.DefaultAntBuilderFactory@12345
artifacts: org.gradle.api.internal.artifacts.dsl.DefaultArtifactHandler_Decorated@12345
asDynamicObject: DynamicObject for project ':api'
baseClassLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@12345
buildDir: /home/user/gradle/samples/userguide/tutorial/projectReports/api/build
buildFile: /home/user/gradle/samples/userguide/tutorial/projectReports/api/build.gradle

4.7.8。 分析构建任务

--profile命令行选项将在构建运行时记录一些有用的计时信息,并将报告写入build / reports / profile目录。 报告将使用运行构建的时间进行命名。 此报告列出了配置阶段和任务执行的汇总时间和详细信息。 配置和任务执行的时间首先用最昂贵的操作排序。 任务执行结果还表明是否有任何任务被跳过(原因),或者没有跳过的任务没有工作。 使用buildSrc目录的构建将在buildSrc / build目录中生成buildSrc的第二个配置文件报告。

4.8、演习

有时,您对以命令行指定的给定一组任务的顺序执行哪些任务感兴趣,但不希望执行任务。 您可以使用-m选项。 例如,如果你运行“gradle -m clean compile”, 您将看到将作为清理和编译任务的一部分执行的所有任务。 这是任务任务的补充,它显示了可执行的任务。

4.9、总结

在本章中,您已经看到从命令行可以对Gradle做的一些事情。 您可以在 附录D ,Gradle命令行中找到关于gradle命令的更多信息。 原文: https://docs.gradle.org/current/userguide/tutorial_gradle_command_line.html

上一页  |  目录  |  下一页

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值