何时使用“<<”>在Gradle任务中 [英] When to use "<<" in Gradle task

查看:94
本文介绍了何时使用“<<”>在Gradle任务中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时我会看到:

 任务hey<< {
println你好,Gradle!
}

其他时间我看到:

 任务hey {
printlnHello,Gradle!
}

何时使用<< code>以及您何时没有(以及为什么)?

解决方案

<<< ; doLast 的简写。

这项任务:

 任务嘿<< {
println你好,Gradle!
}

等同于此任务:

 任务hey {
doLast {
printlnHello,Gradle!




$ b $ p
$ b

第二个例子中的代码每次执行无论您是否正在运行该特定任务,都会执行构建脚本。例如,如果您有以下任务,然后运行 gradle goodbye ,则会看到Hello,World!和再见,世界!即使您只执行再见任务:

 任务hello {
printlnHello,world!
}

任务再见{
println再见,世界!
}

结果:

  $ gradle goodbye 
您好,世界!
再见,世界!
:goodbye UP-TO-DATE

但是,如果您更新了任务定义以使用< doLast (例如任务hello<< {} code>),你只能从你执行的任务中看到 println



Adam Murdoch描述当任务中的代码将在这篇文章。我在此引用了一些相关信息:


当与任务相关的代码是$ b $时,有两个不同的时间点b执行:第一个是配置时间,即构建
脚本执行的时间。这个想法是,在这个时候你配置任务
,以便在另一个时间点做正确的事情,即
的执行时间。执行时间是任务
完成其实际工作的时间点,并且只有在为执行
选定任务并执行相关性后才会发生。



每个任务都有一系列操作,这些操作按任务执行时指定的
顺序运行。一个行动只是一个关闭或一个
行动实施。 doFirst()方法将任务配置为在序列的开头添加
动作。 doLast()和<<方法
将任务配置为在序列结尾处添加动作。



Sometimes I see:

task hey << {
    println "Hello, Gradle!"
}

Other times I see:

task hey {
    println "Hello, Gradle!"
}

When do use "<<" and when do you not (and why)?

解决方案

<< is shorthand for doLast. The tasks in your question are not equivalent.

This task:

task hey << {
    println "Hello, Gradle!"
}

is equivalent to this task:

task hey {
    doLast {
        println "Hello, Gradle!"
    }
}

The code in your second example will execute every time the build script executes, regardless of whether or not you are running that specific task. For example, if you had the following tasks and then ran gradle goodbye, you would see both "Hello, World!" and "Goodbye, World!" even though you are only executing the "goodbye" task:

task hello {
    println "Hello, world!"
}

task goodbye {
    println "Goodbye, world!"
}

Results:

$ gradle goodbye
Hello, world!
Goodbye, world!
:goodbye UP-TO-DATE

However, if you updated the task definitions to use << or doLast (e.g. task hello << {}), you would only see the println from the task you executed.

Adam Murdoch describes when the code in a task will be executed in this post. I've quoted some of the relevant information here:

There are 2 different points in time when code related to a task is executed: The first is configuration time, which is when the build script executes. The idea is that at this time you configure the task, so that it does the right thing at the other point in time, namely, execution time. Execution time is the point in time where the task does its actual work, and happens only if the task is selected for execution, and after its dependencies have executed.

Each task has a sequence of actions, which are run in the order specified when the task executes. An action is simply a closure or an Action implementation. The doFirst() method configures the task to add an action at the start of the sequence. The doLast() and << methods configure the task to add an action at the end of the sequence.

这篇关于何时使用“&lt;&lt;”&gt;在Gradle任务中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆