Calling build in task from a custom task?


(tulsoba) #1

How do I call build-in tasks from my custom gradle task? I need to download some dependencies to the root of my project in eg. tmp. But I need to delete the tmp folder before (cleanup) if it exists:

public class FetchDeps extends DefaultTask
 {
  @TaskAction
  void fetchDependencies() {
    Project project = getProject();
    // File projectDir = project.getProjectDir();
    // Task byName = project.getTasks().getByName("Delete");
         task deleteTmp(type: Delete) {
        delete 'tmp' // if exists
    }
          task copyToTmp(type: Copy) {
        into 'tmp'
        from configurations.runtime
     }
  }

(Peter Niederwieser) #2

A task cannot call other tasks; it can only depend on them. In this particular case, you could also use the ‘project.delete’ and ‘project.copy’ methods.


(tulsoba) #3

Thanks. The delete goes fine but how do I write the correct closure for the copy operation (the task is written in java):

@TaskAction
  void fetchDependencies() {
    Project project = getProject();
    File tmpFolder = new File(project.getProjectDir(), "tmp");
    if(tmpFolder.exists()) {
      project.delete(tmpFolder.getPath());
    }
      //
  copyToLib(type: Copy) {
//
    into "libs"
//
    from configurations.runtime
//
  }
          project.copy(new Closure<String>(tmpFolder) {
      @Override
      public Closure asWritable() {
        // TODO Auto-generated method stub
        return super.asWritable();
      }
    });

(detelinyordanov) #4

I have used Groovy’s MethodClosure in such cases, here is some example: http://www.jroller.com/melix/entry/coding_a_groovy_closure_in