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'

gradleHelloWorldTasks

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

gradleHelloWorldTasksAll

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.

gradle_display_properties

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.

gradleProfilerOutput

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.

gradle_dry_run_output

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.
 

Reference: Gradle Command-Line Conveniences from our JCG partner Dustin Marx at the Inspired by Actual Events blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


eight − 4 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close