Add a strategy to resolve the dependency management plugin version

Closes gh-977
This commit is contained in:
Stephane Nicoll
2019-08-13 14:46:02 +02:00
parent 1672c69ab9
commit c1b1b36f25
4 changed files with 104 additions and 2 deletions

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2012-2019 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 io.spring.initializr.generator.spring.build.gradle;
import io.spring.initializr.generator.project.ResolvedProjectDescription;
/**
* Strategy for resolving a dependency management plugin version from a platform version.
*
* @author Stephane Nicoll
*/
@FunctionalInterface
public interface DependencyManagementPluginVersionResolver {
/**
* Resolves the dependency management plugin version to use for the generation of the
* project with the given {@code description}.
* @param description the description of the project being generated
* @return the corresponding version for the {@code io.spring.dependency-management}
* plugin
*/
String resolveDependencyManagementPluginVersion(ResolvedProjectDescription description);
}

View File

@@ -245,12 +245,15 @@ public class GradleProjectGenerationConfiguration {
@Bean
BuildCustomizer<GradleBuild> springBootPluginContributor(ResolvedProjectDescription projectDescription,
ObjectProvider<DependencyManagementPluginVersionResolver> versionResolver,
InitializrMetadata metadata) {
return (build) -> {
build.plugins().add("org.springframework.boot",
(plugin) -> plugin.setVersion(projectDescription.getPlatformVersion().toString()));
build.plugins().add("io.spring.dependency-management", (plugin) -> plugin.setVersion(
metadata.getConfiguration().getEnv().getGradle().getDependencyManagementPluginVersion()));
build.plugins().add("io.spring.dependency-management",
(plugin) -> plugin.setVersion(versionResolver
.getIfAvailable(() -> new InitializrDependencyManagementPluginVersionResolver(metadata))
.resolveDependencyManagementPluginVersion(projectDescription)));
};
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright 2012-2019 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 io.spring.initializr.generator.spring.build.gradle;
import io.spring.initializr.generator.project.ResolvedProjectDescription;
import io.spring.initializr.metadata.InitializrMetadata;
/**
* {@link DependencyManagementPluginVersionResolver} that resolves the version from the
* {@link InitializrMetadata}.
*
* @author Stephane Nicoll
*/
public class InitializrDependencyManagementPluginVersionResolver implements DependencyManagementPluginVersionResolver {
private final InitializrMetadata metadata;
public InitializrDependencyManagementPluginVersionResolver(InitializrMetadata metadata) {
this.metadata = metadata;
}
@Override
public String resolveDependencyManagementPluginVersion(ResolvedProjectDescription description) {
return this.metadata.getConfiguration().getEnv().getGradle().getDependencyManagementPluginVersion();
}
}

View File

@@ -126,6 +126,26 @@ class GradleKtsProjectGenerationConfigurationTests {
" testImplementation(\"org.springframework.boot:spring-boot-starter-test\")", "}");
}
@Test
void dependencyManagementPluginFallbacksToMetadataIfNotPresent() {
ProjectDescription description = new ProjectDescription();
description.setPlatformVersion(Version.parse("2.1.0.RELEASE"));
description.setLanguage(new JavaLanguage("11"));
List<String> lines = this.projectTester.generate(description).readAllLines("build.gradle.kts");
assertThat(lines).contains(" id(\"io.spring.dependency-management\") version \"1.0.6.RELEASE\"");
}
@Test
void dependencyManagementPluginVersionResolverIsUsedIfPresent() {
ProjectDescription description = new ProjectDescription();
description.setPlatformVersion(Version.parse("2.1.0.RELEASE"));
description.setLanguage(new JavaLanguage("11"));
List<String> lines = this.projectTester
.withBean(DependencyManagementPluginVersionResolver.class, () -> (d) -> "1.5.1.RC1")
.generate(description).readAllLines("build.gradle.kts");
assertThat(lines).contains(" id(\"io.spring.dependency-management\") version \"1.5.1.RC1\"");
}
@Test
void warPluginIsAppliedWhenBuildingProjectThatUsesWarPackaging() {
ProjectDescription description = new ProjectDescription();