Skip to content

Commit 653196f

Browse files
authored
Use version catalog to drive java platform (#531)
* Use version catalog to drive Java platform This commit incorporates the use of the Gradle version catalog as well as the related plugins to automatically update the libs.version.toml versions file with updated dependencies. The UpdateProjectVersionPlugin is also updated to modify the spring boot version property in the `libs.versions.toml` versions file rather than gradle.properties. Resolves #523
1 parent b0a81f9 commit 653196f

File tree

28 files changed

+381
-170
lines changed

28 files changed

+381
-170
lines changed

build.gradle

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ plugins {
22
id 'io.spring.nohttp'
33
id 'org.springframework.pulsar.root-project'
44
id 'org.springframework.pulsar.update-version'
5-
id 'org.ajoberstar.grgit' version '4.0.1' apply false
5+
alias(libs.plugins.grgit) apply false
6+
alias(libs.plugins.versions)
7+
alias(libs.plugins.version.catalog.update)
68
}
79

810
description = 'Spring for Apache Pulsar'
911

1012
apply from: 'gradle/aggregate-jacoco-report.gradle'
1113
apply from: 'gradle/update-copyrights.gradle'
12-
14+
apply from: 'gradle/version-catalog-update.gradle'
1315

1416
allprojects {
1517
group = 'org.springframework.pulsar'
@@ -53,3 +55,11 @@ tasks.register('runAllSampleTests') {
5355
allTestsTasks
5456
}
5557
}
58+
59+
versionCatalogUpdate {
60+
versionCatalogs {
61+
buildLibsCatalog {
62+
catalogFile = file("gradle/build-libs.versions.toml")
63+
}
64+
}
65+
}

buildSrc/build.gradle

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,12 @@ repositories {
1010
gradlePluginPortal()
1111
}
1212

13-
new File(new File("$projectDir").parentFile, "gradle.properties").withInputStream {
14-
def properties = new Properties()
15-
properties.load(it)
16-
ext.set("springFrameworkVersion", properties["springFrameworkVersion"])
17-
if (properties["springFrameworkVersion"].contains("-")) {
18-
repositories {
19-
maven { url "https://repo.spring.io/milestone" }
20-
maven { url "https://repo.spring.io/snapshot" }
21-
}
13+
def versionCatalog = extensions.getByType(VersionCatalogsExtension).named("buildLibs")
14+
def springVersion = versionCatalog.findVersion("spring").orElseThrow().displayName
15+
if (springVersion.contains("-")) {
16+
repositories {
17+
maven { url "https://repo.spring.io/milestone" }
18+
maven { url "https://repo.spring.io/snapshot" }
2219
}
2320
}
2421

@@ -38,24 +35,22 @@ configurations {
3835
}
3936
}
4037

41-
ext.set("springFrameworkVersion", "6.0.10")
42-
4338
dependencies {
4439
implementation localGroovy()
45-
implementation 'commons-codec:commons-codec:1.15'
46-
implementation 'com.fasterxml.jackson.core:jackson-databind:2.11.4'
47-
implementation 'io.github.gradle-nexus:publish-plugin:1.3.0'
48-
implementation("io.spring.javaformat:spring-javaformat-gradle-plugin:0.0.38")
49-
implementation 'io.spring.nohttp:nohttp-gradle:0.0.11'
50-
implementation "org.apache.maven:maven-embedder:3.6.3"
51-
implementation "org.asciidoctor:asciidoctor-gradle-jvm:3.3.2"
52-
implementation 'org.codehaus.groovy:groovy-all:2.5.17'
53-
implementation 'org.jfrog.buildinfo:build-info-extractor-gradle:4.31.9'
54-
implementation "org.gradle:test-retry-gradle-plugin:1.4.0"
55-
implementation 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.7.1'
56-
implementation(platform("org.springframework:spring-framework-bom:${springFrameworkVersion}"))
57-
implementation 'org.springframework:spring-core'
58-
implementation 'org.springframework:spring-web'
40+
implementation buildLibs.asciidoc.gradle.jvm
41+
implementation buildLibs.commons.codec
42+
implementation buildLibs.groovy.all
43+
implementation buildLibs.jfrog.build.extractor.gradle
44+
implementation buildLibs.jackson.databind
45+
implementation buildLibs.javaformat.gradle
46+
implementation buildLibs.maven.embedder
47+
implementation buildLibs.nexus.publish
48+
implementation buildLibs.nohttp.gradle
49+
implementation buildLibs.sonarqube.gradle
50+
implementation(platform(buildLibs.spring.bom))
51+
implementation buildLibs.spring.core
52+
implementation buildLibs.spring.web
53+
implementation buildLibs.test.retry.gradle
5954
}
6055

6156
gradlePlugin {

buildSrc/settings.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,11 @@ pluginManagement {
44
gradlePluginPortal()
55
}
66
}
7+
8+
dependencyResolutionManagement {
9+
versionCatalogs {
10+
buildLibs {
11+
from(files("../gradle/build-libs.versions.toml"))
12+
}
13+
}
14+
}

buildSrc/src/main/java/org/springframework/pulsar/gradle/AbstractSpringModulePlugin.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.gradle.api.plugins.PluginManager;
2424

2525
import org.springframework.pulsar.gradle.optional.OptionalDependenciesPlugin;
26-
import org.springframework.pulsar.gradle.publish.SpringMavenPlugin;
2726

2827
import io.spring.gradle.convention.RepositoryConventionPlugin;
2928

buildSrc/src/main/java/org/springframework/pulsar/gradle/JavaConventionsPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private void configureSpringJavaFormat(Project project) {
123123
project.getTasks().withType(Format.class, (Format) -> Format.setEncoding("UTF-8"));
124124
project.getPlugins().apply(CheckstylePlugin.class);
125125
CheckstyleExtension checkstyle = project.getExtensions().getByType(CheckstyleExtension.class);
126-
checkstyle.setToolVersion("8.45.1");
126+
checkstyle.setToolVersion("10.12.4");
127127
checkstyle.getConfigDirectory().set(project.getRootProject().file("src/checkstyle"));
128128
String version = SpringJavaFormatPlugin.class.getPackage().getImplementationVersion();
129129
DependencySet checkstyleDependencies = project.getConfigurations().getByName("checkstyle").getDependencies();

buildSrc/src/main/java/org/springframework/pulsar/gradle/check/JacocoConventionsPlugin.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.gradle.api.Plugin;
44
import org.gradle.api.Project;
5+
import org.gradle.api.artifacts.VersionCatalogsExtension;
56
import org.gradle.api.plugins.JavaPlugin;
67
import org.gradle.api.tasks.testing.Test;
78
import org.gradle.testing.jacoco.plugins.JacocoPlugin;
@@ -19,8 +20,10 @@ public class JacocoConventionsPlugin implements Plugin<Project> {
1920
public void apply(final Project project) {
2021
project.getPlugins().withType(JavaPlugin.class, (javaPlugin) -> {
2122
project.getPluginManager().apply(JacocoPlugin.class);
23+
VersionCatalogsExtension catalog = project.getExtensions().getByType(VersionCatalogsExtension.class);
24+
String jacocoVeresion = catalog.named("libs").findVersion("jacoco").orElseThrow().getDisplayName();
2225
project.getExtensions().configure(JacocoPluginExtension.class,
23-
(jacocoExtension) -> jacocoExtension.setToolVersion("0.8.9"));
26+
(jacocoExtension) -> jacocoExtension.setToolVersion(jacocoVeresion));
2427
project.getTasks().withType(Test.class, (test) ->
2528
project.getTasks().withType(JacocoReport.class, test::finalizedBy));
2629
});

buildSrc/src/main/java/org/springframework/pulsar/gradle/versions/UpdateProjectVersionTask.java

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,50 +17,57 @@
1717
package org.springframework.pulsar.gradle.versions;
1818

1919
import java.io.File;
20-
import java.util.Objects;
2120
import java.util.function.Function;
2221
import java.util.regex.Matcher;
2322
import java.util.regex.Pattern;
2423

2524
import org.gradle.api.DefaultTask;
2625
import org.gradle.api.Project;
26+
import org.gradle.api.artifacts.VersionCatalogsExtension;
2727

2828
import org.springframework.lang.Nullable;
2929

3030
public abstract class UpdateProjectVersionTask extends DefaultTask {
3131

3232
static final String VERSION_PROPERTY = "version";
3333

34-
static final String SPRING_BOOT_VERSION_PROPERTY = "springBootVersionForDocs";
34+
static final String SPRING_BOOT_VERSION_PROPERTY = "spring-boot-for-docs";
3535

3636
static final Pattern VERSION_PATTERN = Pattern.compile("^([0-9]+)\\.([0-9]+)\\.([0-9]+)(-M\\d+|-RC\\d+|-SNAPSHOT)?$");
3737

3838
protected void updateVersionInGradleProperties(String newVersion) {
39-
this.updatePropertyInGradleProperties(VERSION_PROPERTY, (p) -> p.getVersion().toString(), newVersion);
39+
this.updatePropertyInFile(Project.GRADLE_PROPERTIES, VERSION_PROPERTY,
40+
(p) -> p.getVersion().toString(),
41+
(__) -> newVersion,
42+
(currentValue) -> "%s=%s".formatted(VERSION_PROPERTY, currentValue),
43+
(__) -> "%s=%s".formatted(VERSION_PROPERTY, newVersion));
4044
}
4145

42-
protected void updatePropertyInGradleProperties(String propertyName, String newPropertyValue) {
43-
this.updatePropertyInGradleProperties(propertyName,
44-
(p) -> Objects.toString(p.findProperty(propertyName), ""), newPropertyValue);
46+
protected void updateVersionInTomlVersions(String versionPropertyName,
47+
Function<String, String> newPropertyValueGivenCurrentValue) {
48+
this.updatePropertyInFile("gradle/libs.versions.toml", versionPropertyName,
49+
(p) -> currentVersionForProperty(p, versionPropertyName),
50+
newPropertyValueGivenCurrentValue,
51+
(currentValue) -> "%s = \"%s\"".formatted(versionPropertyName, currentValue),
52+
(newValue) -> "%s = \"%s\"".formatted(versionPropertyName, newValue));
4553
}
4654

47-
protected void updatePropertyInGradleProperties(
48-
String propertyName,
49-
Function<Project, String> currentPropertyValueFromProject,
50-
String newPropertyValue) {
51-
String currentPropertyValue = currentPropertyValueFromProject.apply(getProject());
52-
File gradlePropertiesFile = getProject().getRootProject().file(Project.GRADLE_PROPERTIES);
53-
if (!gradlePropertiesFile.exists()) {
54-
throw new RuntimeException("No gradle.properties to update property in");
55+
protected void updatePropertyInFile(String propertyFile, String propertyName,
56+
Function<Project, String> currentPropertyValueGivenProject,
57+
Function<String, String> newPropertyValueGivenCurrentValue,
58+
Function<String, String> expectedCurrentPropertyEntryInFile,
59+
Function<String, String> newPropertyEntryInFile) {
60+
File file = getProject().getRootProject().file(propertyFile);
61+
if (!file.exists()) {
62+
throw new RuntimeException("File not found at " + propertyFile);
5563
}
56-
System.out.printf("Updating the %s property in %s from %s to %s%n",
57-
propertyName, Project.GRADLE_PROPERTIES, currentPropertyValue, newPropertyValue);
58-
FileUtils.replaceFileText(gradlePropertiesFile, (gradlePropertiesText) -> {
59-
gradlePropertiesText = gradlePropertiesText.replace(
60-
"%s=%s".formatted(propertyName, currentPropertyValue),
61-
"%s=%s".formatted(propertyName, newPropertyValue));
62-
return gradlePropertiesText;
63-
});
64+
String currentValue = currentPropertyValueGivenProject.apply(getProject());
65+
String newValue = newPropertyValueGivenCurrentValue.apply(currentValue);
66+
System.out.printf("Updating the %s property in %s from %s to %s%n", propertyName,
67+
propertyFile, currentValue, newValue);
68+
FileUtils.replaceFileText(file, (propertiesText) -> propertiesText.replace(
69+
expectedCurrentPropertyEntryInFile.apply(currentValue),
70+
newPropertyEntryInFile.apply(newValue)));
6471
}
6572

6673
protected VersionInfo parseVersion(String version) {
@@ -78,6 +85,13 @@ protected VersionInfo parseVersion(String version) {
7885
}
7986
}
8087

88+
protected String currentVersionForProperty(Project project, String versionProperty) {
89+
VersionCatalogsExtension catalog = project.getExtensions().getByType(VersionCatalogsExtension.class);
90+
return catalog.named("libs").findVersion(versionProperty)
91+
.orElseThrow(() -> new IllegalStateException("% property not found".formatted(versionProperty)))
92+
.getDisplayName();
93+
}
94+
8195
record VersionInfo(String major, String minor, String patch, @Nullable String modifier) {
8296
}
8397
}

buildSrc/src/main/java/org/springframework/pulsar/gradle/versions/UpdateToReleaseVersionTask.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 the original author or authors.
2+
* Copyright 2019-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,13 +16,9 @@
1616

1717
package org.springframework.pulsar.gradle.versions;
1818

19-
import java.util.Objects;
20-
2119
import org.gradle.api.tasks.Input;
2220
import org.gradle.api.tasks.TaskAction;
2321

24-
import org.springframework.util.Assert;
25-
2622
public abstract class UpdateToReleaseVersionTask extends UpdateProjectVersionTask {
2723

2824
@Input
@@ -31,7 +27,7 @@ public abstract class UpdateToReleaseVersionTask extends UpdateProjectVersionTas
3127
@TaskAction
3228
public void updateToReleaseVersion() {
3329
updateVersionInGradleProperties(this.releaseVersion);
34-
updatePropertyInGradleProperties(SPRING_BOOT_VERSION_PROPERTY, calculateReleaseBootVersion());
30+
updateVersionInTomlVersions(SPRING_BOOT_VERSION_PROPERTY, this::calculateReleaseBootVersion);
3531
}
3632

3733
public String getReleaseVersion() {
@@ -42,9 +38,7 @@ public void setReleaseVersion(String releaseVersion) {
4238
this.releaseVersion = releaseVersion;
4339
}
4440

45-
private String calculateReleaseBootVersion() {
46-
String currentBootVersion = Objects.toString(getProject().findProperty(SPRING_BOOT_VERSION_PROPERTY), null);
47-
Assert.notNull(currentBootVersion, () -> "% property not found".formatted(SPRING_BOOT_VERSION_PROPERTY));
41+
private String calculateReleaseBootVersion(String currentBootVersion) {
4842
VersionInfo bootVersionSegments = parseVersion(currentBootVersion);
4943
String releaseBootVersion = "%s.%s.%s".formatted(bootVersionSegments.major(), bootVersionSegments.minor(), bootVersionSegments.patch());
5044
String releaseVersionModifier = parseVersion(this.releaseVersion).modifier();
Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 the original author or authors.
2+
* Copyright 2019-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,21 +16,15 @@
1616

1717
package org.springframework.pulsar.gradle.versions;
1818

19-
import java.util.Objects;
20-
2119
import org.gradle.api.tasks.TaskAction;
2220

23-
import org.springframework.util.Assert;
24-
2521
public abstract class UpdateToSnapshotVersionTask extends UpdateProjectVersionTask {
2622

2723
@TaskAction
2824
public void updateToSnapshotVersion() {
2925
String currentVersion = getProject().getVersion().toString();
3026
updateVersionInGradleProperties(calculateNextSnapshotVersion(currentVersion));
31-
String currentBootVersion = Objects.toString(getProject().findProperty(SPRING_BOOT_VERSION_PROPERTY), null);
32-
Assert.notNull(currentBootVersion, () -> "% property not found".formatted(SPRING_BOOT_VERSION_PROPERTY));
33-
updatePropertyInGradleProperties(SPRING_BOOT_VERSION_PROPERTY, calculateNextSnapshotVersion(currentBootVersion));
27+
updateVersionInTomlVersions(SPRING_BOOT_VERSION_PROPERTY, this::calculateNextSnapshotVersion);
3428
}
3529

3630
private String calculateNextSnapshotVersion(String version) {
@@ -45,12 +39,4 @@ private String calculateNextSnapshotVersion(String version) {
4539
}
4640
return "%s.%s.%s-SNAPSHOT".formatted(majorSegment, minorSegment, patchSegment);
4741
}
48-
49-
private String calculateCurrentSnapshotVersion(String version) {
50-
VersionInfo versionSegments = parseVersion(version);
51-
String majorSegment = versionSegments.major();
52-
String minorSegment = versionSegments.minor();
53-
String patchSegment = versionSegments.patch();
54-
return "%s.%s.%s-SNAPSHOT".formatted(majorSegment, minorSegment, patchSegment);
55-
}
5642
}

gradle.properties

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,3 @@ version=1.0.2-SNAPSHOT
33
org.gradle.caching=true
44
org.gradle.parallel=true
55
org.gradle.jvmargs=-Xmx4g -XX:+HeapDumpOnOutOfMemoryError -XX:+UseParallelGC -Dfile.encoding=UTF-8
6-
7-
pulsarClientVersion=3.1.1
8-
pulsarClientReactiveVersion=0.5.1
9-
springFrameworkVersion=6.1.2
10-
11-
# only used by integration tests and samples (unpublished deps)
12-
springBootVersion=3.2.2-SNAPSHOT
13-
14-
# only used for docs (links)
15-
springBootVersionForDocs=3.2.2-SNAPSHOT
16-
17-
springCloudStreamVersion=4.1.0

0 commit comments

Comments
 (0)