Gradle Cheat Sheet
The Gradle Java plugin extends the Base plugin and provides the tasks needed to build a Java (or Kotlin) project. They resemble the Maven build phases for those familiar with Maven. See also Migrating Builds From Apache Maven - Understanding the build lifecycle However, the output from Gradle generally is very different than that of Maven, usually more concise. This is not always what you want. Below you will find some nice commands, options, and configurations that can make the output to the console more complete (a bit more like Maven). This can be particularly helpful when building in a pipeline (eg. GitLab, GitHub, etc.), in which case log files and build reports are usually less or not available. Most of the time all you have is the output to standard out, in which case you would like it to be as complete as possible, specially when the build fails.
Commands and options
Do not overwrite output
Use the --console
option for that, eg.:
gradle --console=verbose test
Run a single test
Single test method:
gradle <module>:test --tests <test.class.full.name>.<method.name>
Single test class:
gradle <module>:test --tests <test.class.full.name>
NB. Module is aka. subproject.
Configurations
Add the following code to your build.gradle
.
For the structure of a build.gradle
file see also Build script structure
To see all dependencies
To see the dependencies of a single module use:
gradle -q dependencies
or
gradle -q <module>:dependencies
To see them for all modules:
// To see all dependencies: gradle printAllDependencies
// See also https://stackoverflow.com/questions/44266687/how-to-print-out-all-dependencies-in-a-gradle-multi-project-build
allprojects {
task printAllDependencies(type: DependencyReportTask) {}
}
Better logging from tests
subprojects {
test {
testLogging {
events "failed"
exceptionFormat "full"
}
}
}
See also TestLoggingContainer
If you do not want to use subprojects
see Sharing Build Logic between Subprojects.
Summarize failing tests at end of build
allprojects {
// add a collection to track failedTests
ext.failedTests = []
// add a testlistener to all tasks of type Test
tasks.withType(Test) {
afterTest { TestDescriptor descriptor, TestResult result ->
if (result.resultType == org.gradle.api.tasks.testing.TestResult.ResultType.FAILURE) {
failedTests << ["${descriptor.className}::${descriptor.name}"]
}
}
}
// print out tracked failed tests when the build has finished
gradle.buildFinished {
if (!failedTests.empty) {
println "Failed tests for ${project.name}:"
failedTests.each { failedTest ->
println failedTest
}
println ""
}
}
}
Show all tasks
gradle tasks [--all]
Show task graph
plugins {
id "com.dorongold.task-tree" version "2.1.1"
}
Usage:
gradle <task 1>...<task N> taskTree
Example:
gradle build taskTree