4 gradle task依赖

来源:互联网 发布:游戏端口被限怎么解块 编辑:程序博客网 时间:2024/06/12 01:30

当构建一个复杂的项目时,不同task之间存在依赖是必然的。比如说,如果想运行'部署'的task,必然要先运行 编译、打包、检测服务器等task,只有当这被些被依赖的task执行完成后,才会部署。对于这种行为之间的依赖,Ant、Maven都提供了声明式的定义,非常简单。同样,使用Gradle定义task之间的依赖也是件很容易的事。

例如,定义如下两个Task,并且在"intro"里加上"dependendsOn"的关键字,如下所示:

task hello << {
    println 'Hello world!'
}
task intro(dependsOn: hello) << {
    println "I'm Gradle" 
}
执行 "gradle intro",结果将是:
Hello World
I'm Gradle

由此可见,当执行gradle intro时,intro依赖的task hello会先被执行。除此之外,dependensOn也支持定义多个task的依赖,使用[]括起来即可。例如

task A(dependensOn:['B','C','D']) <<{ xxx }

除了使用dependensOn跟字符串来定义依赖,我们也可以使用taskX.dependensOn taskY这种形式:

task taskX << {
    println 'taskX'
}
task taskY << {
    println 'taskY'

taskX.dependsOn taskY

或者,也可以使用闭包:

task taskX << {
    println 'taskX'
}
taskX.dependsOn {
    tasks.findAll { task -> task.name.startsWith('lib') }
}
task lib1 << {
    println 'lib1'
}

除了之前讲的task的部分,Gradle还为我们提供了很多可用的API,更多的细节大家可以参考下Gradle的文档。这里我列出一个常用的方法onlyIf。在Gradle里,我们可以使用“OnlyIf()”来决定当前task是否需要被执行,例如:新建build.gradle,加入如下代码:

task hello << {
    println 'hello world'

hello.onlyIf { !project.hasProperty('skipHello') }

当我们直接执行"gradle hello"时,没有任何结果,当我们执行"gradle hello -PskipHello=xxxx"时,会输出"hello world"。

动态任务:

The power of Groovy can be used for more than defining what a task does. For example, you can also use it to dynamically create tasks.
<翻译> Groovy不仅仅能够用来定义一个任务该做什么。比方说,你也可以用它来动态创建任务。

Example 6.8. Dynamic creation of a task
<翻译> 例 6.8.动态创建任务

build.gradle
4.times { counter ->
task "task$counter" << {
    println "I'm task number $counter"
}
}
Output of gradle -q task1
> gradle -q task1
I'm task number 1



 Using Ant Tasks 使用Ant任务

Ant tasks are first-class citizens in Gradle. Gradle provides excellent integration for Ant tasks by simply relying on Groovy. Groovy is shipped with the fantastic AntBuilder. Using Ant tasks from Gradle is as convenient and more powerful than using Ant tasks from a build.xml file. From the example below, you can learn how to execute Ant tasks and how to access Ant properties:
<翻译> Ant任务在Gradle中得到了很好地支持。Gradle通过简单依赖Groovy为Ant任务提供了优化整合。Groovy拥有一个棒极了的AntBuilder。使用来自Gradle的Ant任务作为约定,同时比通过使用来自build.xml的Ant任务更高效。通过下面的示例,你可以学习怎么执行Ant任务和访问Ant属性:

Example 6.13. Using AntBuilder to execute ant.loadfile target
<翻译> 例 6.13.使用AntBuilder执行ant.loadfile目标

build.gradle
task loadfile << {
def files = file('../antLoadfileResources').listFiles().sort()
files.each { File file ->
    if (file.isFile()) {
        ant.loadfile(srcFile: file, property: file.name)
        println " *** $file.name ***"
        println "${ant.properties[file.name]}"
    }
}
}
Output of gradle -q loadfile
> gradle -q loadfile


 Default tasks 默认任务

Gradle allows you to define one or more default tasks for your build.
<翻译> Gradle允许你为你的构建定义一个或多个默认任务。

Example 6.15. Defining a default tasks
<翻译> 例 6.15. 定义一个默认任务

build.gradle
defaultTasks 'clean', 'run'


task clean << {
println 'Default Cleaning!'
}


task run << {
println 'Default Running!'
}


task other << {
println "I'm not a default task!"
}
Output of gradle -q
> gradle -q
Default Cleaning!
Default Running!


Configure by DAG 使用DAG配置

As we later describe in full detail (see Chapter 56, The Build Lifecycle), Gradle has a configuration phase and an execution phase. After the configuration phase, Gradle knows all tasks that should be executed. Gradle offers you a hook to make use of this information. A use-case for this would be to check if the release task is among the tasks to be executed. Depending on this, you can assign different values to some variables.
<翻译> 就想我们之后详细描述那样(看第五十六章“构建生命周期”),Gradle有一个配置阶段和一个执行阶段。在配置阶段后,Gradle知道所有应该被执行的任务。Gradle提供了一个钩子来使用这个信息。一个使用案例将检查发布的任务是否被执行。依此,你可以给一些变量赋不同的值。

In the following example, execution of the distribution and release tasks results in different value of the version variable.
<翻译> 在接下来的示例中,版本变量不同的值时分配和发布任务执行的结果

Example 6.16. Different outcomes of build depending on chosen tasks
<翻译> 例 6.16. 不同的结果取决于选择的构建任务

build.gradle
task distribution << {
println "We build the zip with version=$version"
}


task release(dependsOn: 'distribution') << {
println 'We release now'
}


gradle.taskGraph.whenReady {taskGraph ->
if (taskGraph.hasTask(release)) {
    version = '1.0'
} else {
    version = '1.0-SNAPSHOT'
}
}
Output of gradle -q distribution
> gradle -q distribution
We build the zip with version=1.0-SNAPSHOT
Output of gradle -q release
> gradle -q release
We build the zip with version=1.0
We release now


The important thing is that whenReady affects the release task before the release task is executed. This works even when the release task is not the primary task (i.e., the task passed to the gradle command).
<翻译> 重要的是whenReady的影响在发布任务执行之前。当发布任务不是首要任务时也会奏效。(比如,任务传递给gradle命令)





0 0
原创粉丝点击