Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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 ?

like image 475
codeScriber Avatar asked Feb 15 '23 14:02

codeScriber


1 Answers

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.

like image 87
Peter Niederwieser Avatar answered Apr 28 '23 21:04

Peter Niederwieser