Gradle Project API¶
The Gradle Project API (org.gradle.api.Project) is the central interface for interacting with a Gradle build, providing access to the build's lifecycle, files, dependencies, and tasks^[600-developer__gradle__gradle-build.md].
Core Advantages¶
Gradle provides a robust build framework characterized by several key advantages^[600-developer__gradle__gradle-build.md]:
- Flexibility: High degree of customization in build logic.
- Granularity: Fine-grained control over build components and execution.
- Extensibility: Support for plugins and custom logic expansion.
- Compatibility: Integration with existing tools and systems.
File Operations¶
The Project API provides convenient methods for interacting with the file system relative to the project directory^[600-developer__gradle__gradle-build.md].
File Location and Copying¶
- File Resolution: APIs are available to resolve file paths relative to the current project directory^[600-developer__gradle__gradle-build.md].
- Copying Files: File manipulation can be handled via
org.gradle.api.file.CopySpec^[600-developer__gradle__gradle-build.md].
File Traversal¶
Gradle allows for iterating through file collections using FileTreeElement^[600-developer__gradle__gradle-build.md].
Dependency Management¶
Projects can declare dependencies to resolve external libraries required for compilation or runtime^[600-developer__gradle__gradle-build.md].
Buildscript Configuration¶
The buildscript {} block is used to define the classpath for the build script itself, allowing the use of external libraries or plugins within the build logic^[600-developer__gradle__gradle-build.md].
Task Management¶
A project serves as a container for Tasks, which define the units of work to be executed^[600-developer__gradle__gradle-build.md].
Task Definition and Lifecycle¶
- Creation: Tasks can be created and registered within the project.
- Execution Actions: Logic can be attached to specific phases of a task's lifecycle using
doFirstanddoLastblocks^[600-developer__gradle__gradle-build.md]. - Execution Phase: Actions defined in
doFirstanddoLastare executed during the execution phase of the Gradle lifecycle^[600-developer__gradle__gradle-build.md].
Task Configuration¶
- Inputs and Outputs: Tasks declare inputs and outputs (via
TaskInputsandTaskOutputs) to support incremental builds and caching^[600-developer__gradle__gradle-build.md]. - Ordering (Dependencies): The execution order of tasks can be managed by defining dependencies or specifying that a task runs
afteranother task^[600-developer__gradle__gradle-build.md].
Script Configuration and Structure¶
Multi-Project Builds¶
The API supports the organization of complex builds through specific blocks and methods^[600-developer__gradle__gradle-build.md]:
* allprojects {}: Configures the current project and all its subprojects.
* subprojects {}: Configures only the subprojects of the current project.
* project(':module-name') {}: Manually imports or configures a specific sub-module.
External Scripting¶
Build logic can be modularized by applying external Gradle scripts^[600-developer__gradle__gradle-build.md].
* Syntax: apply from: file("script.[Gradle](<./gradle.md>)")
Properties¶
Project properties can be defined and managed via gradle.properties (using key-value pairs) or the ext {} block for extra properties^[600-developer__gradle__gradle-build.md].
Related Concepts¶
- Gradle
- [[Continuous Integration]]
Sources¶
^[600-developer__gradle__gradle-build.md]