Simple Example
$ vi build.gradle
task helloWorld { doLast { println "Hellow World" <-- groovy } }
Run it
$ gradle hellowWorld
Create the Wrapper
Issue the following command to create the wrapper
$ gradle wrapper
Store the wrapper with your source code
build.gradle
settings.gradle
Example settings.gradle file
rootProject.name = "starter-example"
gradle.properties
Example gradle.properties file
org.gradle.logging.level = info version = 1.0.0
Use the variable in your build.gradle file like so:
task helloWorld { doLast { println "Hellow World, version = " + version } }
Task Dependencies
We can add dependsOn to the task to make it depend on the other.
task copyDocs(type: Copy) { from "src" into "build/docs" include "**/*.md" includeEmptyDir = false } task createZip(type: Zip) { from "build/docs" archiveFilename = "docs.zip" destinationDirectory = file("build/dist") dependsOn copyDocs }
To see the order without executing the tasks, add --dry-run to the end.
$ gradle hellowWorld --dry-run
Another way to visualize the gradle tree you can use gradle-task-tree.
See https://github.com/dorongold/gradle-task-tree
Tasks
List all available tasks
$ gradle tasks --all
Copy Task
task copyDocs(type: Copy) { from "src" into "build/docs" include "**/*.md" includeEmptyDir = false }
Zip
task createZip(type: Zip) { from "build/docs" archiveFilename = "docs.zip" destinationDirectory = file("build/dist") }
Lifecycle Phases
Initialization Phase
- evaluates settings file and sets up build
Configuration Phase
- evaluates build scripts and runs configuration logic.
Execution Phase
- executes task actions in correct order.
Configuration Logic
Execution Logic
Dependencies
web-service/build.gradle
dependencies { implementation project(':utils') implementation project(':api') }
References
Reference | URL |
---|---|
User Manual | https://docs.gradle.org/current/userguide/userguide.html |
Multi-Project Builds | https://docs.gradle.org/current/userguide/intro_multi_project_builds.html |
Dependencies | https://docs.gradle.org/current/userguide/declaring_dependencies.html#declaring-dependencies |
The Java Plugin | https://docs.gradle.org/current/userguide/java_plugin.html |