How to write a gradle task in your build.gradle to do a custom job

As you already know, gradle is a project automation tool based on the concepts and methods of  Apache Ant and Apache Maven contains the good aspects of both and it provides a way to implement your own build idea. Unlike ant or maven that uses xml to configure things, gradle uses a groovy based Domain Specific Language (DSL) to write your build logic. Gradle supports both groovy as well as java. So don’t worry if you are not familiar with groovy, you can write your script in gradle in java itself with minor alterations to formal java.

gradle

In this post I would explain how to write a task in your build.gradle so that you can assign your build script to do a specific job while doing the build. Sounds interesting?. We can do it as simple as writing a method inside a class. Let me start.

First of all the task your are going to write may be inside your existing build.gradle file which is already part of your project. Suppose you want to write a task that reads sql queries from a file and execute it, the structure of the gradle script will look like below:

[code lang=”groovy”]
task runsSQL{
description ‘your task description’
apply plugin: ‘java’

repositories {
mavenLocal()
}

configurations {
jdbcdriver
}

dependencies {
jdbcdriver ‘com.oracle:ojdbc6:11.2.0.4.0’
}

doLast{
println "performing the task"
<reading the file>
<separating the queries>
<connecting to the database>
<executing the queries>
}

}
[/code]

 The initial line of code will define the task and it will also define the dependencies needed to be resolved. As you see, you can give a description for your task to give others what you are going to do with this task. Next is to apply java plugin to your task so that you could use all the java default api’s available in your script.

[groovy]

repositories {

mavenLocal();

}

[/groovy]

The above code will decide where to look for the dependencies for your task. As the oracle jdbc drivers that I am using is not available in the maven online repository, I give local maven repository which has the ojdbc6.jar installed. Next thing is I will define the dependency of the database driver.

[groovy]
doLast{
}
[/groovy]

I am sure you had noticed that I put all the script logic inside the doLast closure. The reason why I am going for this approach is as follows: When you execute your task, anything which is outside the doLast closure will be in configuration phase and will be executed common for all tasks. So if I have one more task which uses the same dependency I don’t have to tell the task to compile it again. In configuration phase things outside doLast will be executed and available for all tasks.

if you don’t have anything to put in the configuration phase the you can use a shorthand operator for defining your task like below:

[groovy]
task myTask()<<{
}
[/groovy]

By following the above method, your script logic can go to any level of complexities. All you want to do extra is to create sub tasks (I am calling it like that! ) which is similar to normal methods (or functions) in programming languages so that you can split and organize our logic. Upcoming posts will tell you how to write methods in gradle to organize your code.

Share this article:

Comments (1)

  1. This is very helpful. I just want to create queries from domain classes.

Leave a Reply

Your email address will not be published. Required fields are marked *