Gradle Groovy Syntax Getting Started

Gradle's build language is based on Groovy. Groovy is a general-purpose programming language, based on Java, that compiles to Java byte codes.

In Groovy, the following packages are all automatically imported:

  • java.lang
  • java.util
  • java.io
  • java.net
  • groovy.lang
  • groovy.util

Variable

def name = 'Groovy' 
def text = "Hello, $name!"
println text
println  "size= ${text.size()} characters."

Collections and Map

def nums = [3, 1, 4, 1, 5, 9, 2, 6, 5] 
def map = [a:1, b:2, c:2] 

Classes and members

class UserInfo {    
    String gender
}

To use UserInfo :

def user = new UserInfo()
println user.setGender 'Famale'
println user.gender 
user.setGender 'Male'
println user.getGender() 
public int square(int num) {
    return num * num
}
println square(2)