我可以从.gradle文件中的groovy方法调用gradle任务吗? [英] Can i call a gradle task from a groovy method in a .gradle file?

查看:187
本文介绍了我可以从.gradle文件中的groovy方法调用gradle任务吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在gradle中有一个非常规的构建脚本,它可以循环编译项目。
这将需要数周时间才能更改为标准gradle构建,因此现在不会发生。



问题是我想停止在脚本中使用ant并移动仅使用groovy + gradle。
问题是如何更改副本等任务? replaceregexp?解压缩?
我假设在standart gradle插件和java插件中,我拥有大部分我需要的东西,但它们都是任务,现在如果我在我的主任务中需要复制操作的方法,我该如何解决?
有没有办法调用任务代码?有没有一种方法可以从groovy脚本调用任务本身?

首先,你应该从不从另一个任务调用任务 - 如果你这样做会发生坏事。相反,您应该声明两个任务之间的关系( dependsOn mustRunAfter finalizedBy )。 在某些情况下(比人们倾向于认为的少),链接任务可能不够灵活;因此,对于某些任务(例如 Copy ),提供了一个等效的方法(例如 project.copy )。但是,应谨慎使用这些方法。在许多情况下,任务是更好的选择,因为它们是Gradle的基本组成部分,并提供许多优点(例如自动最新检查)。

有时使用 GradleBuild 任务也是有意义的,它允许将一个版本作为另一个版本的一部分执行。

I have a non conventional build script in gradle that does cyclic compiling of projects. It's gonna take weeks to change to standard gradle build so that's not gonna happen now.

The issue is I want to stop using ant in my script and move to use groovy + gradle only. The question is how to change tasks like copy ? replaceregexp ? unzip ? I assume in the standart gradle plugin and java plugin I have most of what i need, but they are all tasks, now if i have in my main task a method that needs copy operation, how do I got about ? Is there a way to call the tasks code ? Is there a way to call the task itself from a groovy script ?

解决方案

First of all, you should never call a task from another task - bad things will happen if you do. Instead, you should declare a relationship between the two tasks (dependsOn, mustRunAfter, finalizedBy).

In some cases (fewer than people tend to believe), chaining tasks may not be flexible enough; hence, for some tasks (e.g. Copy) an equivalent method (e.g. project.copy) is provided. However, these methods should be used with care. In many cases, tasks are the better choice, as they are the basic building blocks of Gradle and offer many advantages (e.g. automatic up-to-date checks).

Occasionally it also makes sense to use a GradleBuild task, which allows to execute one build as part of another.

这篇关于我可以从.gradle文件中的groovy方法调用gradle任务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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