简体   繁体   English

我可以从.gradle文件中的groovy方法调用gradle任务吗?

[英]Can i call a gradle task from a groovy method in a .gradle file?

I have a non conventional build script in gradle that does cyclic compiling of projects. 我在gradle中有一个非常规的构建脚本,可以循环编译项目。 It's gonna take weeks to change to standard gradle build so that's not gonna happen now. 这需要数周才能改成标准的gradle构建,所以现在不会发生。

The issue is I want to stop using ant in my script and move to use groovy + gradle only. 问题是我想在我的脚本中停止使用ant并且仅使用groovy + gradle。 The question is how to change tasks like copy ? 问题是如何改变像复制这样的任务? replaceregexp ? 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 ? 我假设在标准gradle插件和java插件中我拥有我需要的大部分内容,但它们都是任务,现在如果我在我的主要任务中有一个需要复制操作的方法,我该怎么办? Is there a way to call the tasks code ? 有没有办法调用任务代码? Is there a way to call the task itself from a groovy script ? 有没有办法从一个groovy脚本调用任务本身?

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 ). 相反,您应该声明两个任务之间的关系( dependsOnmustRunAfterfinalizedBy )。

In some cases (fewer than people tend to believe), chaining tasks may not be flexible enough; 在某些情况下(少于人们倾向于相信),链接任务可能不够灵活; hence, for some tasks (eg Copy ) an equivalent method (eg project.copy ) is provided. 因此,对于某些任务(例如Copy ),提供了等效方法(例如project.copy )。 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 (eg automatic up-to-date checks). 在许多情况下,任务是更好的选择,因为它们是Gradle的基本构建块并提供许多优点(例如,自动更新检查)。

Occasionally it also makes sense to use a GradleBuild task, which allows to execute one build as part of another. 有时,使用GradleBuild任务也是有意义的,它允许将一个构建作为另一个构建执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM