Skip to content

Commit

Permalink
Merge branch '3.1.x'
Browse files Browse the repository at this point in the history
Closes gh-38233
  • Loading branch information
wilkinsona committed Nov 6, 2023
2 parents dbe882a + b01235e commit 08a4a83
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,17 @@
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.attributes.Attribute;
import org.gradle.api.attributes.AttributeContainer;
import org.gradle.api.attributes.Bundling;
import org.gradle.api.attributes.LibraryElements;
import org.gradle.api.attributes.Usage;
import org.gradle.api.file.FileCollection;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.plugins.ApplicationPlugin;
import org.gradle.api.plugins.BasePlugin;
import org.gradle.api.plugins.ExtensionContainer;
import org.gradle.api.plugins.JavaApplication;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.provider.Provider;
import org.gradle.api.provider.ProviderFactory;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.TaskProvider;
Expand Down Expand Up @@ -275,18 +273,21 @@ private void configureAdditionalMetadataLocations(JavaCompile compile) {
.ifPresent((locations) -> compile.doFirst(new AdditionalMetadataLocationsConfigurer(locations)));
}

@SuppressWarnings({ "rawtypes", "unchecked" })
private void configureProductionRuntimeClasspathConfiguration(Project project) {
Configuration productionRuntimeClasspath = project.getConfigurations()
.create(SpringBootPlugin.PRODUCTION_RUNTIME_CLASSPATH_CONFIGURATION_NAME);
AttributeContainer attributes = productionRuntimeClasspath.getAttributes();
ObjectFactory objectFactory = project.getObjects();
attributes.attribute(Usage.USAGE_ATTRIBUTE, objectFactory.named(Usage.class, Usage.JAVA_RUNTIME));
attributes.attribute(Bundling.BUNDLING_ATTRIBUTE, objectFactory.named(Bundling.class, Bundling.EXTERNAL));
attributes.attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE,
objectFactory.named(LibraryElements.class, LibraryElements.JAR));
productionRuntimeClasspath.setVisible(false);
Configuration runtimeClasspath = project.getConfigurations()
.getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME);
productionRuntimeClasspath.attributes((attributes) -> {
ProviderFactory providers = project.getProviders();
AttributeContainer sourceAttributes = runtimeClasspath.getAttributes();
for (Attribute attribute : sourceAttributes.keySet()) {
attributes.attributeProvider(attribute,
providers.provider(() -> sourceAttributes.getAttribute(attribute)));
}
});
productionRuntimeClasspath.setExtendsFrom(runtimeClasspath.getExtendsFrom());
productionRuntimeClasspath.setCanBeResolved(runtimeClasspath.isCanBeResolved());
productionRuntimeClasspath.setCanBeConsumed(runtimeClasspath.isCanBeConsumed());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarOutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.TaskOutcome;
Expand Down Expand Up @@ -191,11 +193,12 @@ void runtimeClasspathIncludesDevelopmentOnlyDependencies() {
}

@TestTemplate
void productionRuntimeClasspathIsConfiguredWithAttributes() {
assertThat(this.gradleBuild.build("build").getOutput()).contains("3 productionRuntimeClasspath attributes:")
.contains("org.gradle.usage: java-runtime")
.contains("org.gradle.libraryelements: jar")
.contains("org.gradle.dependency.bundling: external");
void productionRuntimeClasspathIsConfiguredWithAttributesThatMatchRuntimeClasspath() {
String output = this.gradleBuild.build("build").getOutput();
Matcher matcher = Pattern.compile("runtimeClasspath: (\\[.*\\])").matcher(output);
assertThat(matcher.find()).as("%s found in %s", matcher, output).isTrue();
String attributes = matcher.group(1);
assertThat(output).contains("productionRuntimeClasspath: " + attributes);
}

@TestTemplate
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def collectAttributes(String configurationName) {
def attributes = configurations.findByName(configurationName).attributes
def keys = new TreeSet<>((a1, a2) -> a1.name.compareTo(a2.name))
keys.addAll(attributes.keySet())
keys.collect { key -> "${key}: ${attributes.getAttribute(key)}" }
}

plugins {
id 'org.springframework.boot' version '{version}'
id 'java'
}

springBoot {
mainClass = "com.example.Main"
}

gradle.taskGraph.whenReady {
def runtimeClasspathAttributes = collectAttributes("runtimeClasspath")
def productionRuntimeClasspathAttributes = collectAttributes("productionRuntimeClasspath")
println("runtimeClasspath: ${runtimeClasspathAttributes}")
println("productionRuntimeClasspath: ${productionRuntimeClasspathAttributes}")
}

0 comments on commit 08a4a83

Please sign in to comment.