diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ProjectGenerationRequest.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ProjectGenerationRequest.java index 2ce8b6328ea..639e9b4581d 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ProjectGenerationRequest.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ProjectGenerationRequest.java @@ -324,8 +324,9 @@ class ProjectGenerationRequest { if (this.groupId != null) { builder.setParameter("groupId", this.groupId); } - if (this.artifactId != null) { - builder.setParameter("artifactId", this.artifactId); + String resolvedArtifactId = resolveArtifactId(); + if (resolvedArtifactId != null) { + builder.setParameter("artifactId", resolvedArtifactId); } if (this.version != null) { builder.setParameter("version", this.version); @@ -405,6 +406,21 @@ class ProjectGenerationRequest { } } + /** + * Resolve the artifactId to use or {@code null} if it should not be customized. + * @return the artifactId + */ + protected String resolveArtifactId() { + if (this.artifactId != null) { + return this.artifactId; + } + if (this.output != null) { + int i = this.output.lastIndexOf('.'); + return (i == -1 ? this.output : this.output.substring(0, i)); + } + return null; + } + private static void filter(Map projects, String tag, String tagValue) { for (Iterator> it = projects.entrySet().iterator(); it diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/ProjectGenerationRequestTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/ProjectGenerationRequestTests.java index 9374b990aa7..78702601553 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/ProjectGenerationRequestTests.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/ProjectGenerationRequestTests.java @@ -139,6 +139,39 @@ public class ProjectGenerationRequestTests { this.request.generateUrl(createDefaultMetadata())); } + @Test + public void outputCustomizeArtifactId() { + this.request.setOutput("my-project"); + assertEquals( + createDefaultUrl("?artifactId=my-project&type=test-type"), + this.request.generateUrl(createDefaultMetadata())); + } + + @Test + public void outputArchiveCustomizeArtifactId() { + this.request.setOutput("my-project.zip"); + assertEquals( + createDefaultUrl("?artifactId=my-project&type=test-type"), + this.request.generateUrl(createDefaultMetadata())); + } + + @Test + public void outputArchiveWithDotsCustomizeArtifactId() { + this.request.setOutput("my.nice.project.zip"); + assertEquals( + createDefaultUrl("?artifactId=my.nice.project&type=test-type"), + this.request.generateUrl(createDefaultMetadata())); + } + + @Test + public void outputDoesNotOverrideCustomArtifactId() { + this.request.setOutput("my-project"); + this.request.setArtifactId("my-id"); + assertEquals( + createDefaultUrl("?artifactId=my-id&type=test-type"), + this.request.generateUrl(createDefaultMetadata())); + } + @Test public void buildNoMatch() { InitializrServiceMetadata metadata = readMetadata();