Skip to content

Commit 0757857

Browse files
committed
Stop calling getProject() when ExtractVersionConstraints is executing
Closes gh-40550
1 parent c139bdf commit 0757857

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

buildSrc/src/main/java/org/springframework/boot/build/constraints/ExtractVersionConstraints.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,17 +26,20 @@
2626
import java.util.TreeSet;
2727

2828
import org.gradle.api.DefaultTask;
29+
import org.gradle.api.Project;
2930
import org.gradle.api.Task;
3031
import org.gradle.api.artifacts.ComponentMetadataDetails;
3132
import org.gradle.api.artifacts.Configuration;
3233
import org.gradle.api.artifacts.DependencyConstraint;
3334
import org.gradle.api.artifacts.DependencyConstraintMetadata;
35+
import org.gradle.api.artifacts.DependencyConstraintSet;
3436
import org.gradle.api.artifacts.dsl.DependencyHandler;
3537
import org.gradle.api.tasks.Internal;
3638
import org.gradle.api.tasks.TaskAction;
3739
import org.gradle.platform.base.Platform;
3840

3941
import org.springframework.boot.build.bom.BomExtension;
42+
import org.springframework.boot.build.bom.BomPlugin;
4043
import org.springframework.boot.build.bom.Library;
4144

4245
/**
@@ -55,7 +58,9 @@ public class ExtractVersionConstraints extends DefaultTask {
5558

5659
private final Set<VersionProperty> versionProperties = new TreeSet<>();
5760

58-
private final List<String> projectPaths = new ArrayList<>();
61+
private final List<DependencyConstraintSet> dependencyConstraintSets = new ArrayList<>();
62+
63+
private final List<BomExtension> boms = new ArrayList<>();
5964

6065
public ExtractVersionConstraints() {
6166
DependencyHandler dependencies = getProject().getDependencies();
@@ -68,7 +73,12 @@ public void enforcedPlatform(String projectPath) {
6873
.add(getProject().getDependencies()
6974
.enforcedPlatform(
7075
getProject().getDependencies().project(Collections.singletonMap("path", projectPath))));
71-
this.projectPaths.add(projectPath);
76+
Project project = getProject().project(projectPath);
77+
project.getPlugins().withType(BomPlugin.class).all((plugin) -> {
78+
this.boms.add(project.getExtensions().getByType(BomExtension.class));
79+
this.dependencyConstraintSets
80+
.add(project.getConfigurations().getByName("apiElements").getAllDependencyConstraints());
81+
});
7282
}
7383

7484
@Internal
@@ -89,12 +99,9 @@ public Set<VersionProperty> getVersionProperties() {
8999
@TaskAction
90100
void extractVersionConstraints() {
91101
this.configuration.resolve();
92-
for (String projectPath : this.projectPaths) {
93-
extractVersionProperties(projectPath);
94-
for (DependencyConstraint constraint : getProject().project(projectPath)
95-
.getConfigurations()
96-
.getByName("apiElements")
97-
.getAllDependencyConstraints()) {
102+
this.boms.forEach(this::extractVersionProperties);
103+
for (DependencyConstraintSet constraints : this.dependencyConstraintSets) {
104+
for (DependencyConstraint constraint : constraints) {
98105
this.versionConstraints.put(constraint.getGroup() + ":" + constraint.getName(),
99106
constraint.getVersionConstraint().toString());
100107
this.constrainedVersions.add(new ConstrainedVersion(constraint.getGroup(), constraint.getName(),
@@ -103,9 +110,7 @@ void extractVersionConstraints() {
103110
}
104111
}
105112

106-
private void extractVersionProperties(String projectPath) {
107-
Object bom = getProject().project(projectPath).getExtensions().getByName("bom");
108-
BomExtension bomExtension = (BomExtension) bom;
113+
private void extractVersionProperties(BomExtension bomExtension) {
109114
for (Library lib : bomExtension.getLibraries()) {
110115
String versionProperty = lib.getVersionProperty();
111116
if (versionProperty != null) {

0 commit comments

Comments
 (0)