Skip to content

Commit

Permalink
Resolve dependencies against platform version in BuildMetadataResolver
Browse files Browse the repository at this point in the history
Closes gh-1347
  • Loading branch information
mhalbritter committed Jun 5, 2024
1 parent 999f53c commit f9d74af
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,25 @@
import java.util.stream.Stream;

import io.spring.initializr.generator.buildsystem.Build;
import io.spring.initializr.generator.version.Version;
import io.spring.initializr.metadata.Dependency;
import io.spring.initializr.metadata.InitializrMetadata;

/**
* Resolve metadata information from the build.
*
* @author Stephane Nicoll
* @author Moritz Halbritter
*/
public class BuildMetadataResolver {

private final InitializrMetadata metadata;

public BuildMetadataResolver(InitializrMetadata metadata) {
private final Version platformVersion;

public BuildMetadataResolver(InitializrMetadata metadata, Version platformVersion) {
this.metadata = metadata;
this.platformVersion = platformVersion;
}

/**
Expand All @@ -43,7 +48,11 @@ public BuildMetadataResolver(InitializrMetadata metadata) {
* @return a stream of dependency metadata
*/
public Stream<Dependency> dependencies(Build build) {
return build.dependencies().ids().map((id) -> this.metadata.getDependencies().get(id)).filter(Objects::nonNull);
return build.dependencies()
.ids()
.map((id) -> this.metadata.getDependencies().get(id))
.filter(Objects::nonNull)
.map((dependency) -> dependency.resolve(this.platformVersion));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ BuildCustomizer<Build> junitLauncherContributor() {
}

@Bean
public DefaultStarterBuildCustomizer defaultStarterContributor(InitializrMetadata metadata) {
return new DefaultStarterBuildCustomizer(metadata);
public DefaultStarterBuildCustomizer defaultStarterContributor(InitializrMetadata metadata,
ProjectDescription projectDescription) {
return new DefaultStarterBuildCustomizer(metadata, projectDescription);
}

@Bean
Expand All @@ -81,8 +82,9 @@ public DefaultMavenBuildCustomizer initializrMetadataMavenBuildCustomizer(Projec

@Bean
@ConditionalOnPackaging(WarPackaging.ID)
public WarPackagingWebStarterBuildCustomizer warPackagingWebStarterBuildCustomizer(InitializrMetadata metadata) {
return new WarPackagingWebStarterBuildCustomizer(metadata);
public WarPackagingWebStarterBuildCustomizer warPackagingWebStarterBuildCustomizer(InitializrMetadata metadata,
ProjectDescription projectDescription) {
return new WarPackagingWebStarterBuildCustomizer(metadata, projectDescription);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.spring.initializr.generator.spring.build;

import io.spring.initializr.generator.buildsystem.Build;
import io.spring.initializr.generator.project.ProjectDescription;
import io.spring.initializr.metadata.Dependency;
import io.spring.initializr.metadata.InitializrMetadata;
import io.spring.initializr.metadata.support.MetadataBuildItemMapper;
Expand All @@ -37,8 +38,8 @@ class DefaultStarterBuildCustomizer implements BuildCustomizer<Build> {

private final BuildMetadataResolver buildResolver;

DefaultStarterBuildCustomizer(InitializrMetadata metadata) {
this.buildResolver = new BuildMetadataResolver(metadata);
DefaultStarterBuildCustomizer(InitializrMetadata metadata, ProjectDescription projectDescription) {
this.buildResolver = new BuildMetadataResolver(metadata, projectDescription.getPlatformVersion());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.spring.initializr.generator.spring.build;

import io.spring.initializr.generator.buildsystem.Build;
import io.spring.initializr.generator.project.ProjectDescription;
import io.spring.initializr.metadata.Dependency;
import io.spring.initializr.metadata.InitializrMetadata;
import io.spring.initializr.metadata.support.MetadataBuildItemMapper;
Expand All @@ -36,9 +37,9 @@ public class WarPackagingWebStarterBuildCustomizer implements BuildCustomizer<Bu

private final BuildMetadataResolver buildMetadataResolver;

public WarPackagingWebStarterBuildCustomizer(InitializrMetadata metadata) {
public WarPackagingWebStarterBuildCustomizer(InitializrMetadata metadata, ProjectDescription projectDescription) {
this.metadata = metadata;
this.buildMetadataResolver = new BuildMetadataResolver(metadata);
this.buildMetadataResolver = new BuildMetadataResolver(metadata, projectDescription.getPlatformVersion());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class KotlinJacksonBuildCustomizer implements BuildCustomizer<Build> {
private final ProjectDescription description;

public KotlinJacksonBuildCustomizer(InitializrMetadata metadata, ProjectDescription description) {
this.buildMetadataResolver = new BuildMetadataResolver(metadata);
this.buildMetadataResolver = new BuildMetadataResolver(metadata, description.getPlatformVersion());
this.description = description;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.spring.initializr.generator.spring.code.kotlin;

import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
import io.spring.initializr.generator.project.ProjectDescription;
import io.spring.initializr.generator.spring.build.BuildCustomizer;
import io.spring.initializr.generator.spring.build.BuildMetadataResolver;
import io.spring.initializr.metadata.InitializrMetadata;
Expand All @@ -34,8 +35,9 @@ public class KotlinJpaGradleBuildCustomizer implements BuildCustomizer<GradleBui

private final KotlinProjectSettings settings;

public KotlinJpaGradleBuildCustomizer(InitializrMetadata metadata, KotlinProjectSettings settings) {
this.buildMetadataResolver = new BuildMetadataResolver(metadata);
public KotlinJpaGradleBuildCustomizer(InitializrMetadata metadata, KotlinProjectSettings settings,
ProjectDescription projectDescription) {
this.buildMetadataResolver = new BuildMetadataResolver(metadata, projectDescription.getPlatformVersion());
this.settings = settings;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.spring.initializr.generator.spring.code.kotlin;

import io.spring.initializr.generator.buildsystem.maven.MavenBuild;
import io.spring.initializr.generator.project.ProjectDescription;
import io.spring.initializr.generator.spring.build.BuildCustomizer;
import io.spring.initializr.generator.spring.build.BuildMetadataResolver;
import io.spring.initializr.metadata.InitializrMetadata;
Expand All @@ -32,8 +33,8 @@ public class KotlinJpaMavenBuildCustomizer implements BuildCustomizer<MavenBuild

private final BuildMetadataResolver buildMetadataResolver;

public KotlinJpaMavenBuildCustomizer(InitializrMetadata metadata) {
this.buildMetadataResolver = new BuildMetadataResolver(metadata);
public KotlinJpaMavenBuildCustomizer(InitializrMetadata metadata, ProjectDescription projectDescription) {
this.buildMetadataResolver = new BuildMetadataResolver(metadata, projectDescription.getPlatformVersion());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,15 @@ BuildCustomizer<Build> kotlinDependenciesConfigurer() {
@Bean
@ConditionalOnBuildSystem(GradleBuildSystem.ID)
KotlinJpaGradleBuildCustomizer kotlinJpaGradleBuildCustomizer(InitializrMetadata metadata,
KotlinProjectSettings settings) {
return new KotlinJpaGradleBuildCustomizer(metadata, settings);
KotlinProjectSettings settings, ProjectDescription projectDescription) {
return new KotlinJpaGradleBuildCustomizer(metadata, settings, projectDescription);
}

@Bean
@ConditionalOnBuildSystem(MavenBuildSystem.ID)
KotlinJpaMavenBuildCustomizer kotlinJpaMavenBuildCustomizer(InitializrMetadata metadata) {
return new KotlinJpaMavenBuildCustomizer(metadata);
KotlinJpaMavenBuildCustomizer kotlinJpaMavenBuildCustomizer(InitializrMetadata metadata,
ProjectDescription projectDescription) {
return new KotlinJpaMavenBuildCustomizer(metadata, projectDescription);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.spring.initializr.generator.spring.configuration;

import io.spring.initializr.generator.buildsystem.Build;
import io.spring.initializr.generator.project.ProjectDescription;
import io.spring.initializr.generator.project.ProjectGenerationConfiguration;
import io.spring.initializr.metadata.InitializrMetadata;

Expand All @@ -32,8 +33,9 @@
public class ApplicationConfigurationProjectGenerationConfiguration {

@Bean
WebFoldersContributor webFoldersContributor(Build build, InitializrMetadata metadata) {
return new WebFoldersContributor(build, metadata);
WebFoldersContributor webFoldersContributor(Build build, InitializrMetadata metadata,
ProjectDescription projectDescription) {
return new WebFoldersContributor(build, metadata, projectDescription);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.nio.file.Path;

import io.spring.initializr.generator.buildsystem.Build;
import io.spring.initializr.generator.project.ProjectDescription;
import io.spring.initializr.generator.project.contributor.ProjectContributor;
import io.spring.initializr.generator.spring.build.BuildMetadataResolver;
import io.spring.initializr.metadata.InitializrMetadata;
Expand All @@ -39,9 +40,9 @@ public class WebFoldersContributor implements ProjectContributor {

private final BuildMetadataResolver buildMetadataResolver;

public WebFoldersContributor(Build build, InitializrMetadata metadata) {
public WebFoldersContributor(Build build, InitializrMetadata metadata, ProjectDescription projectDescription) {
this.build = build;
this.buildMetadataResolver = new BuildMetadataResolver(metadata);
this.buildMetadataResolver = new BuildMetadataResolver(metadata, projectDescription.getPlatformVersion());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.spring.initializr.generator.spring.build;

import java.util.Arrays;
import java.util.List;

import io.spring.initializr.generator.buildsystem.Build;
import io.spring.initializr.generator.buildsystem.DependencyScope;
Expand All @@ -35,28 +36,49 @@
* Tests for {@link BuildMetadataResolver}.
*
* @author Stephane Nicoll
* @author Moritz Halbritter
*/
class BuildMetadataResolverTests {

private static final Version PLATFORM_VERSION = Version.parse("1.0.0");

@Test
void dependenciesFiltersDependenciesWithNoMetadata() {
InitializrMetadata metadata = createSampleMetadata();
Build build = createBuild(metadata);
build.dependencies().add("three");
build.dependencies().add("five", "com.example", "five", DependencyScope.COMPILE);
build.dependencies().add("one");
BuildMetadataResolver resolver = new BuildMetadataResolver(metadata);
BuildMetadataResolver resolver = new BuildMetadataResolver(metadata, PLATFORM_VERSION);
assertThat(resolver.dependencies(build)).hasSize(2);
assertThat(resolver.dependencies(build).map(MetadataElement::getId)).containsExactly("three", "one");
}

@Test
void shouldResolveDependenciesAgainstMapping() {
Dependency web = Dependency.withId("web", "group", "artifact");
web.setMappings(List.of(Dependency.Mapping.create("[1.0.0,2.0.0)", null, null, "1.0.0", null, null, null),
Dependency.Mapping.create("[2.0.0,3.0.0)", null, null, "2.0.0", null, null, null)));
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
.addDependencyGroup("web", web)
.build();
Build build = createBuild(metadata);
build.dependencies().add("web");
BuildMetadataResolver resolver = new BuildMetadataResolver(metadata, Version.parse("1.0.0"));
assertThat(resolver.dependencies(build)).singleElement()
.satisfies((dependency) -> assertThat(dependency.getVersion()).isEqualTo("1.0.0"));
resolver = new BuildMetadataResolver(metadata, Version.parse("2.0.0"));
assertThat(resolver.dependencies(build)).singleElement()
.satisfies((dependency) -> assertThat(dependency.getVersion()).isEqualTo("2.0.0"));
}

@Test
void hasFacetWithMatchingFacet() {
InitializrMetadata metadata = createSampleMetadata();
Build build = createBuild(metadata);
build.dependencies().add("one");
build.dependencies().add("my-web");
assertThat(new BuildMetadataResolver(metadata).hasFacet(build, "web")).isTrue();
assertThat(new BuildMetadataResolver(metadata, PLATFORM_VERSION).hasFacet(build, "web")).isTrue();
}

@Test
Expand All @@ -65,7 +87,7 @@ void hasFacetWithNonMatchingFacet() {
Build build = createBuild(metadata);
build.dependencies().add("my-custom");
build.dependencies().add("my-web");
assertThat(new BuildMetadataResolver(metadata).hasFacet(build, "nope")).isFalse();
assertThat(new BuildMetadataResolver(metadata, PLATFORM_VERSION).hasFacet(build, "nope")).isFalse();
}

private InitializrMetadata createSampleMetadata() {
Expand All @@ -80,7 +102,7 @@ private InitializrMetadata createSampleMetadata() {
}

private Build createBuild(InitializrMetadata metadata) {
return new MavenBuild(new MetadataBuildItemResolver(metadata, Version.parse("2.0.0.RELEASE")));
return new MavenBuild(new MetadataBuildItemResolver(metadata, PLATFORM_VERSION));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@

import io.spring.initializr.generator.buildsystem.Build;
import io.spring.initializr.generator.buildsystem.maven.MavenBuild;
import io.spring.initializr.generator.project.MutableProjectDescription;
import io.spring.initializr.generator.project.ProjectDescription;
import io.spring.initializr.generator.test.InitializrMetadataTestBuilder;
import io.spring.initializr.generator.version.Version;
import io.spring.initializr.metadata.Dependency;
import io.spring.initializr.metadata.InitializrMetadata;
import io.spring.initializr.metadata.support.MetadataBuildItemResolver;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -34,6 +37,15 @@
*/
class DefaultStarterBuildCustomizerTests {

private ProjectDescription projectDescription;

@BeforeEach
void setUp() {
MutableProjectDescription description = new MutableProjectDescription();
description.setPlatformVersion(Version.parse("1.0.0"));
this.projectDescription = description;
}

@Test
void defaultStarterIsAddedIfNoneExists() {
Dependency dependency = Dependency.withId("acme", "com.example", "acme");
Expand All @@ -43,7 +55,7 @@ void defaultStarterIsAddedIfNoneExists() {
.build();
Build build = createBuild(metadata);
build.dependencies().add("acme");
new DefaultStarterBuildCustomizer(metadata).customize(build);
new DefaultStarterBuildCustomizer(metadata, this.projectDescription).customize(build);
assertThat(build.dependencies().ids()).containsOnly("acme", DefaultStarterBuildCustomizer.DEFAULT_STARTER);
}

Expand All @@ -56,7 +68,7 @@ void defaultStarterIsAddedIfNoCompileScopedStarterExists() {
.build();
Build build = createBuild(metadata);
build.dependencies().add("runtime");
new DefaultStarterBuildCustomizer(metadata).customize(build);
new DefaultStarterBuildCustomizer(metadata, this.projectDescription).customize(build);
assertThat(build.dependencies().ids()).containsOnly("runtime", DefaultStarterBuildCustomizer.DEFAULT_STARTER);
}

Expand All @@ -67,7 +79,7 @@ void defaultStarterIsNotAddedIfCompileScopedStarterExists() {
.build();
Build build = createBuild(metadata);
build.dependencies().add("web");
new DefaultStarterBuildCustomizer(metadata).customize(build);
new DefaultStarterBuildCustomizer(metadata, this.projectDescription).customize(build);
assertThat(build.dependencies().ids()).containsOnly("web");
}

Expand Down
Loading

0 comments on commit f9d74af

Please sign in to comment.