vault backup: 2025-12-10 11:37:35

This commit is contained in:
2023-05-15 17:16:05 +02:00
committed by Thomas Peetz
parent 91bf72fc87
commit 73f2162ddf
6049 changed files with 513094 additions and 227748 deletions
@@ -0,0 +1,44 @@
---
title: Gradle Goodness
source: https://blog.jdriven.com/2021/02/gradle-goodness-setting-plugin-version-from-property-in-plugins-section/
tags:
- IT/Development/Gradle
---
The `plugins` section in our Gradle build files can be used to define Gradle plugins we want to use. Gradle can optimize the build process if we use `plugins {…​}` in our build scripts, so it is a good idea to use it. But there is a restriction if we want to define a version for a plugin inside the `plugins` section: the version is a fixed string value. We cannot use a property to set the version inside the `plugins` section. We can overcome this by using a `pluginsManagement` section in a settings file in the root of our project. Inside the `pluginsManagement` section we can use properties to set the version of a plugin we want to use. Once it is defined inside `pluginsManagement` we can use it in our project build script without having the specify the version. This allows us to have one place where all plugin versions are defined. We can even use a `gradle.properties` file in our project with all plugin versions and use that in `pluginsManagement`.
In the following settings file we use `pluginsManagement` to use a project property `springBootPluginVersion` to set the version to use for the Spring Boot Gradle plugin.
```kotlin
// File: settings.gradle.kts
pluginManagement {
val springBootPluginVersion: String by settings // use project property with version
plugins {
id("org.springframework.boot") version "${springBootPluginVersion}"
}
}
```
Next in our project build file we can simply reference the id of the Spring Boot Gradle plugin without the version. The version is already resolved in our settings file:
```kotlin
// File: build.gradle.kts
plugins {
java
application
id("org.springframework.boot") // no version here: it is set in settings.gradle.kts
}
application {
mainClass.set("com.mrhaki.sample.App")
}
```
Finally we can add a `gradle.properties` file with the project property (or specify it on the command line or environment variable):
```properties
# File: gradle.properties
springBootPluginVersion=2.4.2
```
Written with Gradle 6.8.2.