What is gradle?

satya - Thu Nov 15 2012 13:05:30 GMT-0500 (Eastern Standard Time)

What is gradle?

What is gradle?

Search for: What is gradle?

satya - Thu Nov 15 2012 13:06:53 GMT-0500 (Eastern Standard Time)

Home page at gradle

Home page at gradle

satya - Thu Nov 15 2012 13:08:08 GMT-0500 (Eastern Standard Time)

So what is it?

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.

satya - 7/11/2018, 1:47:05 PM

Basics of Gradle

Basics of Gradle

Search for: Basics of Gradle

satya - 7/11/2018, 1:47:30 PM

Tutorial on Gradle from Vogella.com

Tutorial on Gradle from Vogella.com

satya - 7/11/2018, 1:50:09 PM

Key files


//A series of tasks
build.gradle

//Names of projects
settings.gradle

satya - 7/11/2018, 1:50:58 PM

Hierarchy


Build
  Projects
    Tasks

satya - 7/11/2018, 1:51:14 PM

A task is a Java class behind the scenes

A task is a Java class behind the scenes

satya - 7/11/2018, 1:52:44 PM

Plug in: A plug in adds a bunch of tasks to the build system


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

satya - 7/11/2018, 1:54:36 PM

Key variables


GRADLE_HOME
GRADLE_HOME\bin

gradle -v

satya - 7/11/2018, 1:58:01 PM

What is the syntax or DSL of Gradle?

What is the syntax or DSL of Gradle?

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

satya - 7/11/2018, 2:03:05 PM

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

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

satya - 7/11/2018, 2:06:42 PM

example


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

satya - 7/11/2018, 2:06:54 PM

What goes in doLast is groovy code

What goes in doLast is groovy code

satya - 7/11/2018, 2:07:17 PM

the name upper is arbitrary

the name upper is arbitrary

satya - 7/11/2018, 2:08:50 PM

One task can depend on another


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

satya - 7/11/2018, 2:09:12 PM

More on dependencies

More on dependencies

satya - 7/11/2018, 2:14:23 PM

Task


Task
   action 1
   action 2
   action 3

satya - 7/11/2018, 2:14:48 PM

Action list

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.

satya - 7/11/2018, 2:22:37 PM

What are groovy closures?

What are groovy closures?

Search for: What are groovy closures?

satya - 7/11/2018, 2:57:37 PM

Syntax of ScriptHandlers in Gradle

Syntax of ScriptHandlers in Gradle

Search for: Syntax of ScriptHandlers in Gradle

satya - 7/11/2018, 2:59:53 PM

Part 1, 2 and 3 of Gradle basics including syntax

Part 1, 2 and 3 of Gradle basics including syntax

satya - 7/12/2018, 9:27:17 AM

A closure is a block of groovy code


def someFunc = {println 'hello'}

//execute that block of code
someFunc();

satya - 7/12/2018, 9:29:50 AM

Delegates of closures


//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

satya - 7/12/2018, 9:35:19 AM

Syntactic sugar overload


//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}

satya - 7/12/2018, 9:38:17 AM

Internalizing Groovy syntax


if 

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

then

someFunc { //closure code }

means call someFunc() with this closure

satya - 7/12/2018, 9:41:14 AM

Whose method is someFunc() above?

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

satya - 7/12/2018, 9:42:37 AM

Another syntactic sugar


somefunc blah

means

somefunc(blah)

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


somefunc {blah}

satya - 7/12/2018, 9:43:17 AM

Examples of gradle java code builds

Examples of gradle java code builds

Search for: Examples of gradle java code builds

satya - 7/12/2018, 9:55:54 AM

what is doLast() in groovy?

what is doLast() in groovy?

Search for: what is doLast() in groovy?

satya - 7/12/2018, 9:59:04 AM

Here is the closure class documentation

Here is the closure class documentation

satya - 7/12/2018, 10:09:49 AM

Understanding doLast()

Understanding doLast()

satya - 7/12/2018, 10:13:34 AM

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

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

satya - 7/12/2018, 1:03:39 PM

Adam Murdoch-3: comments from above

* 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.

satya - 7/12/2018, 1:07:11 PM

Syntax analyze this


//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

satya - 7/12/2018, 1:09:23 PM

Syntax analyses


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

satya - 7/12/2018, 1:10:53 PM

means


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

satya - 7/12/2018, 1:15:35 PM

Reference to a Task object

Reference to a Task object

satya - 7/12/2018, 1:19:50 PM

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

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

satya - 7/12/2018, 1:28:56 PM

function calling syntax in groovy scripting

function calling syntax in groovy scripting

Search for: function calling syntax in groovy scripting

satya - 7/12/2018, 3:27:33 PM

General Groovy DSL syntax notes

General Groovy DSL syntax notes

satya - 7/12/2018, 3:46:02 PM

Providing map as an argument to groovy dsl

Providing map as an argument to groovy dsl

Search for: Providing map as an argument to groovy dsl

satya - 7/13/2018, 8:51:17 AM

Gradle task parameter syntax

Gradle task parameter syntax

Search for: Gradle task parameter syntax

satya - 7/13/2018, 8:53:29 AM

Here is a discussion on the same question at SOF

Here is a discussion on the same question at SOF

satya - 7/13/2018, 8:55:40 AM

Summary of that 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!!!

satya - 7/13/2018, 8:56:39 AM

TaskDefinitionTransformer: The DSL rewrite of that syntax

TaskDefinitionTransformer: The DSL rewrite of that syntax

satya - 7/13/2018, 8:57:09 AM

TaskDefinitionTransformer Gradle syntax

TaskDefinitionTransformer Gradle syntax

Search for: TaskDefinitionTransformer Gradle syntax

satya - 7/13/2018, 9:01:12 AM

Authoring tasks

Authoring tasks

satya - 7/13/2018, 9:01:57 AM

This link has

this link has very many syntactical aspects of a task.

satya - 7/13/2018, 9:06:10 AM

Each task is made a property of the Project object

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

satya - 7/13/2018, 9:08:27 AM

Two ways of configuring a task


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')
}

satya - 7/13/2018, 9:11:46 AM

Gradle shortcut for tasks.getByName()

Gradle shortcut for tasks.getByName()

Search for: Gradle shortcut for tasks.getByName()

satya - 7/13/2018, 9:12:18 AM

differences between task invocation and method invocation in Gradle

differences between task invocation and method invocation in Gradle

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

satya - 7/13/2018, 9:21:51 AM

Writing custom tasks

Writing custom tasks

satya - 7/13/2018, 9:21:58 AM

Example


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'
}

satya - 7/13/2018, 1:25:20 PM

So this tasks.getByName()


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")


satya - 7/13/2018, 1:31:03 PM

Many (3) ways of interpreting a starting token in Gradle


//************************
//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",{})

satya - 7/13/2018, 1:32:15 PM

The three ways are:


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

satya - 7/13/2018, 1:52:11 PM

Groovy Java API docs

Groovy Java API docs

satya - 7/13/2018, 1:52:23 PM

Sorry Gradle Java API docs

Sorry Gradle Java API docs

satya - 7/13/2018, 1:53:07 PM

understanding gradle build settings.gradle

understanding gradle build settings.gradle

Search for: understanding gradle build settings.gradle

satya - 7/13/2018, 1:54:49 PM

This is somewhat documented here: build life cycle

This is somewhat documented here: build life cycle

satya - 7/13/2018, 4:25:52 PM

Role of buildScript inside build.gradle

Role of buildScript inside build.gradle

Search for: Role of buildScript inside build.gradle

satya - 7/13/2018, 4:28:12 PM

Understanding project directory structures

Understanding project directory structures

satya - 7/13/2018, 4:32:39 PM

Here is the SOF discussion on buildScript block

Here is the SOF discussion on buildScript block

satya - 7/13/2018, 4:46:51 PM

From Peter Niederwieser, of product Spock

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.

satya - 7/13/2018, 4:47:53 PM

What is Spock Framework

What is Spock Framework

Search for: What is Spock Framework

satya - 7/13/2018, 5:02:06 PM

Here is how repositories are handled

Here is how repositories are handled

satya - 7/13/2018, 5:03:07 PM

You will see here


flatDir
google
ivy
jcenter
maven
mavencentral
mavenlocal
etc...

satya - 7/26/2018, 7:53:01 PM

Gradle forums

Gradle forums

satya - 3/14/2019, 2:18:06 PM

This is an important document of the Project API

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

satya - 3/15/2019, 9:50:39 AM

How do you run a task?


gradle <task-name>