Skip to content

Commit

Permalink
Do not apply PomDependencyManagementConfigurer when POM customization…
Browse files Browse the repository at this point in the history
… is disabled

Currently, PomDependencyManagementConfigurer is always applied even if it is manually disabled, which breaks the configuration cache
  • Loading branch information
artemy-osipov committed Aug 13, 2024
1 parent 416c09e commit 53cf82e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,20 @@ public void apply(Project project) {
internalComponents.createDependencyManagementReportTask("dependencyManagement");
project.getConfigurations().all(internalComponents.getImplicitDependencyManagementCollector());
project.getConfigurations().all(internalComponents.getDependencyManagementApplier());
configurePomCustomization(project, dependencyManagementExtension);
configurePomCustomization(internalComponents, project, dependencyManagementExtension);
}

private void configurePomCustomization(Project project,
private void configurePomCustomization(InternalComponents internalComponents, Project project,
DependencyManagementExtension dependencyManagementExtension) {
PomDependencyManagementConfigurer pomConfigurer = dependencyManagementExtension.getPomConfigurer();
project.getPlugins()
.withType(MavenPublishPlugin.class,
(mavenPublishPlugin) -> configurePublishingExtension(project, pomConfigurer));
project.afterEvaluate((evaluatedProject) -> {
if (!internalComponents.getDependencyManagementSettings().getPomCustomizationSettings().isEnabled()) {
return;
}
PomDependencyManagementConfigurer pomConfigurer = dependencyManagementExtension.getPomConfigurer();
evaluatedProject.getPlugins()
.withType(MavenPublishPlugin.class,
(mavenPublishPlugin) -> configurePublishingExtension(evaluatedProject, pomConfigurer));
});
}

private void configurePublishingExtension(Project project, PomDependencyManagementConfigurer extension) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import groovy.namespace.QName;
import groovy.util.Node;
import io.spring.gradle.dependencymanagement.internal.DependencyManagementSettings.PomCustomizationSettings;
import io.spring.gradle.dependencymanagement.internal.pom.Coordinates;
import io.spring.gradle.dependencymanagement.internal.pom.Dependency;
import io.spring.gradle.dependencymanagement.internal.pom.Pom;
Expand Down Expand Up @@ -70,8 +69,6 @@ public class StandardPomDependencyManagementConfigurer implements PomDependencyM

private final DependencyManagement dependencyManagement;

private final PomCustomizationSettings settings;

private final PomResolver pomResolver;

private final Project project;
Expand All @@ -82,15 +79,13 @@ public class StandardPomDependencyManagementConfigurer implements PomDependencyM
* The given {@code settings} will control how the dependency management is applied to
* the pom.
* @param dependencyManagement the dependency management
* @param settings the customization settings
* @param pomResolver resolves imported boms during dependency management
* configuration
* @param project owner of the pom that is being configured
*/
public StandardPomDependencyManagementConfigurer(DependencyManagement dependencyManagement,
PomCustomizationSettings settings, PomResolver pomResolver, Project project) {
public StandardPomDependencyManagementConfigurer(DependencyManagement dependencyManagement, PomResolver pomResolver,
Project project) {
this.dependencyManagement = dependencyManagement;
this.settings = settings;
this.pomResolver = pomResolver;
this.project = project;
}
Expand All @@ -102,12 +97,6 @@ public void execute(XmlProvider xmlProvider) {

@Override
public void configurePom(Node pom) {
if (this.settings.isEnabled()) {
doConfigurePom(pom);
}
}

private void doConfigurePom(Node pom) {
Node dependencyManagementNode = findChild(pom, NODE_NAME_DEPENDENCY_MANAGEMENT);
if (dependencyManagementNode == null) {
dependencyManagementNode = pom.appendNode(NODE_NAME_DEPENDENCY_MANAGEMENT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class InternalComponents {

private final DependencyManagementExtension dependencyManagementExtension;

private final DependencyManagementSettings dependencyManagementSettings;

private final Action<Configuration> implicitDependencyManagementCollector;

private final Action<Configuration> dependencyManagementApplier;
Expand All @@ -58,13 +60,13 @@ public InternalComponents(Project project) {
project);
MavenPomResolver pomResolver = new MavenPomResolver(project, configurationContainer);
this.dependencyManagementContainer = new DependencyManagementContainer(project, pomResolver);
DependencyManagementSettings dependencyManagementSettings = new DependencyManagementSettings();
this.dependencyManagementSettings = new DependencyManagementSettings();
this.dependencyManagementExtension = new StandardDependencyManagementExtension(
this.dependencyManagementContainer, configurationContainer, project, dependencyManagementSettings);
this.dependencyManagementContainer, configurationContainer, project, this.dependencyManagementSettings);
this.implicitDependencyManagementCollector = new ImplicitDependencyManagementCollector(
this.dependencyManagementContainer, dependencyManagementSettings);
this.dependencyManagementContainer, this.dependencyManagementSettings);
this.dependencyManagementApplier = new DependencyManagementApplier(project, this.dependencyManagementContainer,
configurationContainer, dependencyManagementSettings, pomResolver);
configurationContainer, this.dependencyManagementSettings, pomResolver);
}

/**
Expand All @@ -75,6 +77,14 @@ public DependencyManagementExtension getDependencyManagementExtension() {
return this.dependencyManagementExtension;
}

/**
* Returns the {@link DependencyManagementSettings}.
* @return the settings
*/
public DependencyManagementSettings getDependencyManagementSettings() {
return this.dependencyManagementSettings;
}

/**
* Returns the {@link Action} that can be applied to a {@link Configuration} to
* collect implicit dependency management from its dependencies.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ public void generatedPomCustomization(Action<GeneratedPomCustomizationHandler> a
public StandardPomDependencyManagementConfigurer getPomConfigurer() {
return new StandardPomDependencyManagementConfigurer(
this.dependencyManagementContainer.getGlobalDependencyManagement(),
this.dependencyManagementSettings.getPomCustomizationSettings(),
new MavenPomResolver(this.project, this.configurationContainer), this.project);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import groovy.xml.XmlParser;
import groovy.xml.XmlUtil;
import io.spring.gradle.dependencymanagement.NodeAssert;
import io.spring.gradle.dependencymanagement.internal.DependencyManagementSettings.PomCustomizationSettings;
import io.spring.gradle.dependencymanagement.internal.maven.MavenPomResolver;
import io.spring.gradle.dependencymanagement.internal.pom.Coordinates;
import io.spring.gradle.dependencymanagement.internal.pom.PomResolver;
Expand Down Expand Up @@ -105,17 +104,6 @@ void multipleImportsAreImportedInTheOppositeOrderToWhichTheyWereImported() throw
assertThat(pom).textAtPath("//project/dependencyManagement/dependencies/dependency[2]/type").isEqualTo("pom");
}

@Test
void customizationOfPublishedPomsCanBeDisabled() throws Exception {
this.dependencyManagement.importBom(null,
new Coordinates("io.spring.platform", "platform-bom", "1.0.3.RELEASE"),
new MapPropertySource(Collections.emptyMap()));
PomCustomizationSettings settings = new PomCustomizationSettings();
settings.setEnabled(false);
NodeAssert pom = configuredPom(settings);
assertThat(pom).nodesAtPath("//project/dependencyManagement/dependencies/dependency").isEmpty();
}

@Test
void individualDependencyManagementIsAddedToThePom() throws Exception {
this.dependencyManagement.addManagedVersion(null, "org.springframework", "spring-core", "4.1.3.RELEASE",
Expand Down Expand Up @@ -263,21 +251,13 @@ void dependencyManagementIsExpandedToCoverDependenciesWithAClassifier() throws E
}

private NodeAssert configuredPom() throws Exception {
return configuredPom(new PomCustomizationSettings());
return configuredPom(PROJECT_TAG + "</project>");
}

private NodeAssert configuredPom(String existingPom) throws Exception {
return configuredPom(existingPom, new PomCustomizationSettings());
}

private NodeAssert configuredPom(PomCustomizationSettings settings) throws Exception {
return configuredPom(PROJECT_TAG + "</project>", settings);
}

private NodeAssert configuredPom(String existingPom, PomCustomizationSettings settings) throws Exception {
Node pom = new XmlParser().parseText(existingPom);
new StandardPomDependencyManagementConfigurer(this.dependencyManagement.getGlobalDependencyManagement(),
settings, this.pomResolver, this.project)
this.pomResolver, this.project)
.configurePom(pom);
return new NodeAssert(XmlUtil.serialize(pom));
}
Expand Down

0 comments on commit 53cf82e

Please sign in to comment.