zoukankan      html  css  js  c++  java
  • Gradle笔记系列(二)

    1、使用Gradle命令行

      在这篇博客中,我们将简要介绍Gradle命令行的使用。

    1.1 执行多任务

      通过在命令行列出每个任务(task),你可以在一次构建(build)中执行多个任务。例如,命令gradle compile test会执行compile和test这两个任务,Gradle按照任务在命令行列出的顺序依次执行每个任务,同时也会执行每个任务的依赖任务。每个任务不管如何被包含在build中,它只会被执行一次(不管它是被指定在命令行中,或者是作为另一个task的依赖出现,或者两者兼有)。看一个例子,下图列出了4个任务,其中dist和test都依赖于compile。

     

    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 }
    build.gradle

    执行命令gradle dist test,输出结果如下

    C:UsersAdministratorDesktopgradle>gradle dist test
    :compile
    compiling source
    :compileTest
    compiling unit tests
    :test
    running unit tests
    :dist
    building the distribution
    
    BUILD SUCCESSFUL
    
    Total time: 2.012 secs

    从上面输出结果可以看出,每个任务只被执行了1次,因此gradle test testgradle test效果完全一样。

    1.2 移除任务

      你可以通过-x命令参数移除task,依然以上述代码为例,下面是测试结果输出

    C:UsersAdministratorDesktopgradle>gradle dist -x test
    :compile
    compiling source
    :dist
    building the distribution
    
    BUILD SUCCESSFUL
    
    Total time: 1.825 secs

      通过上述输出结果可以看出,任务test没有被执行,即便它被dist所依赖。同时你也会注意到任务test所依赖的任务之一compileTest也没有被执行,同时被test和dist所依赖的任务(如compile)仍然被执行了。

      1.3 出错时的继续build

      默认情况下,当gradle构建过程中遇到任何task执行失败时将会立刻中断。这样运行你马上去解决这个构建错误,但是这样也会隐藏可能会发生的其他错误。为了在一次执行过程中尽可能暴露多的错误,你可以使用--continue选项。当你执行命令的时候加上了--continue选项,Gradle将会执行每个任务的所有依赖,而不管失败的发生,而不是在第一个错误发生时就立马停止。在执行过程中所发生的所有错误在构建的结尾都会被列出来。如果一个任务失败了,随后其他依赖于这个任务的子任务,这么做是不安全的。例如,如果在test代码里面有一个编译错误,test任务将不会执行;由于test任务依赖于compilation任务(不管是直接还是间接)。

    1.4 简化task名字

       在命令行指定task时,我们不需要给出任务的全名,只需要给出能够足够唯一标识某个任务的简化的名字即可。例如,在上面的例子中,我们可以使用gradle di命令来执行task dist。此外,当task名是由多于1个的单词构成时,我们也可以使用每个单词的首字母作为task名的简化形式。比如一个名字为compileTest的task,我们可以使用命令:gradle cT来进行编译(或者使用gradle compTest)。简化后的task名字也适用-x选项。

    1.5 选择性的执行某个build文件

      当我们执行gradle命令时,会在当前路径下寻找build文件。可以使用-b选项指定另一个build文件。如果指定了-b选项,那么settings.gradle文件将不会起作用。看例子:

    subdir/myproject.gradle:

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

      执行命令:gradle -q -b subdir/myproject.gradle hello输出结果如下:

    using build file 'myproject.gradle' in 'subdir'.

      或者,可以使用-p选项指定要使用的工程目录。对多工程编译时,应该使用-p选项而不是-b选项。例子:

      subdir/build.gradle

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

      执行命令:gradle -q -p subdir hello输出结果如下:

    using build file 'build.gradle' in 'subdir'.

    如果使用其它构建文件名,可与-b一起使用:gradle -q -p subdir -b subdir/myproject.gradle hello

  • 相关阅读:
    控制台布局编程小结
    代码健壮、测试的必要性
    《Scott Mitchell 的ASP.NET 2.0数据教程》之二 BLL层 学习过程中的问题
    阅读张孝祥的《大胆尝试随需消费的软件培训模式》后感触良多
    Northwind 数据库相关练习
    学习Membership
    WEB标准化参考资料
    商学院实验班I期 5月 教学计划
    怎样设计和创建数据库?
    PathMatchingResourcePatternResolver
  • 原文地址:https://www.cnblogs.com/e007/p/5601358.html
Copyright © 2011-2022 走看看