Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the automatic import of the spring-boot-dependencies BOM to be disabled in the Gradle plugin #43042

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.gradle.api.Action;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.plugins.ExtraPropertiesExtension;

/**
* {@link Action} that is performed in response to the {@link DependencyManagementPlugin}
Expand All @@ -30,16 +31,30 @@
*/
final class DependencyManagementPluginAction implements PluginApplicationAction {

public static final String IMPORT_BOM_PROPERTY = "org.springframework.boot.import-bom";

@Override
public void execute(Project project) {
project.getExtensions()
.findByType(DependencyManagementExtension.class)
.imports((importsHandler) -> importsHandler.mavenBom(SpringBootPlugin.BOM_COORDINATES));
if (shouldImportBom(project)) {
project.getExtensions()
.findByType(DependencyManagementExtension.class)
.imports((importsHandler) -> importsHandler.mavenBom(SpringBootPlugin.BOM_COORDINATES));
}
}

@Override
public Class<? extends Plugin<Project>> getPluginClass() {
return DependencyManagementPlugin.class;
}

private boolean shouldImportBom(Project project) {
Object value = project.getExtensions()
.findByType(ExtraPropertiesExtension.class)
.getProperties()
.getOrDefault(IMPORT_BOM_PROPERTY, Boolean.TRUE);

return (value instanceof Boolean && (Boolean) value)
|| (value instanceof String && Boolean.parseBoolean((String) value));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,24 @@ class DependencyManagementPluginActionIntegrationTests {

@TestTemplate
void noDependencyManagementIsAppliedByDefault() {
assertThat(this.gradleBuild.build("doesNotHaveDependencyManagement")
.task(":doesNotHaveDependencyManagement")
assertThat(this.gradleBuild.build("hasNotConfiguredDependencyManagement")
.task(":hasNotConfiguredDependencyManagement")
.getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
}

@TestTemplate
void bomIsImportedWhenDependencyManagementPluginIsApplied() {
assertThat(this.gradleBuild.build("hasDependencyManagement", "-PapplyDependencyManagementPlugin")
.task(":hasDependencyManagement")
assertThat(this.gradleBuild.build("hasConfiguredDependencyManagement", "-PapplyDependencyManagementPlugin")
.task(":hasConfiguredDependencyManagement")
.getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
}

@TestTemplate
void bomIsNotImportedWhenDependencyManagementPluginIsAppliedAndPropertyIsSetToDisableImport() {
assertThat(this.gradleBuild
.build("hasNotConfiguredDependencyManagement", "-PapplyDependencyManagementPlugin",
"-Porg.springframework.boot.import-bom=false")
.task(":hasNotConfiguredDependencyManagement")
.getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,22 @@ repositories {
maven { url 'repository' }
}

task doesNotHaveDependencyManagement {
def isDependencyManagementConfigured() {
def dependencyManagement = project.extensions.findByName('dependencyManagement')
return dependencyManagement != null && dependencyManagement.managedVersions
}

task hasNotConfiguredDependencyManagement {
doLast {
if (project.extensions.findByName('dependencyManagement') != null) {
throw new GradleException('Found dependency management extension')
if (isDependencyManagementConfigured()) {
throw new GradleException('Managed versions have been configured')
}
}
}

task hasDependencyManagement {
task hasConfiguredDependencyManagement {
doLast {
if (!dependencyManagement.managedVersions) {
if (!isDependencyManagementConfigured()) {
throw new GradleException('No managed versions have been configured')
}
}
Expand Down