User defined tasks in gradle

Plugins provide most of the built in tasks in Gradle. For example – Java plugin provides default tasks. But you can also write your custom tasks in build.gradle file as shown below.
 
task task1;

task1.doFirst( {
    println "This block of task1 will be executed first"
} );

task1.doLast( {
    println "This block of task2 will be executed last"
} );

task1 <<{
    println "This block will be executed after doLast block of task1"
}


//Another way to declare task

task task2 << {
    println "Declaring and defining new task at the same time"
}

task2 << {
    println "This block of task2 will be executed after task2 - doLast"
}

task2 << {
    println "You can have as many blocks as possible for a task"
}
Note that you can also add your own code to be executed in specific task. For example – below lines of block will be executed at the time of compileJava task

compileJava.doFirst { println ‘This will be executed before starting compileJava task’ }
compileJava << { println ‘This will be executed after compileJava task’ }

Web development and Automation testing

solutions delivered!!