Skip to content

Commit

Permalink
Publish a runtime variant that supports Java 22
Browse files Browse the repository at this point in the history
In order to support Java 22, we must use spring-core 6.1.x.
spring-core 6.1.x is a multi-release jar so, in order to support Java
22, a version of Gradle that supports multi-release jars must be
used.

This commit adds a new variant to spring-boot-gradle-plugin for
modern versions of Gradle. When Gradle's plugin API version is 8.7 or
later, we use spring-core 6.1.x. spring-core 6.0.x is used at all
other times.

Closes spring-projectsgh-40074
  • Loading branch information
wilkinsona committed May 2, 2024
1 parent 6fd3ebb commit c7ec608
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ private void customizeMavenPublication(MavenPublication publication, Project pro
project.getPlugins()
.withType(JavaPlugin.class)
.all((javaPlugin) -> customizeJavaMavenPublication(publication, project));
suppressMavenOptionalFeatureWarnings(publication);
}

private void customizePom(MavenPom pom, Project project) {
Expand All @@ -102,7 +101,10 @@ private void customizePom(MavenPom pom, Project project) {
}

private void customizeJavaMavenPublication(MavenPublication publication, Project project) {
addMavenOptionalFeature(project);
addMavenOptionalFeature(publication, project);
if (publication.getName().equals("pluginMaven")) {
return;
}
publication.versionMapping((strategy) -> strategy.usage(Usage.JAVA_API, (mappingStrategy) -> mappingStrategy
.fromResolutionOf(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME)));
publication.versionMapping(
Expand All @@ -112,9 +114,10 @@ private void customizeJavaMavenPublication(MavenPublication publication, Project
/**
* Add a feature that allows maven plugins to declare optional dependencies that
* appear in the POM. This is required to make m2e in Eclipse happy.
* @param publication the project's Maven publication
* @param project the project to add the feature to
*/
private void addMavenOptionalFeature(Project project) {
private void addMavenOptionalFeature(MavenPublication publication, Project project) {
JavaPluginExtension extension = project.getExtensions().getByType(JavaPluginExtension.class);
extension.registerFeature("mavenOptional",
(feature) -> feature.usingSourceSet(extension.getSourceSets().getByName("main")));
Expand All @@ -123,6 +126,7 @@ private void addMavenOptionalFeature(Project project) {
javaComponent.addVariantsFromConfiguration(
project.getConfigurations().findByName("mavenOptionalRuntimeElements"),
ConfigurationVariantDetails::mapToOptional);
suppressMavenOptionalFeatureWarnings(publication);
}

private void suppressMavenOptionalFeatureWarnings(MavenPublication publication) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,34 @@ configurations {
// Downgrade SLF4J is required for tests to run in Eclipse
resolutionStrategy.force("org.slf4j:slf4j-api:1.7.36")
}
all {
modernGradleRuntimeClasspath {
extendsFrom runtimeClasspath
canBeConsumed = false
canBeResolved = true
}
modernGradleRuntimeElements {
extendsFrom configurations.implementation, configurations.runtimeOnly
canBeConsumed = true
canBeResolved = false
attributes {
attribute(Category.CATEGORY_ATTRIBUTE, project.objects.named(Category, Category.LIBRARY))
attribute(Bundling.BUNDLING_ATTRIBUTE, project.objects.named(Bundling, Bundling.EXTERNAL))
attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, 17)
attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.objects.named(LibraryElements, LibraryElements.JAR))
attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage, Usage.JAVA_RUNTIME))
attribute(GradlePluginApiVersion.GRADLE_PLUGIN_API_VERSION_ATTRIBUTE, project.objects.named(GradlePluginApiVersion, "8.7"))
}
outgoing.artifacts.addAll(configurations.runtimeElements.outgoing.artifacts)
}
runtimeElements {
attributes {
attribute(GradlePluginApiVersion.GRADLE_PLUGIN_API_VERSION_ATTRIBUTE, project.objects.named(GradlePluginApiVersion, "7.5"))
}
}
all { configuration ->
if (configuration.name == 'modernGradleRuntimeClasspath') {
return
}
resolutionStrategy {
eachDependency { dependency ->
// Downgrade Jackson as Gradle cannot cope with 2.15.0's multi-version
Expand Down Expand Up @@ -44,6 +71,10 @@ configurations {
}
}

components.java.addVariantsFromConfiguration(configurations.modernGradleRuntimeElements) {
mapToMavenScope("runtime")
}

dependencies {
asciidoctorExtensions("io.spring.asciidoctor:spring-asciidoctor-extensions-section-ids")

Expand Down Expand Up @@ -166,3 +197,18 @@ artifacts {
toolchain {
maximumCompatibleJavaVersion = JavaLanguageVersion.of(20)
}

publishing {
publications.matching { it.name == 'pluginMaven' }.configureEach {
versionMapping {
allVariants {
fromResolutionOf(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME)
}
}
versionMapping {
variant(GradlePluginApiVersion.GRADLE_PLUGIN_API_VERSION_ATTRIBUTE, project.objects.named(GradlePluginApiVersion, "8.7")) {
fromResolutionOf("modernGradleRuntimeClasspath")
}
}
}
}

0 comments on commit c7ec608

Please sign in to comment.