Skip to content

Commit

Permalink
Use source sets to determine configurations deprecated for resolution
Browse files Browse the repository at this point in the history
Fixes gh-22200
  • Loading branch information
wilkinsona committed Jul 2, 2020
1 parent f6b3666 commit 79770b9
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 11 deletions.
Expand Up @@ -27,10 +27,12 @@
import org.gradle.api.file.FileCopyDetails;
import org.gradle.api.file.FileTreeElement;
import org.gradle.api.internal.file.copy.CopyAction;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.specs.Spec;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.bundling.Jar;

/**
Expand Down Expand Up @@ -103,7 +105,12 @@ public void copy() {
@Override
protected CopyAction createCopyAction() {
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;
return this.support.createCopyAction(this, layerResolver, layerToolsLocation);
}
Expand Down
Expand Up @@ -17,8 +17,6 @@
package org.springframework.boot.gradle.tasks.bundling;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
Expand All @@ -30,6 +28,7 @@
import org.gradle.api.artifacts.ResolvedConfiguration;
import org.gradle.api.file.FileCopyDetails;
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.Library;
Expand All @@ -53,9 +52,9 @@ class LayerResolver {

private final Spec<FileCopyDetails> librarySpec;

LayerResolver(Iterable<Configuration> configurations, LayeredSpec layeredConfiguration,
Spec<FileCopyDetails> librarySpec) {
this.resolvedDependencies = new ResolvedDependencies(configurations);
LayerResolver(Iterable<SourceSet> sourceSets, Iterable<Configuration> configurations,
LayeredSpec layeredConfiguration, Spec<FileCopyDetails> librarySpec) {
this.resolvedDependencies = new ResolvedDependencies(sourceSets, configurations);
this.layeredConfiguration = layeredConfiguration;
this.librarySpec = librarySpec;
}
Expand Down Expand Up @@ -96,19 +95,41 @@ private Library asLibrary(FileCopyDetails details) {
*/
private static class ResolvedDependencies {

private static final Set<String> DEPRECATED_FOR_RESOLUTION_CONFIGURATIONS = Collections
.unmodifiableSet(new HashSet<>(Arrays.asList("archives", "compile", "compileOnly", "default", "runtime",
"testCompile", "testCompileOnly", "testRuntime")));
private final Set<String> deprecatedForResolutionConfigurationNames;

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);
}

@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) {
if (configuration.isCanBeResolved()
&& !DEPRECATED_FOR_RESOLUTION_CONFIGURATIONS.contains(configuration.getName())) {
&& !this.deprecatedForResolutionConfigurationNames.contains(configuration.getName())) {
this.configurationDependencies.put(configuration,
new ResolvedConfigurationDependencies(configuration.getResolvedConfiguration()));
}
Expand Down
Expand Up @@ -86,6 +86,11 @@ void notUpToDateWhenBuiltWithLayersAndToolsAndThenWithLayersAndWithoutTools()
.getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
}

@TestTemplate
void layersWithCustomSourceSet() throws IOException {
assertThat(this.gradleBuild.build("bootJar").task(":bootJar").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
}

@TestTemplate
void implicitLayers() throws IOException {
writeMainClass();
Expand Down
@@ -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"
}

0 comments on commit 79770b9

Please sign in to comment.