gradle学习笔记(二)第一个简单demo

Chapter 4. Using the Gradle Command-Line
使用gradle命令行

This chapter introduces the basics of the Gradle command-line. You run a build using the gradle command, which you have already seen in action in previous chapters.
本篇文章介绍gradle的命令行基础,使用gradle命令开始一个例子,前面文章提到的任意一个。(靠,前面文章是哪里!!?难道我错过了什么?好吧,我是从安装那一步开始看的,貌似现在是文章4了,那好吧,就直接从samples中找一个吧,坑爹!)

4.1. Executing multiple tasks(执行多任务)

You can execute multiple tasks in a single build by listing each of the tasks on the command-line. For example, the command gradle compile test will execute the compile and test tasks. Gradle will execute the tasks in the order that they are listed on the command-line, and will also execute the dependencies for each task. Each task is executed once only, regardless of how it came to be included in the build: whether it was specified on the command-line, or as a dependency of another task, or both. Let’s look at an example.

大概的意思是,你可以通过在单个的build中列举出多个任务来执行多任务。例如,使用gradle开始compile和test任务将会执行编译和测试任务。gradle将会执行这些任务按照他们在命令行中的顺序,也将会执行每个任务的依赖任务(这个看起来和maven、ant差不多的样子)。每个任务会被执行一次,不管它是怎么被包含在build中的:无论是被指定到命令行上,还是作为另一个任务的依赖项,或者两者一起。让我们看一个例子。

Below four tasks are defined. Both dist and test depend on the compile task. Running gradle dist test for this build script results in the compile task being executed only once.

官方图片
下面声明了四个任务,dist和test两个任务都依赖于compile任务。用gradle dist test执行这个build(构建)脚本,结果将会是这个编译任务只执行一次!

Example 4.1. Executing multiple tasks
build.gradle


task compile << {
    println '以java的名义召唤,出来吧,来自地狱的家伙,哥斯拉!^_^`'
}

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'
}

这里需要说明一下,有些童鞋看到这里可能有点郁闷了,这个代码该怎么用,无从下手!特别是基础知识不够扎实的童鞋更会迷茫了。学习过ant或者javac命令的同学应该都知道,执行脚本的时候,需要设置各种环境变量,比如java命令编译java文件的时候,可能需要设置一下classpath,其实也就是让系统知道脚本在哪里,同样的道理,gradle应该也是这样的,那么首先在自己的硬盘上建一个文件夹,然后新建一个txt文本文档,将上方的代码复制下来粘贴进去保存,最后改一下这个txt文档的名字和后缀为build.gradle,然后打开命令提示符用cd命令切到该目录,执行gradle test dist就可以看到跟官网上说的一样的效果了。
这里写图片描述

4.2. Excluding tasks(排除任务)

You can exclude a task from being executed using the -x command-line option and providing the name of the task to exclude. Let’s try this with the sample build file above.

你可以使用-x命令操作将某个制定名字的任务排除。让我们试一下上面的那个例子。
直接命令行输入gradle dist -x test
这里写图片描述

官网上说
You can see from the output of this example, that the test task is not executed, even though it is a dependency of the dist task. You will also notice that the test task’s dependencies, such as compileTest are not executed either. Those dependencies of test that are required by another task, such as compile, are still executed.
可以看到例子的输出结果,test任务没有被执行,即便是它是dist任务的一个依赖任务。你应该也注意到了test任务的依赖任务,比如compileTest也没有被执行了。那些test的依赖任务又被另一个任务所依赖的任务,像compile,仍然被执行了。(真是绕人,简单说就是test任务被忽略了,test任务的依赖项目也会被忽略,但是如果test的某些依赖任务被另外一个没被忽略的任务所依赖,那么这些依赖任务仍然会执行。)

4.3. Continuing the build when a failure occurs(当发生错误时继续build)

By default, Gradle will abort execution and fail the build as soon as any task fails. This allows the build to complete sooner, but hides other failures that would have occurred. In order to discover as many failures as possible in a single build execution, you can use the - -continue option.

默认情况下,一旦任何一个任务发生了错误,gradle将会终止执行正在执行的任务。这种机制能够帮助我们更快的完善整个build过程(毕竟遇到一个错就抛出错误,不会往下执行浪费时间了嘛),但是它会隐藏其他的将会发生的错误(就是下面可能发生的错误)。为了在一个单一的build中尽可能的发现更多的错误,你可以使用 - -continue 操作。(其实就是说,默认情况下一发生错误就会中断执行,如果想一下子把所有错误都列举出来就加上- -continue这个鬼东西)

When executed with - -continue, Gradle will execute every task to be executed where all of the dependencies for that task completed without failure, instead of stopping as soon as the first failure is encountered. Each of the encountered failures will be reported at the end of the build.

当使用–continue执行的时候,gradle将会执行每一个任务、所有的依赖任务,没有错误的完成这个build,代替了一发生第一个错误就停止这种机制。每一个发生的错误都会在这个build的结尾报告出来。

If a task fails, any subsequent tasks that were depending on it will not be executed, as it is not safe to do so. For example, tests will not run if there is a compilation failure in the code under test; because the test task will depend on the compilation task (either directly or indirectly).

如果一个任务出错了,任何后面的任务以及它的依赖任务都不会被执行,因为这样做是不安全的。例如,代码编译的时候出错,测试则不会运行了;因为这个测试任务依赖于编译任务(直接或者间接依赖)(这些外国人就是蛋疼,非要在最后加上这样一段,跟前面一段完全没关系,还是在说,一个任务停止了就不往下执行了。)

猜你喜欢

转载自blog.csdn.net/q979076061/article/details/50469380