One of the major benefit Gradle can provide you is continuous build feature. First of all let me explain what is meant by continuous build. A normal build is where you make any changes in the source files (It can be in .java file or in the resources you use) and you need to explicitly build your project to create a new jar/war/ear file. In a continuous build setup, you don’t have to worry about giving build requests every time you make a change in your project. As soon as you make a change in any of the files, the build system will detect it and trigger a new build for you. So the build output will always be updated with new change.
Prerequisites: Gradle 2.5 or later.
It is very easy to setup a continuous build using Gradle. Normally how would you request for a build in Gradle?
gradle clean build
So in-order to start a continuous build, you just have to specify:
gradle clean build --continuous
As you know, clean is optional here.
Once you have executed the above command, Gradle executes the first build as usual and waits instead of exiting when done. When a change occurs, Gradle executes the sequence and goes on.
How to exit from the build?
You have to press CTRL + D and hit enter to exit from the loop.
When the next build will be triggered?
One point here to be noted is that, if you make a change in the Gradle build script itself or in any related files, those changes wont be accompanied in the current continuous build. It will only picked up if you stop the build and start again. As said earlier, once Gradle is entered into it will start watching for the changes in the source files of the project. That means if you consider a typical java project with following directory structure:
Any changes in the above directory – like changes in files, addition / removal of files to/from will initiate a new build.
How Gradle incremental build support optimizes the continuous build?
Gradle don’t just use a single task to build all the files inside a project. It has separate tasks to process files in separate directories.
The following key tasks of the graph use files in the corresponding directories as inputs:
So if any changes are made in src/main/java directory, only compileJava task will execute. Similarly any changes in resources will trigger processResources or processTestResources depending on which directory the file are changed. Same case with test java directory as well. This will optimize the total time Gradle takes for a build in a continuous build setup.
In case if the compilation is failed at src/main/java , subsequent changes to the test source src/test/java will not initiate a new build since there is no point in building the test where main source compilation is failed.
Watch our video on how to trigger a continuous build in gradle: