Gradle Project and Build Script: Difference between revisions
(7 intermediate revisions by the same user not shown) | |||
Line 17: | Line 17: | ||
The project is a collection of [[Gradle Task#Overview|tasks]]. | The project is a collection of [[Gradle Task#Overview|tasks]]. | ||
=Top-Level Script Blocks= | =Top-Level Script Blocks= | ||
Line 35: | Line 31: | ||
{{Internal|Gradle_Multi-Project_Builds#subprojects|subprojects{...} usage examples}} | {{Internal|Gradle_Multi-Project_Builds#subprojects|subprojects{...} usage examples}} | ||
==repositories{}== | ==repositories{}== | ||
Used to declare and configure the repositories associated with this project. The repositories declared here will only be used to pull dependencies from, not publish against. For details on how to specify publishing repositories, see [[Gradle_Artifact_Publishing_Concepts#Overview|Gradle Artifact Publishing]]. This method executes the given configuration closure against the [https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.dsl.RepositoryHandler.html RepositoryHandler] for this project, which is passed to the closure as the closure's delegate. More details on on repositories and how to configured them are available in: {{Internal|Gradle Repositories#Overview|Gradle Repositories}} | Used to declare and configure the repositories associated with this project. The repositories declared here will only be used to pull dependencies from, not publish against. For details on how to specify publishing repositories, see [[Gradle_Artifact_Publishing_Concepts#Overview|Gradle Artifact Publishing]]. This method executes the given configuration closure against the [https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.dsl.RepositoryHandler.html RepositoryHandler] for this project, which is passed to the closure as the closure's delegate. More details on on repositories and how to configured them are available in: {{Internal|Gradle Repositories#Overview|Gradle Repositories}} | ||
=Project's Containers and Handlers= | =Project's Containers and Handlers= | ||
Line 69: | Line 45: | ||
** <span id='Publications_Container'></span>[[Gradle_Maven_Publish_Plugin#Mechanics|Publications Container]] | ** <span id='Publications_Container'></span>[[Gradle_Maven_Publish_Plugin#Mechanics|Publications Container]] | ||
** <span id='Publishing_Repository_Container'>[[Gradle_Maven_Publish_Plugin#Mechanics|Publishing Repository Container]] | ** <span id='Publishing_Repository_Container'>[[Gradle_Maven_Publish_Plugin#Mechanics|Publishing Repository Container]] | ||
=Reacting to Build Lifecycle Events= | =Reacting to Build Lifecycle Events= |
Latest revision as of 09:51, 22 October 2020
DEPLETE TO
Overview
A Project is the main API to use to interact with Gradle. All top level statements within a "build.gradle" build script are delegated to the corresponding Project instance and when executed, modify its state. The build.gradle configuration script is written in the Gradle DSL and it may contain any Groovy language element, variable declarations, script blocks, etc. Groovy single-quoted and double-quoted string literals can be used. The main difference is that double-quoted String literals support String interpolation.
In case of a multi-project build, It is possible to configure a project build from another build script associated with any project in the hierarchy. This capability is called cross-project configuration. Gradle implements cross-project configuration via configuration injection. Configuration injection is the default way to define common behavior.
build.gradle can be created automatically on project setup with gradle init. To make build scripts more concise, Gradle automatically adds the set of default Gradle import statements to the script.
The default name of the build script is "build.gradle" and in most cases there is no need to change it. It can be changed, though, in settings.gradle by setting the "buildFileName" property of the ProjectDescriptor associated with the project whose build script name we want to change:
project(':projectA').buildFileName = 'projectA.gradle'
The project is a collection of tasks.
Top-Level Script Blocks
allprojects{}
Applies the given configuration closure, in order, to the current project and all of its sub-projects.
subprojects{}
Applies the given configuration closure, in order, to all sub-projects of the current project.
repositories{}
Used to declare and configure the repositories associated with this project. The repositories declared here will only be used to pull dependencies from, not publish against. For details on how to specify publishing repositories, see Gradle Artifact Publishing. This method executes the given configuration closure against the RepositoryHandler for this project, which is passed to the closure as the closure's delegate. More details on on repositories and how to configured them are available in:
Project's Containers and Handlers
- Repository Handler
- Configuration Container
- Publishing extension:
Reacting to Build Lifecycle Events
A build lifecycle can be reacted to with code similar to the following examples.
More research necessary.
allprojects {
afterEvaluate { project ->
if (project.hasTests) {
println "Adding test task to $project"
project.task('test') {
doLast {
println "running tests for $project"
}
}
}
}
}
gradle.afterProject {project, projectState ->
if (projectState.failure) {
println "Evaluation of $project FAILED"
}
else {
println "Evaluation of $project succeeded"
}
}