What Is Gradle Scan?
Posted on April 17, 2019  (Last modified on May 1, 2024 )
3 minutes • 494 words
This project uses these versions of languages, frameworks, and libraries.
-
gradle
: 5.4
This tutorial may work with newer versions and possibly older versions, but has only been tested on the versions mentioned above.
A Gradle scan is a build scan that provides insights into how your build ran and why it did what it did. Once you’ve generated a scan, you can share that with other members of your team (or anyone who may help diagnose issues) via a URL. Here’s a sample scan !
Creating a Gradle Scan
Creating a Gradle scan is extremely easy! In your existing gradle project simply run ./gradlew build --scan
or gradle build --scan
if you’re not using the gradle wrapper.
You’ll be prompted to accept the Gradle Cloud Services license agreement. To proceed, you’ll need to accept the agreement, but as always, you should read through and evaluate if you should accept the agreement.
Ultimately, if you accept, you should see something like https://gradle.com/s/czajmbyg73t62
printed to the console at the end of the command. That link is your Gradle scan — Open it in a browser
!
Grokking the Gradle Scan
There’s a lot of information presented in an unconfigured scan, and parsing it can be confusing. Thankfully, Gradle has broken the scan down into relevant chunks of information, such as Plugins , Switches , or the Timeline .
Plugins merely shows what plugins are enabled in this gradle build, while switches does the same — but for switches.
The timeline, however, shows the stages of your build over time. This is extremely helpful for profiling your build and figuring out what exactly is taking so long. While the build I shared above is quite simple, you’ll likely find that in your application the timeline will be full of information, especially so the more complicated your build is.
Configuring the Scan
Perhaps one of the nicest features, is the ability to provide additional information from the environment in your scan. For example: you may want to publish a scan and tie it to a commit hash, or a git tag. You can do that simply by specifying additional configuration options in the scan’s task.
buildScan {
background {
def commitId = 'git rev-parse --verify HEAD'.execute().text.trim()
value "Git Commit ID", commitId
}
}
It’s worth mentioning that, as an effort to avoid slowing builds down with this operation, this is ran on a background thread (as illustrated by the “background” block in that configuration). Ideally, any additional information should now impact the build — It wouldn’t be as useful of a tool if it did!
Hopefully you found this post on Gradle scans helpful! If you’d like to learn more about Gradle, you can find my posts on the subject here or the official docs here !