What is gradle?

What is gradle?

Search for: What is gradle?

Home page at gradle

Gradle combines the power and flexibility of Ant with the dependency management and conventions of Maven into a more effective way to build. Powered by a Groovy DSL and packed with innovation, Gradle provides a declarative way to describe all kinds of builds through sensible defaults. Gradle is quickly becoming the build system of choice for many open source projects, leading edge enterprises and legacy automation challenges.

Basics of Gradle

Search for: Basics of Gradle

Tutorial on Gradle from Vogella.com


//A series of tasks
build.gradle

//Names of projects
settings.gradle

Build
  Projects
    Tasks

A task is a Java class behind the scenes


//Load the following from a repo
apply plugin:'my-plugin-with-tasks'

GRADLE_HOME
GRADLE_HOME\bin

gradle -v

What is the syntax or DSL of Gradle?

Search for: What is the syntax or DSL of Gradle?

Explanation for the syntax seemed to stuck in here on gradle site


task upper {
    doLast {
        String someString = 'mY_nAmE'
        println "Original: " + someString
        println "Upper case: " + someString.toUpperCase()
    }
}

What goes in doLast is groovy code

the name upper is arbitrary


task hello {
    doLast {
        println 'Hello world!'
    }
}
task intro(dependsOn: hello) {
    doLast {
        println "I'm Gradle"
    }
}

More on dependencies


Task
   action 1
   action 2
   action 3

The calls doFirst and doLast can be executed multiple times. They add an action to the beginning or the end of the task?s actions list. When the task executes, the actions in the action list are executed in order.

What are groovy closures?

Search for: What are groovy closures?

Syntax of ScriptHandlers in Gradle

Search for: Syntax of ScriptHandlers in Gradle

Part 1, 2 and 3 of Gradle basics including syntax


def someFunc = {println 'hello'}

//execute that block of code
someFunc();

//some-var a future definition
//some-other-func as well 

def someFunc = {
println some-var;
some-other-func();
}

//Set the context for its variables
someFunc.setDelegate(some-object-that-has-some-var);
someFunc();

//both some-var and some-other-func
//will come from this context object

//This is a defintion
someFunc = {some-closure}

//This is an invocation
someFunc()

//This is different meaning
//Call func that takes closure
someOtherFunc(someFunc)

//Same as above but closure is on the fly
someOtherFunc {some-closure}

if 

someFunc(Closure c)
{
   //execute closure or pass it on
   c();
}

then

someFunc { //closure code }

means call someFunc() with this closure

The code above is valid groovy code.

So someFunc() must be existing in the context of an object

Gradle must set that parent context object so that someFunc() gets resolved as a valid method


somefunc blah

means

somefunc(blah)

Notice here the "blah" is not a closure. if it had been then it would be


somefunc {blah}

Examples of gradle java code builds

Search for: Examples of gradle java code builds

what is doLast() in groovy?

Search for: what is doLast() in groovy?

Here is the closure class documentation

Understanding doLast()

This is a nice one. Finally the doLast() cracked

* There are 2 different points in time when code related to a task is executed: The first is configuration time, which is when the build script executes. The idea is that at this time you configure the task, so that it does the right thing at the other point in time, namely, execution time. Execution time is the point in time where the task does its actual work, and happens only if the task is selected for execution, and after its dependencies have executed.

* Each task has a sequence of actions, which are run in the order specified when the task executes. An action is simply a closure or an Action implementation. The doFirst() method configures the task to add an action at the start of the sequence. The doLast() and << methods configure the task to add an action at the end of the sequence.


//same as task("hello", closure)
//using the syntax simplification of Groovy

task hello {
    doLast {
        println 'Hello world!'
    }
}

//Inside the closure which gets to run 
//as part of defining the task,
//the code calls

task.doLast(closure)

//the last closure is the
print hello world

task upper {
    doLast {
        String someString = 'mY_nAmE'
        println "Original: " + someString
        println "Upper case: " + someString.toUpperCase()
    }
}

//define task
st = task("upper")
//do the following when task executes
st.doLast(The code above)

Reference to a Task object

the key word "task" is a method on the Project root object

function calling syntax in groovy scripting

Search for: function calling syntax in groovy scripting

General Groovy DSL syntax notes

Providing map as an argument to groovy dsl

Search for: Providing map as an argument to groovy dsl

Gradle task parameter syntax

Search for: Gradle task parameter syntax

Here is a discussion on the same question at SOF

The task definition line appears to be tampered by a specific DSL to pretty much mean what ever one would like to mean as long as it satisfies the "task" method signatures on a "project" object.

So it is hard to break ones head on extending direct Groovy method syntax to this line!!!

TaskDefinitionTransformer: The DSL rewrite of that syntax

TaskDefinitionTransformer Gradle syntax

Search for: TaskDefinitionTransformer Gradle syntax

Authoring tasks

this link has very many syntactical aspects of a task.

Each task is available as a property of the project, using the task name as the property name


Copy myCopy = task(myCopy, type: Copy)
myCopy.from 'resources'
myCopy.into 'target'
myCopy.include('**/*.txt', '**/*.xml', '**/*.properties')

//Or

task myCopy(type: Copy)

myCopy {
   from 'resources'
   into 'target'
   include('**/*.txt', '**/*.xml', '**/*.properties')
}

//Or

task copy(type: Copy) {
   from 'resources'
   into 'target'
   include('**/*.txt', '**/*.xml', '**/*.properties')
}

Gradle shortcut for tasks.getByName()

Search for: Gradle shortcut for tasks.getByName()

differences between task invocation and method invocation in Gradle

Search for: differences between task invocation and method invocation in Gradle

Writing custom tasks


class GreetingTask extends DefaultTask {
    String greeting = 'hello from GreetingTask'

    @TaskAction
    def greet() {
        println greeting
    }
}

// Use the default greeting
task hello(type: GreetingTask)

// Customize the greeting
task greeting(type: GreetingTask) {
    greeting = 'greetings from GreetingTask'
}

blah {
//some code
}

//is same as
tasks.getByName blah {
//some code
}

//so 
blah 

//is a shortcut for
tasks.getByName blah

//or - due to groovy syntax

tasks.getByName("blah")



//************************
//As a function
//************************
token some-thing
task blah

//It is interpreted as a function
//See in project object
token(some-thing)
task(blah)//creates a task

//************************
//As a property of Project
//************************
token.property="something"
blah-task.from="hello"

//************************
//As a function on a task
//************************
token {}
blah-task {}

//is same as 
tasks.getByName token {}
tasks.getByName("token",{})

As a function
As a property
As a special form of tasks.getByName()

Groovy Java API docs

Sorry Gradle Java API docs

understanding gradle build settings.gradle

Search for: understanding gradle build settings.gradle

This is somewhat documented here: build life cycle

Role of buildScript inside build.gradle

Search for: Role of buildScript inside build.gradle

Understanding project directory structures

Here is the SOF discussion on buildScript block

The buildScript block determines which plugins, task classes, and other classes are available for use in the rest of the build script. Without a buildScript block, you can use everything that ships with Gradle out-of-the-box. If you additionally want to use third-party plugins, task classes, or other classes (in the build script!), you have to specify the corresponding dependencies in the buildScript block.

What is Spock Framework

Search for: What is Spock Framework

Here is how repositories are handled


flatDir
google
ivy
jcenter
maven
mavencentral
mavenlocal
etc...

Gradle forums

This is an important document of the Project API

This is part of the Java API of Gradle. Among many other things it describes the difference between the configuration part of the taks versus the execution part of the task.

When a task is created it assumes the top level closure of that task as something that must be run, right after creating the task. That is why this closure is called the configuration closure.

A configuration closure can be seen as the closure or function that configures the object by manipulating that objects properties by calling its various methods and then return the object thus configured.

This means the "doLast({})" closure is not part of the configuration time. The entire closure is saved as a "doLast()" action for future execution.

So by looking at a closure we cannot assume a) who the owner is b) who the delegate is c) or when it will get executed without actually looking at the documentation


gradle <task-name>