Gradle is an open source build automation system that builds upon the concepts of Apache Ant and Apache Maven and introduces a Groovy-based domain-specific language (DSL) instead of the XML form used by Apache Maven for declaring the project configuration. (Wikipedia)
Gradle Build Scripts
After look this example. Is this a JSON format or Python code? The answer is that Gradle scripts are written in a special purpose build language provided by Gradle. It sits on top of a generic scripting language called Groovy.
- Groovy has a lot of syntactic sugar in other features to allow us to write build scripts that look alot more like natural language when compared to using something like Java.
- Groovy integrates perfectly with java which is the language that Gradle platform is written in.
Some concept:
- Delegate object is the entire build script (all content in build.gradle file)
- If you write a Gradle plugin, you could write it in any language and use the same delegate object. Ex: apply plugin: ‘com.android.application’
- Gradle build language is also called the Gradle DSL(Domain specific language). It is a language that is finely tailored for a specific task.
- Recommend that you keep build scripts declarative and try not to pollute it with low-level logic. You can write them in any JVM language like Java, Groovy or Scala.
Groovy Fundamentals: copy this code and paste into build.gradle file and open cmd (Write command: gradle groovy)
After we understand about groovy, we will learn about task.
1.Configure a task
2.Task Dependencies
After we understand about 1 task. We will thinking about the relationships between tasks. I will discuss about three ways to configure the relationships between tasks: depends on, finalize by and must run after
Task A `dependsOn` task B if task A can’t do its work without task B having done its work. To take a familiar example, when you’re getting up in then morning, you can’t put on your shoes without putting on your socks first. Let’s declare and configure some tasks to represent this process.
Task A is `finalizedBy` task B if every time task A runs, task B should be run afterwards. To continue our example, every time you eat breakfast, you must
brush your teeth afterwards. Let’s create those tasks, and declare the relationship between them.
The use case for `mustRunAfter` is slightly less obvious. Say we have a long
running process that’s unlikely to fail, like deploying some artifact to a continuous integration server, and we also have a short running task that is
likely to fail, like running unit tests. Those two tasks don’t have any
dependency relationship, but if we’re running both, we would really like the
unit tests to run before the integration tests.
3.Interacting with the file system
Type of action can be Zip, Copy, Sync, Delete…
To know more about other type of Task or more please read document here.
4.Incremental Builds
As our builds become increasingly more complex, we want to ensure that we don’t redo any work that has already been done the last time we executed our build. Incremental build is only doing the minimum amount of work necessary.
Gradle accomplishes this by tracking each task’s inputs and outputs. Before each task is run, Gradle saves a snapshot of the inputs used by the task. If that particular task doesn’t have any snapshots of its input yet or if the inputs have changed, the Gradle will run the task again.
5.Parameterising Your Build
You may often come across situations where there’s part of your build script that you’ll want to change somewhat regularly. Example you build for CI to tester, Staging to customer test and Production for end-user. You will write parameter to your build.
Example:
If you run this task command line will throw exception because it unknow greeting.
You will add parameter by three way. The order of precedence is same to the order of appearance below:
- Add ext{greeting= “Hello World”} into build.gradle file
- Add -Pgreeting= “Hello World” in to command line.
- Add greeting = “Hello World” into gradle.properties file
6.Creating Custom Task Types
7.Troubleshoooting and Logging
If you want to know more information about build process you can use gradle -d task to view debug log
Logging Levels: Debug (-d)> Info (-i)> Lifecycle, Warning (default)> Error , Quiet (-q)
8. Build Lifecycle
There are actually three phases to the lifecycle of any Gradle build:
Initialization is mostly concerned with setting up multi-project builds.
Configuration is phase the build script is executing which configures all the project’s talks. This is when a directed a cyclic task is build up and Gradle determines which tasks need to be run and in which order.
Execution is phase all the task actions of all the tasks that are chosen are executed in the proper order