Creating and executing task

Gradle is domain specific language where the domain in this case is automating application build process. Gradle consists of Projects and Tasks. Each project has multiple Tasks and each Task has multiple actions.

In this post I will explain how to create and execute task.

Create a file by name build.gradle with the below code


task hello {
    doFirst {
        println 'Hello World'
    doLast {
        println 'I am Gradle'

The gradle script is a configuration script. Whenever the build script is executed, instances of org.gradle.api.Project and org.gradle.api.Task are created. The script is used to configure these instance.

In the above code we create a task by using “task” keyword and name of the task as “hello”.

Every task instance has two methods “doFirst” and “doLast” and the actions of those methods in this case are println statements.

Open the command line and go to the folder containing the build.gradle.

Run the below code

gradle hello

The gradle build will search for build.gradle file in the current folder and execute the task hello.


Task :hello
Hello World
I am Gradle

Leave a Reply