Writing sample build script(Hello World)

Gradle is based on the concept of task and you write these task in a file called build.gradle.

Below is a sample build.gradle file which has a task called helloWorld which prints the text ‘Hello world.’ on console.

Now to execute this build script we need to go to the directory where the build.gradle file is store and execute the below command command

gradle <task_name>

Now since our task name is helloWorldin the build.gradle file we will execute it with the command gradle helloWorld (-q for quitely) and below is the output of the command

Looping

task count {
    doLast {
        4.times { print "$it " }
    }
}

Creating an Adhoc task(Avoidance API)

tasks.register("helloNewTask") {
    group = 'Welcome'
    description = 'Produces a greeting'

    doLast {
        println 'Hello Avoidance API'
    }
}

Costume task (Avoidance API)

class Greeting extends DefaultTask {  
    String message 
    String recipient

    @TaskAction 
    void sayGreeting() {
        println "${message}, ${recipient}!" 
    }
}

tasks.register("hello", Greeting) { 
    group = 'Welcome'
    description = 'Produces a world greeting'
    message = 'Hello' 
    recipient = 'World'
}

Leave a Comment