Skip to content

Commit

Permalink
Merge branch '3.0.x' into 3.1.x
Browse files Browse the repository at this point in the history
Closes gh-35925
  • Loading branch information
wilkinsona committed Jun 16, 2023
2 parents c192fb8 + 845c97f commit 0cfc14e
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.build.classpath;

import java.util.Set;
import java.util.stream.Collectors;

import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.component.ModuleComponentSelector;
import org.gradle.api.artifacts.result.DependencyResult;
import org.gradle.api.artifacts.result.ResolutionResult;
import org.gradle.api.file.FileCollection;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.TaskAction;

/**
* Tasks to check that none of classpath's direct dependencies are unconstrained.
*
* @author Andy Wilkinson
*/
public class CheckClasspathForUnconstrainedDirectDependencies extends DefaultTask {

private Configuration classpath;

public CheckClasspathForUnconstrainedDirectDependencies() {
getOutputs().upToDateWhen((task) -> true);
}

@Classpath
public FileCollection getClasspath() {
return this.classpath;
}

public void setClasspath(Configuration classpath) {
this.classpath = classpath;
}

@TaskAction
void checkForUnconstrainedDirectDependencies() {
ResolutionResult resolutionResult = this.classpath.getIncoming().getResolutionResult();
Set<? extends DependencyResult> dependencies = resolutionResult.getRoot().getDependencies();
Set<String> unconstrainedDependencies = dependencies.stream()
.map(DependencyResult::getRequested)
.filter(ModuleComponentSelector.class::isInstance)
.map(ModuleComponentSelector.class::cast)
.map((selector) -> selector.getGroup() + ":" + selector.getModule())
.collect(Collectors.toSet());
Set<String> constraints = resolutionResult.getAllDependencies()
.stream()
.filter(DependencyResult::isConstraint)
.map(DependencyResult::getRequested)
.filter(ModuleComponentSelector.class::isInstance)
.map(ModuleComponentSelector.class::cast)
.map((selector) -> selector.getGroup() + ":" + selector.getModule())
.collect(Collectors.toSet());
unconstrainedDependencies.removeAll(constraints);
if (!unconstrainedDependencies.isEmpty()) {
throw new GradleException("Found unconstrained direct dependencies: " + unconstrainedDependencies);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.springframework.boot.build.ConventionsPlugin;
import org.springframework.boot.build.DeployedPlugin;
import org.springframework.boot.build.classpath.CheckClasspathForConflicts;
import org.springframework.boot.build.classpath.CheckClasspathForUnconstrainedDirectDependencies;
import org.springframework.boot.build.classpath.CheckClasspathForUnnecessaryExclusions;
import org.springframework.util.StringUtils;

Expand Down Expand Up @@ -63,6 +64,7 @@ public void apply(Project project) {
(artifact) -> artifact.builtBy(starterMetadata));
createClasspathConflictsCheck(runtimeClasspath, project);
createUnnecessaryExclusionsCheck(runtimeClasspath, project);
createUnconstrainedDirectDependenciesCheck(runtimeClasspath, project);
configureJarManifest(project);
}

Expand All @@ -82,6 +84,17 @@ private void createUnnecessaryExclusionsCheck(Configuration classpath, Project p
project.getTasks().getByName(JavaBasePlugin.CHECK_TASK_NAME).dependsOn(checkClasspathForUnnecessaryExclusions);
}

private void createUnconstrainedDirectDependenciesCheck(Configuration classpath, Project project) {
CheckClasspathForUnconstrainedDirectDependencies checkClasspathForUnconstrainedDirectDependencies = project
.getTasks()
.create("check" + StringUtils.capitalize(classpath.getName() + "ForUnconstrainedDirectDependencies"),
CheckClasspathForUnconstrainedDirectDependencies.class);
checkClasspathForUnconstrainedDirectDependencies.setClasspath(classpath);
project.getTasks()
.getByName(JavaBasePlugin.CHECK_TASK_NAME)
.dependsOn(checkClasspathForUnconstrainedDirectDependencies);
}

private void configureJarManifest(Project project) {
project.getTasks().withType(Jar.class, (jar) -> project.afterEvaluate((evaluated) -> {
jar.manifest((manifest) -> {
Expand Down

0 comments on commit 0cfc14e

Please sign in to comment.