Commit 73ee6652 authored by Stephane Nicoll's avatar Stephane Nicoll

Use project location to infer the artifactId

On start.spring.io, if you customize the artifactId it creates a zip file
with the same name. The `spring init` command did not have a similar
shortcut.

This commit updates the request to customize the artifactId if none is
set and a custom location was specified. Just as we check for the
presence of a dot to figure out if we have to extract the archive or not,
we check for it to generate an artifactId without an extension.

In practice, `spring init foo` creates a foo directory with a project
whose artifactId is `foo` and `spring init foo.zip` stores a foo.zip
file with the same project (i.e. the artifactId is `foo`).

Closes gh-3714
parent 58d0776a
...@@ -324,8 +324,9 @@ class ProjectGenerationRequest { ...@@ -324,8 +324,9 @@ class ProjectGenerationRequest {
if (this.groupId != null) { if (this.groupId != null) {
builder.setParameter("groupId", this.groupId); builder.setParameter("groupId", this.groupId);
} }
if (this.artifactId != null) { String resolvedArtifactId = resolveArtifactId();
builder.setParameter("artifactId", this.artifactId); if (resolvedArtifactId != null) {
builder.setParameter("artifactId", resolvedArtifactId);
} }
if (this.version != null) { if (this.version != null) {
builder.setParameter("version", this.version); builder.setParameter("version", this.version);
...@@ -405,6 +406,21 @@ class ProjectGenerationRequest { ...@@ -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<String, ProjectType> projects, String tag, private static void filter(Map<String, ProjectType> projects, String tag,
String tagValue) { String tagValue) {
for (Iterator<Map.Entry<String, ProjectType>> it = projects.entrySet().iterator(); it for (Iterator<Map.Entry<String, ProjectType>> it = projects.entrySet().iterator(); it
......
...@@ -139,6 +139,39 @@ public class ProjectGenerationRequestTests { ...@@ -139,6 +139,39 @@ public class ProjectGenerationRequestTests {
this.request.generateUrl(createDefaultMetadata())); 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 @Test
public void buildNoMatch() { public void buildNoMatch() {
InitializrServiceMetadata metadata = readMetadata(); InitializrServiceMetadata metadata = readMetadata();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment