2. 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
3. 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)
4. The Gradle DSL
●
●
●
a Domain Specific Language
a language designed for a specific need (such as building projects)
uses groovy
5. 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
6. Simple example: fileTree
●
●
1.
2.
3.
4.
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
fileTree('lib')
fileTree('lib').include('**/*.java').exclude('**/*.cpp')
fileTree(dir: 'lib', include: '**/*.java', exclude: '**/*.cpp')
fileTree('lib') {
include '**/*.java'
exclude '**/*.cpp'
}
7. Implementation of fileTree
First example: fileTree('lib')
From source code: ConfigurableFileTree fileTree(Object baseDir);
The baseDir is actually a string.
8. Implementation of fileTree
Second 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.
9. Implementation of fileTree
Third 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.
10. Implementation of fileTree
Fourth 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.
11. The Simplest Gradle Script
apply 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
12. The Simplest Gradle Script
Why 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
13. Custom tasks
task --> 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
14. Custom tasks
Gradle 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.
15. doLast notation
Sometimes 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 :)
16. Order must be mantained
Sometimes 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
}
17. Gradle Plugins
These 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).
18. The Wrapper
Gradle 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.
19. The Wrapper
The 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).
20. Adding libraries
First: 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:+'
}