---
description: https://docs.gradle.org/current/userguide/userguide_single.html
---

# Gradle

[Great official userguide with walk through examples](https://docs.gradle.org/current/userguide/userguide.html)

## Configuration

[LINK](https://docs.gradle.org/current/userguide/dependency_management_terminology.html#sub:terminology_configuration) Configuration is a named set of dependencies grouped together as a specific goal.&#x20;

## Source sets

* the source files and where they’re located
* the compilation classpath, including any required dependencies (via Gradle [configurations](https://docs.gradle.org/current/userguide/dependency_management_terminology.html#sub:terminology_configuration))
* where the compiled class files are placed

[LINK](https://docs.gradle.org/current/userguide/building_java_projects.html)

![](https://415484505-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LxtoAXZwwOc4XGto8vb%2F-M77Au1vuWh_1o5cOFbi%2F-M77CAE3Gevguuqvd6Ii%2Fjava-sourcesets-compilation.png?alt=media\&token=d9b2e0dd-b6d7-4611-b6aa-fceaf0dc3854)

_sourceSet_ is a placeholder (for main source set it is default)

![](https://415484505-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LxtoAXZwwOc4XGto8vb%2F-M77CsUVKzmWRDPELnMQ%2F-M77CyiTBeRUXMrevEpd%2Fjava-sourcesets-process-resources.png?alt=media\&token=ec9adc23-0a16-4e60-8831-0ba875800a58)

## Dependencies

* **Repository** => e.g. mavenCentral()
* **Configuration** => e.g. implementation
* **Module** **coordinate** => '_\<groupId>_:_\<artifactId>_:_\<version>_'

### Dependency configurations

Every dependency declared for a Gradle project applies to a specific scope. Many Gradle plugins add pre-defined configurations to your project.

![](https://415484505-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LxtoAXZwwOc4XGto8vb%2F-M77D-2ZRT4916hG85nX%2F-M77HGJDlLdrpTZ0-Quy%2Fdependency-management-configurations.png?alt=media\&token=00496901-0c58-40ed-b1df-c6d09121724e)

#### Configuration inheritance and composition

Child configurations inherit the whole set of dependencies declared for any of its superconfigurations.

![](https://415484505-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LxtoAXZwwOc4XGto8vb%2F-M77HNQsYyA6OtfBWCnI%2F-M77HewUvg1q3g38Y_4_%2Fdependency-management-configuration-inheritance.png?alt=media\&token=c41c79ed-2f2c-45b0-839b-2662b5dc2d2e)

Under the covers the `testImplementation` and `implementation` configurations form an inheritance hierarchy by calling the method [Configuration.extendsFrom(org.gradle.api.artifacts.Configuration\[\])](https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.Configuration.html#org.gradle.api.artifacts.Configuration:extendsFrom\(org.gradle.api.artifacts.Configuration\[]\))

#### Example

Let’s say you wanted to write a suite of smoke tests. Each smoke test makes a HTTP call to verify a web service endpoint. As the underlying test framework the project already uses JUnit. You can define a new configuration named `smokeTest` that extends from the `testImplementation` configuration to reuse the existing test framework dependency.

```
configurations {
    smokeTest.extendsFrom testImplementation
}

dependencies {
    testImplementation 'junit:junit:4.12'
    smokeTest 'org.apache.httpcomponents:httpclient:4.5.5'
}
```

### [Resolvable and consumable configurations](https://docs.gradle.org/current/userguide/declaring_dependencies.html#sec:resolvable-consumable-configs) <a href="#sec-resolvable-consumable-configs" id="sec-resolvable-consumable-configs"></a>

Configurations have at least 3 different roles:

1. to declare dependencies
2. as a _consumer_, to resolve a set of dependencies to files
3. as a _producer_, to expose artifacts and their dependencies for consumption by other projects (such _consumable_ configurations usually represent the [variants](https://docs.gradle.org/current/userguide/variant_model.html) the producer offers to its consumers)

```
configurations {
    // declare a "configuration" named "someConfiguration"
    // it doesn’t tell us how the configuration is meant to be used
    someConfiguration
}
dependencies {
    // add a project dependency to the "someConfiguration" configuration
    someConfiguration project(":lib")
}
```

```
configurations {
    // declare a configuration that is going to resolve the compile classpath of the application
    compileClasspath.extendsFrom(someConfiguration)

    // declare a configuration that is going to resolve the runtime classpath of the application
    runtimeClasspath.extendsFrom(someConfiguration)
}
```

### Api vs Implementation

* api - both a compile and runtime dependency on this artifact
* implementation - only a runtime dependency on this artifact&#x20;

[Good article with explanation](https://tomgregory.com/how-to-use-gradle-api-vs-implementation-dependencies-with-the-java-library-plugin/)

## Java plugin

![](https://415484505-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LxtoAXZwwOc4XGto8vb%2F-M77D-2ZRT4916hG85nX%2F-M77Ej3nOILk9mjiabiB%2FjavaPluginTasks.png?alt=media\&token=78de1fe9-0975-4bc6-92d0-407e82265084)

### Project layout&#x20;

[LINK](https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_project_layout)

* `src/main/java`
* `src/main/resources`
* `src/test/java`
* `src/test/resources`
* `src/`_`sourceSet`_`/java`
* `src/`_`sourceSet`_`/resources`

### Configurations

* **Gray text** — the configuration is _deprecated_.
* **Green background** — you can declare dependencies against the configuration.
* **Blue-gray background** — the configuration is for consumption by tasks, not for you to declare dependencies.
* **Light blue background with monospace text** — a task.

![Java plugin - main source set dependency configurations](https://415484505-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LxtoAXZwwOc4XGto8vb%2F-M77L5zO4ORR9E4lSbYn%2F-M77kZz3SFlArR6Naqc9%2Fjava-main-configurations.png?alt=media\&token=cab7771e-5229-4740-b2b4-5e50af1e7595)

![Java plugin - test source set dependency configurations](https://415484505-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LxtoAXZwwOc4XGto8vb%2F-M77mwxl2rS6zp48KyfQ%2F-M77wChKaYpCrEm0jwhx%2Fjava-test-configurations.png?alt=media\&token=8696baa0-bf6e-4558-9238-44d4d1e9d1cc)

### Java extension

```
java {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}
```

## Dependency hell

```
// investigate test class path
gradle -q dependencyInsight --dependency com.h2database --configuration testRuntimeClasspath

gradle -q dependencyInsight --dependency snakeyaml --configuration compileClasspath

// worked recently
gradle etl-core-app:dependencyInsight --dependency databind
```

## Modules

Great article about which kind of modules can exist in the application. [LINK](https://habr.com/ru/company/cian/blog/667776/)

Visualise modules. [Tool\_1](https://github.com/ivancarras/graphfity) [Tool\_2](https://github.com/savvasdalkitsis/module-dependency-graph)

### subprojects vs allprojects

key difference between "subprojects" and "allprojects" is that "subprojects" only applies the settings to the direct subprojects of the current project, while "allprojects" applies the settings to the current project and all of its subprojects.

## Build lifecycle

<figure><img src="https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FMcTJQXqCrIe1dpBxa1o4%2Fimage.png?alt=media&#x26;token=5f1c671b-068c-4357-acd3-58ec193e1091" alt="" width="375"><figcaption></figcaption></figure>

During the <mark style="background-color:orange;">configuration phase</mark>, Gradle finds the build script(s) in the root and subproject directories.

When a build script, `build.gradle(.kts)`, is found, Gradle configures a [Project](https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html) object.

The purpose of the [<mark style="background-color:orange;">Project</mark>](https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html) <mark style="background-color:orange;">object is to create a collection of</mark> [<mark style="background-color:orange;">Task</mark>](https://docs.gradle.org/current/javadoc/org/gradle/api/Task.html) <mark style="background-color:orange;">objects, apply plugins, and retrieve dependencies</mark>.

## Version catalog

[gradle docs](https://docs.gradle.org/current/userguide/platforms.html)
