为什么gradle运行gradle.build中的每个任务 [英] Why does gradle run every task in gradle.build

查看:162
本文介绍了为什么gradle运行gradle.build中的每个任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是gradle的新手,并有一个基本问题。

I'm very new to gradle and have a basic question.

当我向gradle.build文件中添加自定义任务并调用 gradlw build时, gradle clean或任何其他gradle命令,
它会自动运行我的自定义任务。

When I add a custom task to my gradle.build file and call "gradlw build" or "gradle clean" or any other gradle command, it automatically runs my custom task.

在gradle中事情是如何工作的?运行构建文件中的每个任务?
是否只有在我手动需要时才可以运行任务?

Is that how things work in gradle? run every task in the build file? Is there way to run a task only when I want it manually?

推荐答案

task foo {
  println 'hello'
}

任务,并在任务的配置期间,告诉gradle执行 println'hello'。每个任务都在每次构建时都进行了配置,因为gradle需要知道其配置是什么,以便知道是否必须执行任务。

That creates a task, and during the configuration of the task, it tells gradle to execute println 'hello'. Every task is configured at each build, because gradle needs to know what its configuration is to know if the task must be executed or not.

task foo << {
  println 'hello'
}

这会创建一个任务,并在执行,它告诉gradle执行 println'hello'。因此,只有在您明确选择运行任务 foo 或依赖于 foo 的任务时,才会执行代码。

That creates a task, and during the execution of the task, it tells gradle to execute println 'hello'. So the code will only be executed if you explicitly chose to run the task foo, or a task that depends on foo.

等效于

task foo {
  doLast {
    println 'hello'
  }
}

您选择不发布您的代码,可能假设gradle行为异常,并且您的代码与该问题无关。因此,这只是一个猜测,但是您可能使用了第一个不正确的代码,而不是第二个正确的代码。

You chose not to post your code, probably assuming that gradle was acting bizarrely, and that your code had nothing to do with the problem. So this is just a guess, but you probably used the first incorrect code rather than the second, correct one.

这篇关于为什么gradle运行gradle.build中的每个任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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