0

I wanted to execute gradle task from my plugin code.

Any one can suggest me, how can I programmatically execute gradle task from code.

Thanks, Sumeet.

Rene Groeschke
  • 27,999
  • 10
  • 69
  • 78
sumeet
  • 525
  • 2
  • 7
  • 16

2 Answers2

1

You can do it as follows

task a {
  doLast {
    println 'test'
  }
}

task b {
    doLast {
        a.execute()
    }
}

So in plugin code it might be something similar to

project.tasks.<taskname>.execute()

But this might be changed in the future. You should rely on the chaining of the tasks rather then invoking them directly.

Martin Linha
  • 979
  • 9
  • 21
  • Hi Martin, first of all thanks for responding on my question. I am trying to apply the solution provided by you, but not succeed as of now. I have used the instance of Project(com.intellij.openapi.project.Project), and then trying to call task on that instance, but not able to get task on project instance. Can you please confirm me exact which project class you had referred into your solution. – sumeet Feb 26 '17 at 10:32
  • The instance is correct. Maybe the task at the time you call it is not available at that time yet. Difficult to same without seeing the code. Try to look it up by calling `project.tasks.findByName(name)`. If it returns null, then try it at different time like calling it within `gradle.taskGraph.whenReady {taskGraph -> project.tasks.findByName(name) }` – Martin Linha Feb 26 '17 at 11:18
  • class MyAction extends AnAction { public void actionPerformed(AnActionEvent anActionEvent) { mProject=anActionEvent.getProject(); path=mProject.getBasePath(); ApplicationManager.getApplication().runWriteAction(runTasks()); } Runnable runTasks() { return new Runnable(){ public void run(){ CommandProcessor.getInstance().executeCommand(mProject, new Runnable() { public void run() { try { List tasks=new ArrayList(); String taskname=Constant.MY_TASK; tasks.add(taskname); mProject. } here I am not getting task on mProject instance in last line... – sumeet Feb 26 '17 at 12:01
  • Please open a new question for this. My answer answers you first question, so please mark it as the answer. I can try to help you in different thread, it is difficult to do it here via comments. – Martin Linha Feb 26 '17 at 12:16
  • Yeah, sure, I can understand, but only issue which is I am facing is that I am not able to get task on your solution "project.tasks".... On pressing dot(.) into IDE, it should show tasks as suggestion, but its not happening... – sumeet Feb 26 '17 at 12:21
  • Oh, my bad sorry. You are using a wrong class. It is this https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html – Martin Linha Feb 26 '17 at 13:18
  • It comes from `compile gradleApi()` – Martin Linha Feb 26 '17 at 13:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/136720/discussion-between-sumeet-and-martin-linha). – sumeet Feb 27 '17 at 06:25
  • Thanks to let me know about this API's, but now problem is that I am not getting exec() method on instance of project.task() – sumeet Feb 27 '17 at 11:02
  • Use `project.tasks.findByName('name').execute()` – Martin Linha Feb 27 '17 at 11:03
  • surely something we are missing here, as still I am not able to get method findByName('name') on project.tasks(). Is there any sample code available or any blog for implementing such feature, that will be very helpful for me to resolve minor issues related to API's. Looking forward for your response. – sumeet Feb 27 '17 at 12:33
  • It is not tasks(), just tasks. Or you call project.getTasks() – Martin Linha Feb 27 '17 at 12:54
  • Now Task t = project.getTasks().findByName("SonarQube"); is working fine, but then not able to get any method like "t.execute()". That means execute method is not coming on calling .(dot) to project.getTasks().findByName("SonarQube").. – sumeet Feb 27 '17 at 13:12
  • See my example. There is execute() on task. Please copy the example and run it. You will see it works. Or in you case execute() does not return task, so you have to call JUST `project.getTasks().findByName("SonarQube").execute()`. And please answer the question since it answers your very first question. – Martin Linha Feb 28 '17 at 11:11
  • I am again repeating my issue, that I am not getting execute() method on project.getTasks().findByName("SonarQube"). method's are not available on Task instance, so my question is still not answered as I am not able to resolve the issue. – sumeet Feb 28 '17 at 11:42
  • Do you have any blog or sample app code which is using related API's, that will be quite helpful to solve this issue, instead of this long chain comments.. – sumeet Feb 28 '17 at 12:09
  • It is Groovy. Please invoke the method execute() on the task and you will find out that it is there. See second comment here https://discuss.gradle.org/t/runnig-task-programmatically/7542 BTW I executed the code in my answer and it works. – Martin Linha Feb 28 '17 at 14:07
  • If the same code is working on your machine, then it must be different version of jar file, from where this API's are coming. Can you please share that version of jar file by uploading the same on cloud, or tell me from where you have downloaded the same. – sumeet Feb 28 '17 at 16:55
  • Another thing, as you are saying this is groovy, and from the second comment of [link] (https://discuss.gradle.org/t/runnig-task-programmatically/7542/17) as this execute method is not exposed to public API, then how can we access them, means from which jar file we can access this method. – sumeet Feb 28 '17 at 17:07
  • I wanted to execute task from the Android studio plugin code, that is build up by using the IntelliJ IDE, and I think it should be achievable only with the API's of package "com.intellij.openapi" and its sub-packages. – sumeet Mar 02 '17 at 09:10
1

The answer provided by Martin Linha does not work anymore with recent versions of Gradle, for instance Gradle 7. The Task class does not have an execute method anymore. Instead, the activities have to be executed. In addition, you might want to execute the dependencies as well:

void executeTask(Task task) {
    task.taskDependencies.getDependencies(task).each {
       subTask -> executeTask(subTask)
    }
    task.actions.each { it.execute(task) }
}

Note that this is still a hack and not guaranteed to work.

ocroquette
  • 3,049
  • 1
  • 23
  • 26