Skip to content

Commit

Permalink
Use version catalog to drive java platform (#531)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
onobc authored Dec 27, 2023
1 parent b0a81f9 commit 653196f
Show file tree
Hide file tree
Showing 28 changed files with 381 additions and 170 deletions.
14 changes: 12 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ plugins {
id 'io.spring.nohttp'
id 'org.springframework.pulsar.root-project'
id 'org.springframework.pulsar.update-version'
id 'org.ajoberstar.grgit' version '4.0.1' apply false
alias(libs.plugins.grgit) apply false
alias(libs.plugins.versions)
alias(libs.plugins.version.catalog.update)
}

description = 'Spring for Apache Pulsar'

apply from: 'gradle/aggregate-jacoco-report.gradle'
apply from: 'gradle/update-copyrights.gradle'

apply from: 'gradle/version-catalog-update.gradle'

allprojects {
group = 'org.springframework.pulsar'
Expand Down Expand Up @@ -53,3 +55,11 @@ tasks.register('runAllSampleTests') {
allTestsTasks
}
}

versionCatalogUpdate {
versionCatalogs {
buildLibsCatalog {
catalogFile = file("gradle/build-libs.versions.toml")
}
}
}
45 changes: 20 additions & 25 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ repositories {
gradlePluginPortal()
}

new File(new File("$projectDir").parentFile, "gradle.properties").withInputStream {
def properties = new Properties()
properties.load(it)
ext.set("springFrameworkVersion", properties["springFrameworkVersion"])
if (properties["springFrameworkVersion"].contains("-")) {
repositories {
maven { url "https://repo.spring.io/milestone" }
maven { url "https://repo.spring.io/snapshot" }
}
def versionCatalog = extensions.getByType(VersionCatalogsExtension).named("buildLibs")
def springVersion = versionCatalog.findVersion("spring").orElseThrow().displayName
if (springVersion.contains("-")) {
repositories {
maven { url "https://repo.spring.io/milestone" }
maven { url "https://repo.spring.io/snapshot" }
}
}

Expand All @@ -38,24 +35,22 @@ configurations {
}
}

ext.set("springFrameworkVersion", "6.0.10")

dependencies {
implementation localGroovy()
implementation 'commons-codec:commons-codec:1.15'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.11.4'
implementation 'io.github.gradle-nexus:publish-plugin:1.3.0'
implementation("io.spring.javaformat:spring-javaformat-gradle-plugin:0.0.38")
implementation 'io.spring.nohttp:nohttp-gradle:0.0.11'
implementation "org.apache.maven:maven-embedder:3.6.3"
implementation "org.asciidoctor:asciidoctor-gradle-jvm:3.3.2"
implementation 'org.codehaus.groovy:groovy-all:2.5.17'
implementation 'org.jfrog.buildinfo:build-info-extractor-gradle:4.31.9'
implementation "org.gradle:test-retry-gradle-plugin:1.4.0"
implementation 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.7.1'
implementation(platform("org.springframework:spring-framework-bom:${springFrameworkVersion}"))
implementation 'org.springframework:spring-core'
implementation 'org.springframework:spring-web'
implementation buildLibs.asciidoc.gradle.jvm
implementation buildLibs.commons.codec
implementation buildLibs.groovy.all
implementation buildLibs.jfrog.build.extractor.gradle
implementation buildLibs.jackson.databind
implementation buildLibs.javaformat.gradle
implementation buildLibs.maven.embedder
implementation buildLibs.nexus.publish
implementation buildLibs.nohttp.gradle
implementation buildLibs.sonarqube.gradle
implementation(platform(buildLibs.spring.bom))
implementation buildLibs.spring.core
implementation buildLibs.spring.web
implementation buildLibs.test.retry.gradle
}

gradlePlugin {
Expand Down
8 changes: 8 additions & 0 deletions buildSrc/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@ pluginManagement {
gradlePluginPortal()
}
}

dependencyResolutionManagement {
versionCatalogs {
buildLibs {
from(files("../gradle/build-libs.versions.toml"))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.gradle.api.plugins.PluginManager;

import org.springframework.pulsar.gradle.optional.OptionalDependenciesPlugin;
import org.springframework.pulsar.gradle.publish.SpringMavenPlugin;

import io.spring.gradle.convention.RepositoryConventionPlugin;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private void configureSpringJavaFormat(Project project) {
project.getTasks().withType(Format.class, (Format) -> Format.setEncoding("UTF-8"));
project.getPlugins().apply(CheckstylePlugin.class);
CheckstyleExtension checkstyle = project.getExtensions().getByType(CheckstyleExtension.class);
checkstyle.setToolVersion("8.45.1");
checkstyle.setToolVersion("10.12.4");
checkstyle.getConfigDirectory().set(project.getRootProject().file("src/checkstyle"));
String version = SpringJavaFormatPlugin.class.getPackage().getImplementationVersion();
DependencySet checkstyleDependencies = project.getConfigurations().getByName("checkstyle").getDependencies();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.VersionCatalogsExtension;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.tasks.testing.Test;
import org.gradle.testing.jacoco.plugins.JacocoPlugin;
Expand All @@ -19,8 +20,10 @@ public class JacocoConventionsPlugin implements Plugin<Project> {
public void apply(final Project project) {
project.getPlugins().withType(JavaPlugin.class, (javaPlugin) -> {
project.getPluginManager().apply(JacocoPlugin.class);
VersionCatalogsExtension catalog = project.getExtensions().getByType(VersionCatalogsExtension.class);
String jacocoVeresion = catalog.named("libs").findVersion("jacoco").orElseThrow().getDisplayName();
project.getExtensions().configure(JacocoPluginExtension.class,
(jacocoExtension) -> jacocoExtension.setToolVersion("0.8.9"));
(jacocoExtension) -> jacocoExtension.setToolVersion(jacocoVeresion));
project.getTasks().withType(Test.class, (test) ->
project.getTasks().withType(JacocoReport.class, test::finalizedBy));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,57 @@
package org.springframework.pulsar.gradle.versions;

import java.io.File;
import java.util.Objects;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.artifacts.VersionCatalogsExtension;

import org.springframework.lang.Nullable;

public abstract class UpdateProjectVersionTask extends DefaultTask {

static final String VERSION_PROPERTY = "version";

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

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

protected void updateVersionInGradleProperties(String newVersion) {
this.updatePropertyInGradleProperties(VERSION_PROPERTY, (p) -> p.getVersion().toString(), newVersion);
this.updatePropertyInFile(Project.GRADLE_PROPERTIES, VERSION_PROPERTY,
(p) -> p.getVersion().toString(),
(__) -> newVersion,
(currentValue) -> "%s=%s".formatted(VERSION_PROPERTY, currentValue),
(__) -> "%s=%s".formatted(VERSION_PROPERTY, newVersion));
}

protected void updatePropertyInGradleProperties(String propertyName, String newPropertyValue) {
this.updatePropertyInGradleProperties(propertyName,
(p) -> Objects.toString(p.findProperty(propertyName), ""), newPropertyValue);
protected void updateVersionInTomlVersions(String versionPropertyName,
Function<String, String> newPropertyValueGivenCurrentValue) {
this.updatePropertyInFile("gradle/libs.versions.toml", versionPropertyName,
(p) -> currentVersionForProperty(p, versionPropertyName),
newPropertyValueGivenCurrentValue,
(currentValue) -> "%s = \"%s\"".formatted(versionPropertyName, currentValue),
(newValue) -> "%s = \"%s\"".formatted(versionPropertyName, newValue));
}

protected void updatePropertyInGradleProperties(
String propertyName,
Function<Project, String> currentPropertyValueFromProject,
String newPropertyValue) {
String currentPropertyValue = currentPropertyValueFromProject.apply(getProject());
File gradlePropertiesFile = getProject().getRootProject().file(Project.GRADLE_PROPERTIES);
if (!gradlePropertiesFile.exists()) {
throw new RuntimeException("No gradle.properties to update property in");
protected void updatePropertyInFile(String propertyFile, String propertyName,
Function<Project, String> currentPropertyValueGivenProject,
Function<String, String> newPropertyValueGivenCurrentValue,
Function<String, String> expectedCurrentPropertyEntryInFile,
Function<String, String> newPropertyEntryInFile) {
File file = getProject().getRootProject().file(propertyFile);
if (!file.exists()) {
throw new RuntimeException("File not found at " + propertyFile);
}
System.out.printf("Updating the %s property in %s from %s to %s%n",
propertyName, Project.GRADLE_PROPERTIES, currentPropertyValue, newPropertyValue);
FileUtils.replaceFileText(gradlePropertiesFile, (gradlePropertiesText) -> {
gradlePropertiesText = gradlePropertiesText.replace(
"%s=%s".formatted(propertyName, currentPropertyValue),
"%s=%s".formatted(propertyName, newPropertyValue));
return gradlePropertiesText;
});
String currentValue = currentPropertyValueGivenProject.apply(getProject());
String newValue = newPropertyValueGivenCurrentValue.apply(currentValue);
System.out.printf("Updating the %s property in %s from %s to %s%n", propertyName,
propertyFile, currentValue, newValue);
FileUtils.replaceFileText(file, (propertiesText) -> propertiesText.replace(
expectedCurrentPropertyEntryInFile.apply(currentValue),
newPropertyEntryInFile.apply(newValue)));
}

protected VersionInfo parseVersion(String version) {
Expand All @@ -78,6 +85,13 @@ protected VersionInfo parseVersion(String version) {
}
}

protected String currentVersionForProperty(Project project, String versionProperty) {
VersionCatalogsExtension catalog = project.getExtensions().getByType(VersionCatalogsExtension.class);
return catalog.named("libs").findVersion(versionProperty)
.orElseThrow(() -> new IllegalStateException("% property not found".formatted(versionProperty)))
.getDisplayName();
}

record VersionInfo(String major, String minor, String patch, @Nullable String modifier) {
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 the original author or authors.
* Copyright 2019-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,13 +16,9 @@

package org.springframework.pulsar.gradle.versions;

import java.util.Objects;

import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.TaskAction;

import org.springframework.util.Assert;

public abstract class UpdateToReleaseVersionTask extends UpdateProjectVersionTask {

@Input
Expand All @@ -31,7 +27,7 @@ public abstract class UpdateToReleaseVersionTask extends UpdateProjectVersionTas
@TaskAction
public void updateToReleaseVersion() {
updateVersionInGradleProperties(this.releaseVersion);
updatePropertyInGradleProperties(SPRING_BOOT_VERSION_PROPERTY, calculateReleaseBootVersion());
updateVersionInTomlVersions(SPRING_BOOT_VERSION_PROPERTY, this::calculateReleaseBootVersion);
}

public String getReleaseVersion() {
Expand All @@ -42,9 +38,7 @@ public void setReleaseVersion(String releaseVersion) {
this.releaseVersion = releaseVersion;
}

private String calculateReleaseBootVersion() {
String currentBootVersion = Objects.toString(getProject().findProperty(SPRING_BOOT_VERSION_PROPERTY), null);
Assert.notNull(currentBootVersion, () -> "% property not found".formatted(SPRING_BOOT_VERSION_PROPERTY));
private String calculateReleaseBootVersion(String currentBootVersion) {
VersionInfo bootVersionSegments = parseVersion(currentBootVersion);
String releaseBootVersion = "%s.%s.%s".formatted(bootVersionSegments.major(), bootVersionSegments.minor(), bootVersionSegments.patch());
String releaseVersionModifier = parseVersion(this.releaseVersion).modifier();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 the original author or authors.
* Copyright 2019-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,21 +16,15 @@

package org.springframework.pulsar.gradle.versions;

import java.util.Objects;

import org.gradle.api.tasks.TaskAction;

import org.springframework.util.Assert;

public abstract class UpdateToSnapshotVersionTask extends UpdateProjectVersionTask {

@TaskAction
public void updateToSnapshotVersion() {
String currentVersion = getProject().getVersion().toString();
updateVersionInGradleProperties(calculateNextSnapshotVersion(currentVersion));
String currentBootVersion = Objects.toString(getProject().findProperty(SPRING_BOOT_VERSION_PROPERTY), null);
Assert.notNull(currentBootVersion, () -> "% property not found".formatted(SPRING_BOOT_VERSION_PROPERTY));
updatePropertyInGradleProperties(SPRING_BOOT_VERSION_PROPERTY, calculateNextSnapshotVersion(currentBootVersion));
updateVersionInTomlVersions(SPRING_BOOT_VERSION_PROPERTY, this::calculateNextSnapshotVersion);
}

private String calculateNextSnapshotVersion(String version) {
Expand All @@ -45,12 +39,4 @@ private String calculateNextSnapshotVersion(String version) {
}
return "%s.%s.%s-SNAPSHOT".formatted(majorSegment, minorSegment, patchSegment);
}

private String calculateCurrentSnapshotVersion(String version) {
VersionInfo versionSegments = parseVersion(version);
String majorSegment = versionSegments.major();
String minorSegment = versionSegments.minor();
String patchSegment = versionSegments.patch();
return "%s.%s.%s-SNAPSHOT".formatted(majorSegment, minorSegment, patchSegment);
}
}
12 changes: 0 additions & 12 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,3 @@ version=1.0.2-SNAPSHOT
org.gradle.caching=true
org.gradle.parallel=true
org.gradle.jvmargs=-Xmx4g -XX:+HeapDumpOnOutOfMemoryError -XX:+UseParallelGC -Dfile.encoding=UTF-8

pulsarClientVersion=3.1.1
pulsarClientReactiveVersion=0.5.1
springFrameworkVersion=6.1.2

# only used by integration tests and samples (unpublished deps)
springBootVersion=3.2.2-SNAPSHOT

# only used for docs (links)
springBootVersionForDocs=3.2.2-SNAPSHOT

springCloudStreamVersion=4.1.0
2 changes: 1 addition & 1 deletion gradle/aggregate-jacoco-report.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apply plugin: 'jacoco'

jacoco {
toolVersion '0.8.9'
toolVersion libs.versions.jacoco.get()
}

tasks.withType(Test) {
Expand Down
21 changes: 14 additions & 7 deletions gradle/antora-docs.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//file:noinspection GroovyUnusedAssignment
node {
version = '16.16.0'
}
Expand Down Expand Up @@ -36,11 +37,17 @@ tasks.create('generateAntoraResources') {
}

def generateAttributes() {
return ['spring-boot-version': project.springBootVersionForDocs ?: 'current',
'spring-framework-version': project.springFrameworkVersion ?: 'current',
'spring-cloud-stream-version': project.springCloudStreamVersion ?: 'current',
'spring-pulsar-version': project.version,
'pulsar-client-version': project.pulsarClientVersion ?: 'current',
'pulsar-client-reactive-version': project.pulsarClientReactiveVersion ?: 'current',
'is-snapshot-version': project.version.endsWith("-SNAPSHOT")]
def versionCatalog = extensions.getByType(VersionCatalogsExtension).named("libs")
def springBootVersionForDocs = versionCatalog.findVersion("spring-boot-for-docs").orElseThrow().displayName
def springFrameworkVersion = versionCatalog.findVersion("spring").orElseThrow().displayName
def springCloudStreamVersion = versionCatalog.findVersion("spring-cloud-stream").orElseThrow().displayName
def pulsarClientVersion = versionCatalog.findVersion("pulsar").orElseThrow().displayName
def pulsarClientReactiveVersion = versionCatalog.findVersion("pulsar-reactive").orElseThrow().displayName
return ['is-snapshot-version': project.version.endsWith("-SNAPSHOT"),
'pulsar-client-version': pulsarClientVersion ?: 'current',
'pulsar-client-reactive-version': pulsarClientReactiveVersion ?: 'current',
'spring-boot-version': springBootVersionForDocs ?: 'current',
'spring-cloud-stream-version': springCloudStreamVersion ?: 'current',
'spring-framework-version': springFrameworkVersion ?: 'current',
'spring-pulsar-version': project.version]
}
Loading

0 comments on commit 653196f

Please sign in to comment.