Notes on gradle
Cheats or Frequently used
Gradle Command | Description |
---|---|
gradle projects | List Gradle projects |
gradle tasks –all | List all of the tasks that can be run |
gradle dependencies | Like mvn depeendency:tree |
gradle clean build | Like mvn clean install |
gradle helmInstall | Build docker image and deploy it to K8S (custom task) |
How multi-projects work
In this example, there are three directories the root directory and two sub-directories.
Root Project
+ subProject1
+ subProject2
When gradle clean
is run, it runs in the Root and in the two subProjects.
Gradle executes clean on the root and every subproject, because clean is a task that exists in every project and follows Gradle’s task execution model
The clean task is a lifecycle task defined in the base plugin, which is applied automatically in Java projects.
On the other hand, gradle dependencies
only runs on the Root directory. This is because the dependencies
task
is not a global task—it belongs to each project separately.
Testing
How can I run a single test in Gradle (from command line)?
# Specific Test File (all tests in this file are run)
gradle subproject:test --tests com.example.play.MySampleTest
# Specific Test doItTest() File (only this exact test is run)
gradle subproject:test --tests com.example.play.MySampleTest#doItTest
# Run all Tests whos filename matches 'Base*Test'
gradle subproject:test --tests "Base*Test"
Where is log file output
./gradlew test --info
./gradlew test --debug
Will show you the output that you normally don’t see
ls -l subproject/build/test-results/test
less subproject/build/test-results/test/TEST-com.example.play.MySampleTest.xml
<system.out><![CDATA[
... this is where you'll see the log output
]]></system-out>
<system-err><![CDATA[]]><system-err>
The XML file contains the log output including a stack trace if one exists. It also has the success/failure of the test.
Tip: If you just want everything (logs, test outputs) nicely in console while developing, add this once to your build.gradle:
test {
testLogging {
events "passed", "skipped", "failed", "standard_out", "standard_error"
showStandardStreams = true
}
}
Misc
How can I run a command in build.gradle?
task buildDockerImage(type:Exec, dependsOn: docker) {
commandLine "docker"
args = ["build"]
}
task pushDockerImage(type:Exec, dependsOn: docker) {
commandLine "docker"
args = ["push", "harbor.example.com/myproj/myapp:latest"]
}