Gradle builds a script file for handling two things; one is projects and other is tasks. Every Gradle build represents one or more projects.

A project represents a library JAR or a web application or it might represent a ZIP that is assembled from the JARs produced by other projects. In simple words, a project is made up of different tasks.

A task means a piece of work, which a build performs. A task might be compiling some classes, creating a JAR, generating Javadoc, or publishing some archives to a repository.

Gradle uses Groovy language for writing scripts.

## Writing Build Script

Gradle provides a Domain Specific Language (DSL), for describing builds. This uses the Groovy language to make it easier to describe a build. Each build script of Gradle is encoded using UTF-8, saved offline and named as build.gradle.

We describe the tasks and projects by using a Groovy script. You can run a Gradle build using the Gradle command. This command looks for a file called build.gradle.

Take a look at the following example which represents a small script that prints tutorialspoint.

Copy and save the following script into a file named build.gradle. This build script defines a task name hello, which is used to print tutorialspoint string.

doLast {
println 'tutorialspoint'
}
}

Execute the following command in the command prompt where the build.gradle file is stored.

Output

You will see the following output −

tutorialspoint

If you think task works similar to ANT’s target, then that is right. Gradle task is equivalent to ANT target.

You can simplify this hello task by specifying a shortcut (represents a symbol <<) to the doLast statement. If you add this shortcut to the above task helloit will look like the following script −

println 'tutorialspoint'
}

Now, You can execute the above script using gradle –q hello command.

The Grade script mainly uses two real Objects, one is Project Object and the other is Script Object.

• Project Object − Each script describes about one or multiple projects. While in the execution, this scripts configures the Project Object. You can execute some methods and use property in your build script, which are delegated to the Project Object.

• Script Object − Gradle takes script code into classes, which implements Script Interface and then, it is executed. This means that all the properties and methods declared by the script interface are available in your script.

The following table defines the list of standard project properties. All these properties are available in your build script.

Sr. No. Name Type Default Value
1 project Project The Project instance
2 name String The name of the project directory.
3 path String The absolute path of the project.
4 description String A description for the project.
5 projectDir File The directory containing the build script.
6 buildDir File projectDir/build
7 group Object Unspecified
8 version Object Unspecified
9 ant AntBuilder An AntBuilder instance

## Groovy Basics

Gradle build scripts use the full length Groovy API.

As a startup, take a look at the following examples.

### Example 1

This example explains about converting a string to upper case.

Copy and save the code which is given below, into build.gradle file.

String expString = 'TUTORIALS point'
println "Original: " + expString
println "Upper case: " + expString.toUpperCase()
}

Execute the following command in the command prompt. It executes the above given script. You should execute this, where the build.gradle file is stored.

Output

Original: TUTORIALS point
Upper case: TUTORIALS POINT

### Example 2

The following example explains printing the value of an implicit parameter ($it) for 4 times Copy and save the following code into build.gradle file. task count << { 4.times { print "$it "
}
}

Execute the following command in the command prompt. It executes the script stated above. You should execute this, where the build.gradle file is stored.

$gradle –q count Output This produces the following output − 0 1 2 3 ## Features of Groovy Groovy language provides plenty of features. Some important features are discussed below − ## Groovy JDK Methods Groovy adds lot of useful methods to the standard Java classes. For example, Iterable API from JDK implements an each() method, which iterates over the elements of the Iterable Interface. Copy and save the following code into build.gradle file. task groovyJDK << { String myName = "Marc"; myName.each() { println "${it}"
};
}

Execute the following command in the command prompt. It executes the above given script. You should execute this, where the build.gradle file stores.

Output

When you execute the above code, you should see the following output −

M
a
r
c

## Property Accessors

You can automatically access appropriate getter and setter methods of a particular property by specifying its reference.

The following snippet defines the syntaxes of getter and setter methods of a property buildDir.

// Using a getter method
println project.buildDir
println getProject().getBuildDir()

// Using a setter method
project.buildDir = 'target'
getProject().setBuildDir('target')

### Optional Parentheses on Method Calls

Groovy contains a special feature in methods calling, which is that the parentheses are optional for method calling. This feature applies to Gradle scripting as well.

Take a look at the following syntax. which defines a method calling systemProperty of test object.

test.systemProperty 'some.prop', 'value'
test.systemProperty('some.prop', 'value')

### Closure as the Last Parameter

Gradle DSL uses closures in many places, where the last parameter of a method is a closure. You can place the closure after the method call.

The following snippet defines that the syntaxes Closures use as repositories() method parameters.

repositories {
println "in a closure"
}
repositories() {
println "in a closure"
}
repositories({ println "in a closure" })

### Default Imports

Gradle automatically adds a set of import statements to the Gradle scripts. The following list shows the default import packages to the Gradle script.

The default import packages to the Gradle script are listed below −