课程表

Gradle课程

工具箱
速查手册

Gradle 命令行的基本使用

当前位置:免费教程 » 软件/图像 » Gradle

本章介绍了命令行的基本使用。正如在前面的章节里你所见到的调用 gradle 命令来完成一些功能。

多任务调用

你可以以列表的形式在命令行中一次调用多个任务。例如 gradle compile test 命令会依次调用,并且每个任务仅会被调用一次。compile 和 test 任务以及它们的依赖任务。无论它们是否被包含在脚本中:即无论是以命令行的形式定义的任务还是依赖于其它任务都会被调用执行。来看下面的例子。

下面定义了四个任务。dist 和 test 都依赖于 compile,只用当 compile 被调用之后才会调用 gradle dist test 任务。

任务依赖

多任务调用

build.gradle

  1. task compile << {
  2. println 'compiling source'
  3. }
  4. task compileTest(dependsOn: compile) << {
  5. println 'compiling unit tests'
  6. }
  7. task test(dependsOn: [compile, compileTest]) << {
  8. println 'running unit tests'
  9. }
  10. task dist(dependsOn: [compile, test]) << {
  11. println 'building the distribution'
  12. }

gradle dist test 的输出结果。

  1. \> gradle dist test
  2. :compile
  3. compiling source
  4. :compileTest
  5. compiling unit tests
  6. :test
  7. running unit tests
  8. :dist
  9. building the distribution
  10. BUILD SUCCESSFUL
  11. Total time: 1 secs

由于每个任务仅会被调用一次,所以调用 gradle test test 与调用 gradle test 效果是相同的。

排除任务

你可以用命令行选项 -x 来排除某些任务,让我们用上面的例子来示范一下。

排除任务

gradle dist -x test 的输出结果。

  1. \> gradle dist -x test
  2. :compile
  3. compiling source
  4. :dist
  5. building the distribution
  6. BUILD SUCCESSFUL
  7. Total time: 1 secs

可以看到,test 任务并没有被调用,即使他是 dist 任务的依赖。同时 test 任务的依赖任务 compileTest 也没有被调用,而像 compile 被 test 和其它任务同时依赖的任务仍然会被调用。

失败后继续执行

默认情况下只要有任务调用失败 Gradle 就是中断执行。这可能会使调用过程更快,但那些后面隐藏的错误不会被发现。所以你可以使用--continue 在一次调用中尽可能多的发现所有问题。

采用了--continue 选项,Gralde会调用每一个任务以及它们依赖的任务。而不是一旦出现错误就会中断执行。所有错误信息都会在最后被列出来。

一旦某个任务执行失败,那么所有依赖于该任务的子任务都不会被调用。例如由于 test 任务依赖于 complie 任务,所以如果 compile 调用出错,test 便不会被直接或间接调用。

简化任务名

当你试图调用某个任务的时候,无需输入任务的全名。只需提供足够的可以唯一区分出该任务的字符即可。例如,上面的例子你也可以这么写。用 gradle di 来直接调用 dist 任务。

简化任务名

gradle di 的输出结果

  1. \> gradle di
  2. :compile
  3. compiling source
  4. :compileTest
  5. compiling unit tests
  6. :test
  7. running unit tests
  8. :dist
  9. building the distribution
  10. BUILD SUCCESSFUL
  11. Total time: 1 secs

你也可以用驼峰命名的任务中每个单词的首字母进行调用。例如,可以执行 gradle compTest or even gradle cT 来调用 compileTest 任务。

简化驼峰任务名

gradle cT 的输出结果。

  1. \> gradle cT
  2. :compile
  3. compiling source
  4. :compileTest
  5. compiling unit tests
  6. BUILD SUCCESSFUL
  7. Total time: 1 secs

简化后你仍然可以使用 -x 参数。

选择构建位置

调用 gradle 时,默认情况下总是会构建当前目录下的文件,可以使用-b 参数选择构建的文件,并且当你使用此参数时settings。gradle 将不会生效,看下面的例子:

选择文件构建

subdir/myproject.gradle

  1. task hello << {
  2. println "using build file '$buildFile.name' in '$buildFile.parentFile.name'."
  3. }

gradle -q -b subdir/myproject.gradle hello 的输出结果

  1. Output of gradle -q -b subdir/myproject.gradle hello
  2. \> gradle -q -b subdir/myproject.gradle hello
  3. using build file 'myproject.gradle' in 'subdir'.

另外,你可以使用 -p 参数来指定构建的目录,例如在多项目构建中你可以用 -p 来替代 -b 参数。

选择构建目录

gradle -q -p subdir hello 的输出结果

  1. \> gradle -q -p subdir hello
  2. using build file 'build.gradle' in 'subdir'.

获取构建信息

Gradle 提供了许多内置任务来收集构建信息。这些内置任务对于了解依赖结构以及解决问题都是很有帮助的。

了解更多,可以参阅项目报告插件以为你的项目添加构建报告。

项目列表

执行 gradle projects 会为你列出子项目名称列表。如下例。

收集项目信息

gradle -q projects 的输出结果

  1. \> gradle -q projects
  2. \------------------------------------------------------------
  3. Root project
  4. \------------------------------------------------------------
  5. Root project 'projectReports'
  6. +--- Project ':api' - The shared API for the application
  7. \--- Project ':webapp' - The Web application implementation
  8. To see a list of the tasks of a project, run gradle <project-path>:tasks
  9. For example, try running gradle :api:tasks

这份报告展示了每个项目的描述信息。当然你可以在项目中用 description 属性来指定这些描述信息。

为项目添加描述信息.

build.gradle

  1. description = 'The shared API for the application'

任务列表

执行 gradle tasks 会列出项目中所有任务。这会显示项目中所有的默认任务以及每个任务的描述。如下例

获取任务信息

gradle -q tasks的输出结果:

  1. \> gradle -q tasks
  2. \------------------------------------------------------------
  3. All tasks runnable from root project
  4. \------------------------------------------------------------
  5. Default tasks: dists
  6. Build tasks
  7. \-----------
  8. clean - Deletes the build directory (build)
  9. dists - Builds the distribution
  10. libs - Builds the JAR
  11. Build Setup tasks
  12. \-----------------
  13. init - Initializes a new Gradle build. [incubating]
  14. wrapper - Generates Gradle wrapper files. [incubating]
  15. Help tasks
  16. \----------
  17. dependencies - Displays all dependencies declared in root project 'projectReports'.
  18. dependencyInsight - Displays the insight into a specific dependency in root project 'projectReports'.
  19. help - Displays a help message
  20. projects - Displays the sub-projects of root project 'projectReports'.
  21. properties - Displays the properties of root project 'projectReports'.
  22. tasks - Displays the tasks runnable from root project 'projectReports' (some of the displayed tasks may belong to subprojects).
  23. To see all tasks and more detail, run with --all.

默认情况下,这只会显示那些被分组的任务。你可以通过为任务设置 group 属性和 description 来把 这些信息展示到结果中。

更改任务报告内容

build.gradle

  1. dists {
  2. description = 'Builds the distribution'
  3. group = 'build'
  4. }

当然你也可以用--all 参数来收集更多任务信息。这会列出项目中所有任务以及任务之间的依赖关系。

Obtaining more information about tasks

gradle -q tasks --all的输出结果:

  1. \> gradle -q tasks --all
  2. \------------------------------------------------------------
  3. All tasks runnable from root project
  4. \------------------------------------------------------------
  5. Default tasks: dists
  6. Build tasks
  7. \-----------
  8. clean - Deletes the build directory (build)
  9. api:clean - Deletes the build directory (build)
  10. webapp:clean - Deletes the build directory (build)
  11. dists - Builds the distribution [api:libs, webapp:libs]
  12. docs - Builds the documentation
  13. api:libs - Builds the JAR
  14. api:compile - Compiles the source files
  15. webapp:libs - Builds the JAR [api:libs]
  16. webapp:compile - Compiles the source files
  17. Build Setup tasks
  18. \-----------------
  19. init - Initializes a new Gradle build. [incubating]
  20. wrapper - Generates Gradle wrapper files. [incubating]
  21. Help tasks
  22. \----------
  23. dependencies - Displays all dependencies declared in root project 'projectReports'.
  24. dependencyInsight - Displays the insight into a specific dependency in root project 'projectReports'.
  25. help - Displays a help message
  26. projects - Displays the sub-projects of root project 'projectReports'.
  27. properties - Displays the properties of root project 'projectReports'.
  28. tasks - Displays the tasks runnable from root project 'projectReports' (some of the displayed tasks may belong to subprojects).

获取任务帮助信息

执行 gradle help --task someTask 可以显示指定任务的详细信息。或者多项目构建中相同任务名称的所有任务的信息。如下例。

获取任务帮助

gradle -q help --task libs 的输出结果

  1. \> gradle -q help --task libs
  2. Detailed task information for libs
  3. Paths
  4. :api:libs
  5. :webapp:libs
  6. Type
  7. Task (org.gradle.api.Task)
  8. Description
  9. Builds the JAR

这些结果包含了任务的路径、类型以及描述信息等。

获取依赖列表

执行 gradle dependencies 会列出项目的依赖列表,所有依赖会根据任务区分,以树型结构展示出来。如下例。

获取依赖信息

gradle -q dependencies api:dependencies webapp:dependencies 的输出结果

  1. \> gradle -q dependencies api:dependencies webapp:dependencies
  2. \------------------------------------------------------------
  3. Root project
  4. \------------------------------------------------------------
  5. No configurations
  6. \------------------------------------------------------------
  7. Project :api - The shared API for the application
  8. \------------------------------------------------------------
  9. compile
  10. \--- org.codehaus.groovy:groovy-all:2.2.0
  11. testCompile
  12. \--- junit:junit:4.11
  13. \--- org.hamcrest:hamcrest-core:1.3
  14. \------------------------------------------------------------
  15. Project :webapp - The Web application implementation
  16. \------------------------------------------------------------
  17. compile
  18. +--- project :api
  19. | \--- org.codehaus.groovy:groovy-all:2.2.0
  20. \--- commons-io:commons-io:1.2
  21. testCompile
  22. No dependencies

虽然输出结果很多,但这对于了解构建任务十分有用,当然你可以通过--configuration 参数来查看 指定构建任务的依赖情况。

过滤依赖信息

gradle -q api:dependencies --configuration testCompile 的输出结果

  1. \> gradle -q api:dependencies --configuration testCompile
  2. \------------------------------------------------------------
  3. Project :api - The shared API for the application
  4. \------------------------------------------------------------
  5. testCompile
  6. \--- junit:junit:4.11
  7. \--- org.hamcrest:hamcrest-core:1.3

查看特定依赖

执行 Running gradle dependencyInsight 可以查看指定的依赖情况。如下例。

获取特定依赖

gradle -q webapp:dependencyInsight --dependency groovy --configuration compile 的输出结果

  1. \> gradle -q webapp:dependencyInsight --dependency groovy --configuration compile
  2. org.codehaus.groovy:groovy-all:2.2.0
  3. \--- project :api
  4. \--- compile

这对于了解依赖关系、了解为何选择此版本作为依赖十分有用。了解更多请参阅依赖检查报告

dependencyInsight 任务是'Help'任务组中的一个。这项任务需要进行配置才可以。如果用了 Java 相关的插件,那么 dependencyInsight 任务已经预先被配置到'Compile'下了。你只需要通过'--dependency'参数来制定所需查看的依赖即可。如果你不想用默认配置的参数项你可以通过 '--configuration' 参数来进行指定。了解更多请参阅依赖检查报告

获取项目属性列表

执行 gradle properties 可以获取项目所有属性列表。如下例。

属性信息

gradle -q api:properties 的输出结果

  1. \> gradle -q api:properties
  2. \------------------------------------------------------------
  3. Project :api - The shared API for the application
  4. \------------------------------------------------------------
  5. allprojects: [project ':api']
  6. ant: org.gradle.api.internal.project.DefaultAntBuilder@12345
  7. antBuilderFactory: org.gradle.api.internal.project.DefaultAntBuilderFactory@12345
  8. artifacts: org.gradle.api.internal.artifacts.dsl.DefaultArtifactHandler@12345
  9. asDynamicObject: org.gradle.api.internal.ExtensibleDynamicObject@12345
  10. buildDir: /home/user/gradle/samples/userguide/tutorial/projectReports/api/build
  11. buildFile: /home/user/gradle/samples/userguide/tutorial/projectReports/api/build.gradle

构建日志

--profile 参数可以收集一些构建期间的信息并保存到 build/reports/profile 目录下并且以构建时间命名这些文件。

下面这份日志记录了总体花费时间以及各过程花费的时间,并以时间大小倒序排列,并且记录了任务的执行情况。

如果采用了 buildSrc,那么在 buildSrc/build 下同时也会生成一份日志记录记录。

Dry Run

有时可能你只想知道某个任务在一个任务集中按顺序执行的结果,但并不想实际执行这些任务。那么你可以用-m 参数。例如 gradle -m clean compile 会调用 clean 和 compile,这与 tasks 可以形成互补,让你知道哪些任务可以用于执行。

本章小结

在本章中你学到很多用命令行可以做的事情,了解更多你可以参阅 gradle command in 附录 D, Gradle 命令行

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号