Thursday, December 26, 2013

Gradle Command-Line Conveniences

In my post A First Look at Building Java with Gradle, I briefly mentioned using Gradle's "gradle tasks" command to see the available tasks for a particular Gradle build. In this post, I expand on that brief mention a bit more and look at some related Gradle command-line conveniences.

Gradle makes it easy to determine available Gradle tasks for a given project. The next screen snapshot demonstrates using gradle tasks (or gradle :tasks) in the same directory as the simple Gradle build file (build.gradle) I used in my previously mentioned Gradle post. The screen snapshots follow the code listing of build.gradle which is reproduced here for convenience.

Basic Java Application build.gradle
apply plugin: 'java'

Adding --all to gradle tasks (gradle tasks --all) will show even more details (including tasks' dependencies) as shown in the next screen snapshot.

Something I used to do often when working with an Ant build file new to me was to add a target "showProperties" that used a series of Ant echo tasks to display the properties used by that build file. Gradle provides essentially this capability out-of-the-box. The next screen snapshot demonstrates use of gradle -q properties to display the properties associated with the Gradle project and tasks in the build.gradle file in the same directory.

Another useful command-line Gradle option is --profile. This can be used in conjunction with running a Gradle command. For example, gradle tasks --profile generates the same standard output as shown above, but also writes build performance statistics to a file with the naming convention profile-YYYY-MM-DD-HH-mm-ss.html in the build/reports/profile subdirectory of the directory from which the build was executed. An example of that generated file is shown next.

The final Gradle command-line option I cover in this post is the "dry run" option -m (or --dry-run). This option allows one to see the Gradle tasks which are run and the order in which they are run without actually executing those tasks. Because the one-line Gradle build.gradle file used in this post applies the Java plugin, the automatically added Gradle Tasks include compileJava, classes, jar, and javadoc. The following screen snapshot demonstrates running gradle -m jar to see the dry run output that shows the dependent tasks that must be run before "jar" and the order they must be run (compileJava->processResources->classes->jar). Note "SKIPPED" notation indicating that the Gradle tasks are not actually executed.

Chapter 11 ("Using the Gradle Command-Line") of the Gradle User Guide (PDF) contains additional details regarding use of Gradle's command-line interface with sections on listing projects, listing tasks, listing project dependencies, listing project properties, and listing the order Gradle tasks are executed.

No comments: