当前位置:K88软件开发文章中心编程工具Gradle → 文章内容

Gradle Gradle 命令行的基本使用

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-24 10:31:23

由 珍珍阿姨 创建, 最后一次修改 2016-08-12 Gradle 命令行的基本使用本章介绍了命令行的基本使用。正如在前面的章节里你所见到的调用 gradle 命令来完成一些功能。多任务调用你可以以列表的形式在命令行中一次调用多个任务。例如 gradle compile test 命令会依次调用,并且每个任务仅会被调用一次。compile 和 test 任务以及它们的依赖任务。无论它们是否被包含在脚本中:即无论是以命令行的形式定义的任务还是依赖于其它任务都会被调用执行。来看下面的例子。下面定义了四个任务。dist 和 test 都依赖于 compile,只用当 compile 被调用之后才会调用 gradle dist test 任务。任务依赖多任务调用build.gradletask 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'}gradle dist test 的输出结果。\> gradle dist test:compilecompiling source:compileTestcompiling unit tests:testrunning unit tests:distbuilding the distributionBUILD SUCCESSFULTotal time: 1 secs由于每个任务仅会被调用一次,所以调用 gradle test test 与调用 gradle test 效果是相同的。排除任务你可以用命令行选项 -x 来排除某些任务,让我们用上面的例子来示范一下。排除任务gradle dist -x test 的输出结果。\> gradle dist -x test:compilecompiling source:distbuilding the distributionBUILD SUCCESSFULTotal time: 1 secs可以看到,test 任务并没有被调用,即使他是 dist 任务的依赖。同时 test 任务的依赖任务 compileTest 也没有被调用,而像 compile 被 test 和其它任务同时依赖的任务仍然会被调用。失败后继续执行默认情况下只要有任务调用失败 Gradle 就是中断执行。这可能会使调用过程更快,但那些后面隐藏的错误不会被发现。所以你可以使用--continue 在一次调用中尽可能多的发现所有问题。采用了--continue 选项,Gralde会调用每一个任务以及它们依赖的任务。而不是一旦出现错误就会中断执行。所有错误信息都会在最后被列出来。一旦某个任务执行失败,那么所有依赖于该任务的子任务都不会被调用。例如由于 test 任务依赖于 complie 任务,所以如果 compile 调用出错,test 便不会被直接或间接调用。简化任务名当你试图调用某个任务的时候,无需输入任务的全名。只需提供足够的可以唯一区分出该任务的字符即可。例如,上面的例子你也可以这么写。用 gradle di 来直接调用 dist 任务。简化任务名gradle di 的输出结果\> gradle di:compilecompiling source:compileTestcompiling unit tests:testrunning unit tests:distbuilding the distributionBUILD SUCCESSFULTotal time: 1 secs你也可以用驼峰命名的任务中每个单词的首字母进行调用。例如,可以执行 gradle compTest or even gradle cT 来调用 compileTest 任务。简化驼峰任务名gradle cT 的输出结果。\> gradle cT:compilecompiling source:compileTestcompiling unit testsBUILD SUCCESSFULTotal time: 1 secs简化后你仍然可以使用 -x 参数。选择构建位置调用 gradle 时,默认情况下总是会构建当前目录下的文件,可以使用-b 参数选择构建的文件,并且当你使用此参数时settings。gradle 将不会生效,看下面的例子:选择文件构建subdir/myproject.gradletask hello << { println "using build file '$buildFile.name' in '$buildFile.parentFile.name'."}gradle -q -b subdir/myproject.gradle hello 的输出结果Output of gradle -q -b subdir/myproject.gradle hello\> gradle -q -b subdir/myproject.gradle hellousing build file 'myproject.gradle' in 'subdir'.另外,你可以使用 -p 参数来指定构建的目录,例如在多项目构建中你可以用 -p 来替代 -b 参数。选择构建目录gradle -q -p subdir hello 的输出结果\> gradle -q -p subdir hellousing build file 'build.gradle' in 'subdir'.获取构建信息Gradle 提供了许多内置任务来收集构建信息。这些内置任务对于了解依赖结构以及解决问题都是很有帮助的。了解更多,可以参阅项目报告插件以为你的项目添加构建报告。项目列表执行 gradle projects 会为你列出子项目名称列表。如下例。收集项目信息gradle -q projects 的输出结果\> gradle -q projects\------------------------------------------------------------Root project\------------------------------------------------------------Root project 'projectReports'+--- Project ':api' - The shared API for the application\--- Project ':webapp' - The Web application implementationTo see a list of the tasks of a project, run gradle <project-path>:tasksFor example, try running gradle :api:tasks这份报告展示了每个项目的描述信息。当然你可以在项目中用 description 属性来指定这些描述信息。为项目添加描述信息.build.gradledescription = 'The shared API for the application'任务列表执行 gradle tasks 会列出项目中所有任务。这会显示项目中所有的默认任务以及每个任务的描述。如下例获取任务信息gradle -q tasks的输出结果:\> gradle -q tasks\------------------------------------------------------------All tasks runnable from root project\------------------------------------------------------------Default tasks: distsBuild tasks\-----------clean - Deletes the build directory (build)dists - Builds the distributionlibs - Builds the JARBuild Setup tasks\-----------------init - Initializes a new Gradle build. [incubating]wrapper - Generates Gradle wrapper files. [incubating]Help tasks\----------dependencies - Displays all dependencies declared in root project 'projectReports'.dependencyInsight - Displays the insight into a specific dependency in root project 'projectReports'.help - Displays a help messageprojec

[1] [2] [3]  下一页


Gradle Gradle 命令行的基本使用