gradle - small introduction

22
Gradle Small introduction

Upload: igor-popov

Post on 26-Jan-2015

143 views

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Gradle - small introduction

GradleSmall introduction

Page 2: Gradle - small introduction

Required knowledge● average java● average groovy (properties, closures, collections: lists, maps)● general knowledge of build scripts (ant, maven etc)● mad search skillz (google)● desire to learn

Page 3: Gradle - small introduction

Groovy Lang - quick intro● lists: [1, 2, 3, 4]● maps: ['key1': 'value1', 'key2': 'value2'] --> can be used as parameters to

methods● when calling methods the parens are optional● properties: instead of using getters and setters directly you can just use the

name of the property as if it were a "normal" variable (without the "set" and "get" prefix)

Page 4: Gradle - small introduction

The Gradle DSL● a Domain Specific Language● a language designed for a specific need (such as building projects)● uses groovy

Page 5: Gradle - small introduction

The Project● the most important object in the Gradle DSL● everything you need is inside the "project" variable● if you don't specify an object all the method calls are delegated to the

"project" variable

Page 6: Gradle - small introduction

Simple example: fileTree● used for example when you want to load all the files in a directory (the "lib"

directory which contains all the jars)● TMTOWTDI: there's more than one way to do it

1. fileTree('lib')2. fileTree('lib').include('**/*.java').exclude('**/*.cpp')3. fileTree(dir: 'lib', include: '**/*.java', exclude: '**/*.cpp')4. fileTree('lib') {

include '**/*.java'exclude '**/*.cpp'

}

Page 7: Gradle - small introduction

Implementation of fileTreeFirst example: fileTree('lib')

From source code: ConfigurableFileTree fileTree(Object baseDir);

The baseDir is actually a string.

Page 8: Gradle - small introduction

Implementation of fileTreeSecond example: fileTree('lib').include('**/*.java').exclude('**/*.cpp')

From source code: ConfigurableFileTree fileTree(Object baseDir);

Gradle uses here something called: fluent interface(read more here: http://www.martinfowler.com/bliki/FluentInterface.html).

Basically this means you can chain method calls.

Probably you are wondering where are these include and exclude methods from, hmm? Well, they are from a super interface of ConfigurableFileTree:org.gradle.api.tasks.util.PatternFilterable.

Page 9: Gradle - small introduction

Implementation of fileTreeThird example: fileTree(dir: 'lib', include: '**/*.java', exclude: '**/*.cpp')

From source code: ConfigurableFileTree fileTree(Map<String, ?> args);

Here, each of "dir", "include" and "exclude" are actually the keys of a map.Behind the scenes the fileTree method will configure a ConfigurableFileTree object.

Page 10: Gradle - small introduction

Implementation of fileTreeFourth example: fileTree('lib') {

include '**/*.java'exclude '**/*.cpp'

}

From source code: ConfigurableFileTree fileTree(Object baseDir, Closure configureClosure);

There is yet another way to configure the fileTree method: using a closure.

Page 11: Gradle - small introduction

The Simplest Gradle Scriptapply plugin: "java"

This assumes that you respect the standard maven project structure:

src |____ main | |____ java // here go all the source files ... | | |____ test |____ java // ... and here go all the test classes

Page 12: Gradle - small introduction

The Simplest Gradle ScriptWhy does this simple script work?

Gradle:● uses a thing called "Convention over Configuration",● provides a number of tasks to ease the creation of a new project:

○ :compileJava

○ :processResources

○ :classes

○ :jar

○ :assemble

○ :compileTestJava

○ :processTestResources

○ :testClasses

○ :test

○ :check

○ :build

Page 13: Gradle - small introduction

Custom taskstask --> groovy compiler plugin:

Standard way to define a new task:

task myTaskName() {// doing some stuff...

}

This is actually a more convenient way for the following:

task("myTaskName")

Source (StackOverflow): How to interpret Gradle DSL

Page 14: Gradle - small introduction

Custom tasksGradle provides some predefined tasks which you can configure.

Example: Copy, Zip, Compile, Jar, War

task myCopyTask(type: Copy) { from 'src/main/webapp' into 'build/explodedWar'}

Notice that the task receives a named parameter (a map with one entry actually) called type.

This is similar to inheriting from a base class in the sense that now, in the closure (the stuff between curly brackets { // stuff }) you have access to the from and into methods. These are specific to the copy task.

Page 15: Gradle - small introduction

doLast notationSometimes you need to add something to an existing task.

For example to add some stuff to the task defined in the previous slide we would do:

task myCopyTask << { // do something}

This is equivalent to the following:task myCopyTask.doLast { // do something}

The doLast notation is pretty common in build scripts so you'd better remember it :)

Page 16: Gradle - small introduction

Order must be mantainedSometimes you need to specify that a task should run only after other tasks are run first. For this you need to use dependsOn.

task someOtherTask() {// do some other stuff

}

task myTask(dependsOn: someOtherTask) {// do stuff

}

Page 17: Gradle - small introduction

Gradle PluginsThese add new capabilities to gradle (obviously).

Here's how to add a plugin:apply plugin: "java"

The plugins basically:● Add tasks to the project (example compile, test)● Preconfigure tasks with defaults● Add dependency configurations to the project● Add new properties and methods to existing type via extensions

There are plugins for many things such as static code analysis, new languages (groovy), various IDE's (eclipse, idea).

Page 18: Gradle - small introduction

The WrapperGradle provides a way to build a project even without having gradle installed, although you need to have at least the JDK installed.

Here is how to define the wrapper task:

task wrapper(type: Wrapper) {gradleVersion = '1.3'

}

And here's how you call it from the command line:

gradle wrapper

You need to run this each time you update the gradle version variable.

Gradle will generate some files in the project's root directory: gradlew.bat gradlew and the gradle directory.

Page 19: Gradle - small introduction

The WrapperThe recommended way to build the project after you generate the wrapper files is using the gradlew.bat script which will take the same parameters as the "normal" gradle:

gradlew build

Just be carefull to call gradlew in the project root directory(since that's were the script is located).

Page 20: Gradle - small introduction

Adding librariesFirst: you must define a repository that tells gradle where to take the libraries from.Here's how to do it:

repositories {mavenCentral()

}

mavenCentral() is the usual repository you should use.

Second: you must specify the libraries you want to use. For example, if you want junit you need to search on MvnRepository the actual string to include:

dependencies {testCompile 'junit:junit:+'

}

Page 21: Gradle - small introduction

The complete scriptapply plugin: 'java'apply plugin: 'eclipse'

defaultTasks 'build', 'eclipse'

repositories {mavenCentral()

}

dependencies {testCompile 'junit:junit:+'

}

task wrapper(type: Wrapper) {gradleVersion = '1.3'

}

Page 22: Gradle - small introduction

Thanks for attention

and

Enjoy!

Thanks...