Use build tag to infer the build system

Stop using the type's prefix to infer the build system to use as the
build tag in the metadata is supposed to provide that information.

See gh-817
This commit is contained in:
Stephane Nicoll 2019-02-11 14:37:35 +01:00
parent c98f59e8f2
commit 08c785b952
3 changed files with 42 additions and 18 deletions

View File

@ -227,15 +227,14 @@ By default, Spring Initializr exposes the following resources (all accessed via
* `/starter.zip` generate a complete project structure archived in a zip
* `/starter.tgz` generate a complete project structure archived in a tgz
Each type also defines one or more *tags* that provides additional metadata entries to
qualify the entry. The following standard tags exist:
The build system must be defined with a `build` tag providing the name of the
`BuildSystem` to use (e.g. `maven`, `gradle`).
* `build`: the name of the `BuildSystem` to use (e.g. `maven`, `gradle`)
* `format`: the format of the project (e.g. `project` for a full project, `build` for just
a build file).
By default, the HTML UI filters all the available types to only display the ones that have
a `format` tag with value `project`.
Additional tags can be provided to further qualify the entry. Besides the mandatory `build`
tag, a `format` tag is also available to define the format of the project (e.g. `project`
for a full project, `build` for just a build file). By default, the HTML UI filters all
the available types to only display the ones that have a `format` tag with value
`project`.
You can of course implement additional endpoints that generate whatever project structure
you need but, for now, we'll simply configure our instance to generate a Gradle or a Maven

View File

@ -20,8 +20,6 @@ import java.util.List;
import java.util.stream.Collectors;
import io.spring.initializr.generator.buildsystem.BuildSystem;
import io.spring.initializr.generator.buildsystem.gradle.GradleBuildSystem;
import io.spring.initializr.generator.buildsystem.maven.MavenBuildSystem;
import io.spring.initializr.generator.language.Language;
import io.spring.initializr.generator.packaging.Packaging;
import io.spring.initializr.generator.project.ProjectDescription;
@ -50,7 +48,7 @@ public class ProjectRequestToDescriptionConverter {
description.setApplicationName(getApplicationName(request, metadata));
description.setArtifactId(request.getArtifactId());
description.setBaseDirectory(request.getBaseDir());
description.setBuildSystem(getBuildSystem(request));
description.setBuildSystem(getBuildSystem(request, metadata));
description.setDescription(request.getDescription());
description.setGroupId(request.getGroupId());
description.setLanguage(
@ -89,6 +87,10 @@ public class ProjectRequestToDescriptionConverter {
throw new InvalidProjectRequestException(
"Unknown type '" + type + "' check project metadata");
}
if (!typeFromMetadata.getTags().containsKey("build")) {
throw new InvalidProjectRequestException("Invalid type '" + type
+ "' (missing build tag) check project metadata");
}
}
}
@ -127,9 +129,10 @@ public class ProjectRequestToDescriptionConverter {
});
}
private BuildSystem getBuildSystem(ProjectRequest request) {
return (request.getType().startsWith("gradle")) ? new GradleBuildSystem()
: new MavenBuildSystem();
private BuildSystem getBuildSystem(ProjectRequest request,
InitializrMetadata metadata) {
Type typeFromMetadata = metadata.getTypes().get(request.getType());
return BuildSystem.forId(typeFromMetadata.getTags().get("build"));
}
private String getPackageName(ProjectRequest request, InitializrMetadata metadata) {

View File

@ -18,10 +18,12 @@ package io.spring.initializr.web.project;
import java.util.Collections;
import io.spring.initializr.generator.buildsystem.gradle.GradleBuildSystem;
import io.spring.initializr.generator.project.ProjectDescription;
import io.spring.initializr.generator.spring.test.InitializrMetadataTestBuilder;
import io.spring.initializr.generator.version.Version;
import io.spring.initializr.metadata.InitializrMetadata;
import io.spring.initializr.metadata.Type;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
@ -31,6 +33,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
* Tests for {@link ProjectRequestToDescriptionConverter}.
*
* @author Madhura Bhave
* @author Stephane Nicoll
*/
public class ProjectRequestToDescriptionConverterTests {
@ -48,6 +51,20 @@ public class ProjectRequestToDescriptionConverterTests {
.withMessage("Unknown type 'foo-build' check project metadata");
}
@Test
public void convertWhenTypeDoesNotDefineBuildTagShouldThrowException() {
Type type = new Type();
type.setId("example-project");
InitializrMetadata testMetadata = InitializrMetadataTestBuilder.withDefaults()
.addType(type).build();
ProjectRequest request = getProjectRequest();
request.setType("example-project");
assertThatExceptionOfType(InvalidProjectRequestException.class)
.isThrownBy(() -> this.converter.convert(request, testMetadata))
.withMessage(
"Invalid type 'example-project' (missing build tag) check project metadata");
}
@Test
void convertWhenSpringBootVersionInvalidShouldThrowException() {
ProjectRequest request = getProjectRequest();
@ -119,11 +136,16 @@ public class ProjectRequestToDescriptionConverterTests {
}
@Test
void convertShouldSetBuildSystemFromRequestType() {
void convertShouldSetBuildSystemFromRequestTypeAndBuildTag() {
Type type = new Type();
type.setId("example-type");
type.getTags().put("build", "gradle");
InitializrMetadata testMetadata = InitializrMetadataTestBuilder.withDefaults()
.addType(type).build();
ProjectRequest request = getProjectRequest();
request.setType("gradle-build");
ProjectDescription description = this.converter.convert(request, this.metadata);
assertThat(description.getBuildSystem().id()).isEqualTo("gradle");
request.setType("example-type");
ProjectDescription description = this.converter.convert(request, testMetadata);
assertThat(description.getBuildSystem()).isInstanceOf(GradleBuildSystem.class);
}
@Test