Use source sets to determine configurations deprecated for resolution

Fixes gh-22200
pull/22996/head
Andy Wilkinson 4 years ago
parent f6b3666b16
commit 79770b9615

@ -27,10 +27,12 @@ import org.gradle.api.file.FileCollection;
import org.gradle.api.file.FileCopyDetails; import org.gradle.api.file.FileCopyDetails;
import org.gradle.api.file.FileTreeElement; import org.gradle.api.file.FileTreeElement;
import org.gradle.api.internal.file.copy.CopyAction; import org.gradle.api.internal.file.copy.CopyAction;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.specs.Spec; import org.gradle.api.specs.Spec;
import org.gradle.api.tasks.Internal; import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Nested; import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.Optional; import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.bundling.Jar; import org.gradle.api.tasks.bundling.Jar;
/** /**
@ -103,7 +105,12 @@ public class BootJar extends Jar implements BootArchive {
@Override @Override
protected CopyAction createCopyAction() { protected CopyAction createCopyAction() {
if (this.layered != null) { if (this.layered != null) {
LayerResolver layerResolver = new LayerResolver(getConfigurations(), this.layered, this::isLibrary); JavaPluginConvention javaPluginConvention = getProject().getConvention()
.findPlugin(JavaPluginConvention.class);
Iterable<SourceSet> sourceSets = (javaPluginConvention != null) ? javaPluginConvention.getSourceSets()
: Collections.emptySet();
LayerResolver layerResolver = new LayerResolver(sourceSets, getConfigurations(), this.layered,
this::isLibrary);
String layerToolsLocation = this.layered.isIncludeLayerTools() ? LIB_DIRECTORY : null; String layerToolsLocation = this.layered.isIncludeLayerTools() ? LIB_DIRECTORY : null;
return this.support.createCopyAction(this, layerResolver, layerToolsLocation); return this.support.createCopyAction(this, layerResolver, layerToolsLocation);
} }

@ -17,8 +17,6 @@
package org.springframework.boot.gradle.tasks.bundling; package org.springframework.boot.gradle.tasks.bundling;
import java.io.File; import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
@ -30,6 +28,7 @@ import org.gradle.api.artifacts.ResolvedArtifact;
import org.gradle.api.artifacts.ResolvedConfiguration; import org.gradle.api.artifacts.ResolvedConfiguration;
import org.gradle.api.file.FileCopyDetails; import org.gradle.api.file.FileCopyDetails;
import org.gradle.api.specs.Spec; import org.gradle.api.specs.Spec;
import org.gradle.api.tasks.SourceSet;
import org.springframework.boot.loader.tools.Layer; import org.springframework.boot.loader.tools.Layer;
import org.springframework.boot.loader.tools.Library; import org.springframework.boot.loader.tools.Library;
@ -53,9 +52,9 @@ class LayerResolver {
private final Spec<FileCopyDetails> librarySpec; private final Spec<FileCopyDetails> librarySpec;
LayerResolver(Iterable<Configuration> configurations, LayeredSpec layeredConfiguration, LayerResolver(Iterable<SourceSet> sourceSets, Iterable<Configuration> configurations,
Spec<FileCopyDetails> librarySpec) { LayeredSpec layeredConfiguration, Spec<FileCopyDetails> librarySpec) {
this.resolvedDependencies = new ResolvedDependencies(configurations); this.resolvedDependencies = new ResolvedDependencies(sourceSets, configurations);
this.layeredConfiguration = layeredConfiguration; this.layeredConfiguration = layeredConfiguration;
this.librarySpec = librarySpec; this.librarySpec = librarySpec;
} }
@ -96,19 +95,41 @@ class LayerResolver {
*/ */
private static class ResolvedDependencies { private static class ResolvedDependencies {
private static final Set<String> DEPRECATED_FOR_RESOLUTION_CONFIGURATIONS = Collections private final Set<String> deprecatedForResolutionConfigurationNames;
.unmodifiableSet(new HashSet<>(Arrays.asList("archives", "compile", "compileOnly", "default", "runtime",
"testCompile", "testCompileOnly", "testRuntime")));
private final Map<Configuration, ResolvedConfigurationDependencies> configurationDependencies = new LinkedHashMap<>(); private final Map<Configuration, ResolvedConfigurationDependencies> configurationDependencies = new LinkedHashMap<>();
ResolvedDependencies(Iterable<Configuration> configurations) { ResolvedDependencies(Iterable<SourceSet> sourceSets, Iterable<Configuration> configurations) {
this.deprecatedForResolutionConfigurationNames = deprecatedForResolutionConfigurationNames(sourceSets);
configurations.forEach(this::processConfiguration); configurations.forEach(this::processConfiguration);
} }
@SuppressWarnings("deprecation")
private Set<String> deprecatedForResolutionConfigurationNames(Iterable<SourceSet> sourceSets) {
Set<String> configurationNames = new HashSet<>();
configurationNames.add("archives");
configurationNames.add("default");
for (SourceSet sourceSet : sourceSets) {
try {
configurationNames.add(sourceSet.getCompileConfigurationName());
}
catch (NoSuchMethodError ex) {
// Continue
}
configurationNames.add(sourceSet.getCompileOnlyConfigurationName());
try {
configurationNames.add(sourceSet.getRuntimeConfigurationName());
}
catch (NoSuchMethodError ex) {
// Continue
}
}
return configurationNames;
}
private void processConfiguration(Configuration configuration) { private void processConfiguration(Configuration configuration) {
if (configuration.isCanBeResolved() if (configuration.isCanBeResolved()
&& !DEPRECATED_FOR_RESOLUTION_CONFIGURATIONS.contains(configuration.getName())) { && !this.deprecatedForResolutionConfigurationNames.contains(configuration.getName())) {
this.configurationDependencies.put(configuration, this.configurationDependencies.put(configuration,
new ResolvedConfigurationDependencies(configuration.getResolvedConfiguration())); new ResolvedConfigurationDependencies(configuration.getResolvedConfiguration()));
} }

@ -86,6 +86,11 @@ class BootJarIntegrationTests extends AbstractBootArchiveIntegrationTests {
.getOutcome()).isEqualTo(TaskOutcome.SUCCESS); .getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
} }
@TestTemplate
void layersWithCustomSourceSet() throws IOException {
assertThat(this.gradleBuild.build("bootJar").task(":bootJar").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
}
@TestTemplate @TestTemplate
void implicitLayers() throws IOException { void implicitLayers() throws IOException {
writeMainClass(); writeMainClass();

@ -0,0 +1,36 @@
plugins {
id 'java'
id 'org.springframework.boot' version '{version}'
}
sourceSets {
custom
}
bootJar {
mainClassName = 'com.example.Application'
layered()
}
repositories {
mavenCentral()
maven { url "file:repository" }
}
dependencies {
implementation("com.example:library:1.0-SNAPSHOT")
implementation("org.apache.commons:commons-lang3:3.9")
implementation("org.springframework:spring-core:5.2.5.RELEASE")
}
task listLayers(type: JavaExec) {
classpath = bootJar.outputs.files
systemProperties = [ "jarmode": "layertools" ]
args "list"
}
task extractLayers(type: JavaExec) {
classpath = bootJar.outputs.files
systemProperties = [ "jarmode": "layertools" ]
args "extract"
}
Loading…
Cancel
Save