diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitCommand.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitCommand.java index 18d2e81346..9c4631fb87 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitCommand.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitCommand.java @@ -79,35 +79,35 @@ public class InitCommand extends OptionParsingCommand { private OptionSpec listCapabilities; - private OptionSpec bootVersion; + private OptionSpec groupId; - private OptionSpec dependencies; + private OptionSpec artifactId; - private OptionSpec javaVersion; + private OptionSpec version; - private OptionSpec packaging; + private OptionSpec name; - private OptionSpec build; + private OptionSpec description; - private OptionSpec format; + private OptionSpec packaging; private OptionSpec type; - private OptionSpec extract; + private OptionSpec build; - private OptionSpec force; + private OptionSpec format; - private OptionSpec language; + private OptionSpec javaVersion; - private OptionSpec groupId; + private OptionSpec language; - private OptionSpec artifactId; + private OptionSpec bootVersion; - private OptionSpec name; + private OptionSpec dependencies; - private OptionSpec version; + private OptionSpec extract; - private OptionSpec description; + private OptionSpec force; InitOptionHandler(InitializrService initializrService) { this.serviceCapabilitiesReport = new ServiceCapabilitiesReportGenerator( @@ -129,48 +129,48 @@ public class InitCommand extends OptionParsingCommand { } private void projectGenerationOptions() { - this.bootVersion = option(Arrays.asList("boot-version", "b"), - "Spring Boot version to use (for example '1.2.0.RELEASE')") + this.groupId = option(Arrays.asList("groupId", "g"), + "Project coordinates (for example 'org.test')") + .withRequiredArg(); + this.artifactId = option(Arrays.asList("artifactId", "a"), + "Project coordinates; infer archive name (for example 'test')") + .withRequiredArg(); + this.version = option(Arrays.asList("version", "v"), + "Project version (for example '0.0.1-SNAPSHOT')") + .withRequiredArg(); + this.name = option(Arrays.asList("name", "n"), + "Project name; infer application name") + .withRequiredArg(); + this.description = option("description", + "Project description") .withRequiredArg(); - this.dependencies = option( - Arrays.asList("dependencies", "d"), - "Comma separated list of dependencies to include in the " - + "generated project").withRequiredArg(); - this.javaVersion = option(Arrays.asList("java-version", "j"), - "Java version to use (for example '1.8')").withRequiredArg(); this.packaging = option(Arrays.asList("packaging", "p"), - "Packaging type to use (for example 'jar')").withRequiredArg(); + "Project packaging (for example 'jar')").withRequiredArg(); + this.type = option( + Arrays.asList("type", "t"), + "Project type. Not normally needed if you use --build " + + "and/or --format. Check the capabilities of the service " + + "(--list) for more details").withRequiredArg(); this.build = option("build", - "The build system to use (for example 'maven' or 'gradle')") + "Build system to use (for example 'maven' or 'gradle')") .withRequiredArg().defaultsTo("maven"); this.format = option( "format", - "The format of the generated content (for example 'build' for a build file, " + "Format of the generated content (for example 'build' for a build file, " + "'project' for a project archive)").withRequiredArg() .defaultsTo("project"); - this.type = option( - Arrays.asList("type", "t"), - "The project type to use. Not normally needed if you use --build " - + "and/or --format. Check the capabilities of the service " - + "(--list) for more details").withRequiredArg(); - this.language = option(Arrays.asList("language", "lang"), - "Programming Language to use (for example 'java')") - .withRequiredArg(); - this.groupId = option(Arrays.asList("groupId", "g"), - "The project group that produced the dependency (for example 'org.test')") - .withRequiredArg(); - this.artifactId = option(Arrays.asList("artifactId", "a"), - "The unique id for an artifact produced by the project group (for example 'test')") - .withRequiredArg(); - this.name = option(Arrays.asList("name", "n"), - "The full name of the project.") - .withRequiredArg(); - this.version = option(Arrays.asList("version", "v"), - "The version of the dependency (for example '0.0.1-SNAPSHOT')") - .withRequiredArg(); - this.description = option(Arrays.asList("description", "des"), - "A detailed description of the project") - .withRequiredArg(); + this.javaVersion = option(Arrays.asList("java-version", "j"), + "Language level (for example '1.8')").withRequiredArg(); + this.language = option(Arrays.asList("language", "l"), + "Programming language (for example 'java')") + .withRequiredArg(); + this.bootVersion = option(Arrays.asList("boot-version", "b"), + "Spring Boot version (for example '1.2.0.RELEASE')") + .withRequiredArg(); + this.dependencies = option( + Arrays.asList("dependencies", "d"), + "Comma-separated list of dependency identifiers to include in the " + + "generated project").withRequiredArg(); } private void otherOptions() { 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 38f8b188f7..5219431511 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 @@ -25,12 +25,14 @@ import java.util.List; import java.util.Map; import org.apache.http.client.utils.URIBuilder; + import org.springframework.util.StringUtils; /** * Represent the settings to apply to generating the project. * * @author Stephane Nicoll + * @author EddĂș MelĂ©ndez * @since 1.2.0 */ class ProjectGenerationRequest { @@ -43,33 +45,33 @@ class ProjectGenerationRequest { private boolean extract; - private String bootVersion; + private String groupId; - private List dependencies = new ArrayList(); + private String artifactId; - private String javaVersion; + private String version; + + private String name; + + private String description; private String packaging; + private String type; + private String build; private String format; private boolean detectType; - private String type; + private String javaVersion; private String language; - private String groupId; - - private String artifactId; - - private String name; - - private String version; + private String bootVersion; - private String description; + private List dependencies = new ArrayList(); /** * The URL of the service to use. @@ -117,35 +119,63 @@ class ProjectGenerationRequest { } /** - * The Spring Boot version to use or {@code null} if it should not be customized. - * @return the Spring Boot version of {@code null} + * The groupId to use or {@code null} if it should not be customized. + * @return the groupId or {@code null} */ - public String getBootVersion() { - return this.bootVersion; + public String getGroupId() { + return this.groupId; } - public void setBootVersion(String bootVersion) { - this.bootVersion = bootVersion; + public void setGroupId(String groupId) { + this.groupId = groupId; } /** - * The identifiers of the dependencies to include in the project. - * @return the dependency identifiers + * The artifactId to use or {@code null} if it should not be customized. + * @return the artifactId or {@code null} */ - public List getDependencies() { - return this.dependencies; + public String getArtifactId() { + return this.artifactId; + } + + public void setArtifactId(String artifactId) { + this.artifactId = artifactId; } /** - * The Java version to use or {@code null} if it should not be customized. - * @return the Java version or {@code null} + * The artifact version to use or {@code null} if it should not be customized. + * @return the artifact version or {@code null} */ - public String getJavaVersion() { - return this.javaVersion; + public String getVersion() { + return this.version; } - public void setJavaVersion(String javaVersion) { - this.javaVersion = javaVersion; + public void setVersion(String version) { + this.version = version; + } + + /** + * The name to use or {@code null} if it should not be customized. + * @return the name or {@code null} + */ + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + /** + * The description to use or {@code null} if it should not be customized. + * @return the description or {@code null} + */ + public String getDescription() { + return this.description; + } + + public void setDescription(String description) { + this.description = description; } /** @@ -160,6 +190,19 @@ class ProjectGenerationRequest { this.packaging = packaging; } + /** + * The type of project to generate. Should match one of the advertized type that the + * service supports. If not set, the default is retrieved from the service metadata. + * @return the project type + */ + public String getType() { + return this.type; + } + + public void setType(String type) { + this.type = type; + } + /** * The build type to use. Ignored if a type is set. Can be used alongside the * {@link #getFormat() format} to identify the type to use. @@ -199,20 +242,20 @@ class ProjectGenerationRequest { } /** - * The type of project to generate. Should match one of the advertized type that the - * service supports. If not set, the default is retrieved from the service metadata. - * @return the project type + * The Java version to use or {@code null} if it should not be customized. + * @return the Java version or {@code null} */ - public String getType() { - return this.type; + public String getJavaVersion() { + return this.javaVersion; } - public void setType(String type) { - this.type = type; + public void setJavaVersion(String javaVersion) { + this.javaVersion = javaVersion; } /** * The programming language to use or {@code null} if it should not be customized. + * @return the programming language or {@code null} */ public String getLanguage() { return this.language; @@ -223,58 +266,23 @@ class ProjectGenerationRequest { } /** - * The groupId to use or {@code null} if it should not be customized. - */ - public String getGroupId() { - return this.groupId; - } - - public void setGroupId(String groupId) { - this.groupId = groupId; - } - - /** - * The artifactId to use or {@code null} if it should not be customized. - */ - public String getArtifactId() { - return this.artifactId; - } - - public void setArtifactId(String artifactId) { - this.artifactId = artifactId; - } - - /** - * The name to use or {@code null} if it should not be customized. - */ - public String getName() { - return this.name; - } - - public void setName(String name) { - this.name = name; - } - - /** - * The artifact version to use or {@code null} if it should not be customized. + * The Spring Boot version to use or {@code null} if it should not be customized. + * @return the Spring Boot version or {@code null} */ - public String getVersion() { - return this.version; + public String getBootVersion() { + return this.bootVersion; } - public void setVersion(String version) { - this.version = version; + public void setBootVersion(String bootVersion) { + this.bootVersion = bootVersion; } /** - * + * The identifiers of the dependencies to include in the project. + * @return the dependency identifiers */ - public String getDescription() { - return this.description; - } - - public void setDescription(String description) { - this.description = description; + public List getDependencies() { + return this.dependencies; } /** @@ -295,40 +303,40 @@ class ProjectGenerationRequest { sb.append(projectType.getAction()); builder.setPath(sb.toString()); - if (this.artifactId != null) { - builder.setParameter("artifactId", this.artifactId); - } - if (this.bootVersion != null) { - builder.setParameter("bootVersion", this.bootVersion); - } - if (!this.dependencies.isEmpty()) { builder.setParameter("dependencies", StringUtils.collectionToCommaDelimitedString(this.dependencies)); } - if (this.description != null) { - builder.setParameter("description", this.description); - } + if (this.groupId != null) { builder.setParameter("groupId", this.groupId); } - if (this.javaVersion != null) { - builder.setParameter("javaVersion", this.javaVersion); + if (this.artifactId != null) { + builder.setParameter("artifactId", this.artifactId); } - if (this.language != null) { - builder.setParameter("language", this.language); + if (this.version != null) { + builder.setParameter("version", this.version); } if (this.name != null) { builder.setParameter("name", this.name); } + if (this.description != null) { + builder.setParameter("description", this.description); + } if (this.packaging != null) { builder.setParameter("packaging", this.packaging); } if (this.type != null) { builder.setParameter("type", projectType.getId()); } - if (this.version != null) { - builder.setParameter("version", this.version); + if (this.javaVersion != null) { + builder.setParameter("javaVersion", this.javaVersion); + } + if (this.language != null) { + builder.setParameter("language", this.language); + } + if (this.bootVersion != null) { + builder.setParameter("bootVersion", this.bootVersion); } return builder.build(); @@ -384,7 +392,7 @@ class ProjectGenerationRequest { private static void filter(Map projects, String tag, String tagValue) { for (Iterator> it = projects.entrySet().iterator(); it - .hasNext();) { + .hasNext(); ) { Map.Entry entry = it.next(); String value = entry.getValue().getTags().get(tag); if (!tagValue.equals(value)) { diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitCommandTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitCommandTests.java index da9a42df07..8b233da6a2 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitCommandTests.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitCommandTests.java @@ -25,7 +25,6 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import joptsimple.OptionSet; - import org.apache.http.Header; import org.apache.http.client.methods.HttpUriRequest; import org.junit.Before; @@ -35,6 +34,7 @@ import org.junit.rules.TemporaryFolder; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.MockitoAnnotations; + import org.springframework.boot.cli.command.status.ExitStatus; import static org.hamcrest.Matchers.startsWith; @@ -262,6 +262,31 @@ public class InitCommandTests extends AbstractHttpClientMockTests { assertEquals("File should not have changed", fileLength, conflict.length()); } + @Test + public void parseProjectOptions() throws Exception { + this.handler.disableProjectGeneration(); + this.command.run("-g=org.demo", "-a=acme", "-v=1.2.3-SNAPSHOT", "-n=acme-sample", + "--description=Acme sample project", "-p=war", "-t=ant-project", + "--build=grunt", "--format=web", "-j=1.9", "-l=groovy", + "-b=1.2.0.RELEASE", "-d=web,data-jpa"); + assertEquals("org.demo", this.handler.lastRequest.getGroupId()); + assertEquals("acme", this.handler.lastRequest.getArtifactId()); + assertEquals("1.2.3-SNAPSHOT", this.handler.lastRequest.getVersion()); + assertEquals("acme-sample", this.handler.lastRequest.getName()); + assertEquals("Acme sample project", this.handler.lastRequest.getDescription()); + assertEquals("war", this.handler.lastRequest.getPackaging()); + assertEquals("ant-project", this.handler.lastRequest.getType()); + assertEquals("grunt", this.handler.lastRequest.getBuild()); + assertEquals("web", this.handler.lastRequest.getFormat()); + assertEquals("1.9", this.handler.lastRequest.getJavaVersion()); + assertEquals("groovy", this.handler.lastRequest.getLanguage()); + assertEquals("1.2.0.RELEASE", this.handler.lastRequest.getBootVersion()); + List dependencies = this.handler.lastRequest.getDependencies(); + assertEquals(2, dependencies.size()); + assertTrue(dependencies.contains("web")); + assertTrue(dependencies.contains("data-jpa")); + } + @Test public void overwriteFileInArchive() throws Exception { File folder = this.temporaryFolder.newFolder(); @@ -278,30 +303,6 @@ public class InitCommandTests extends AbstractHttpClientMockTests { assertTrue("File should have changed", fileLength != conflict.length()); } - @Test - public void parseProjectOptions() throws Exception { - this.handler.disableProjectGeneration(); - this.command.run("-b=1.2.0.RELEASE", "-d=web,data-jpa", "-j=1.9", "-p=war", "--build=grunt", - "--format=web", "-t=ant-project", "-lang=groovy", "-g=org.test", "-a=demo", "-n=demo", - "-v=0.0.1-SNAPSHOT", "-des=Demo project for Spring Boot"); - assertEquals("1.2.0.RELEASE", this.handler.lastRequest.getBootVersion()); - List dependencies = this.handler.lastRequest.getDependencies(); - assertEquals(2, dependencies.size()); - assertTrue(dependencies.contains("web")); - assertTrue(dependencies.contains("data-jpa")); - assertEquals("1.9", this.handler.lastRequest.getJavaVersion()); - assertEquals("war", this.handler.lastRequest.getPackaging()); - assertEquals("grunt", this.handler.lastRequest.getBuild()); - assertEquals("web", this.handler.lastRequest.getFormat()); - assertEquals("ant-project", this.handler.lastRequest.getType()); - assertEquals("groovy", this.handler.lastRequest.getLanguage()); - assertEquals("org.test", this.handler.lastRequest.getGroupId()); - assertEquals("demo", this.handler.lastRequest.getArtifactId()); - assertEquals("demo", this.handler.lastRequest.getName()); - assertEquals("0.0.1-SNAPSHOT", this.handler.lastRequest.getVersion()); - assertEquals("Demo project for Spring Boot", this.handler.lastRequest.getDescription()); - } - @Test public void parseTypeOnly() throws Exception { this.handler.disableProjectGeneration(); 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 6c07eab3bb..19fc80c8e1 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 @@ -27,6 +27,7 @@ import org.json.JSONObject; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; + import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.util.StreamUtils; @@ -68,7 +69,7 @@ public class ProjectGenerationRequestTests { @Test public void customBootVersion() { this.request.setBootVersion("1.2.0.RELEASE"); - assertEquals(createDefaultUrl("?bootVersion=1.2.0.RELEASE&type=test-type"), + assertEquals(createDefaultUrl("?type=test-type&bootVersion=1.2.0.RELEASE"), this.request.generateUrl(createDefaultMetadata())); } @@ -90,7 +91,7 @@ public class ProjectGenerationRequestTests { @Test public void customJavaVersion() { this.request.setJavaVersion("1.8"); - assertEquals(createDefaultUrl("?javaVersion=1.8&type=test-type"), + assertEquals(createDefaultUrl("?type=test-type&javaVersion=1.8"), this.request.generateUrl(createDefaultMetadata())); } @@ -115,20 +116,20 @@ public class ProjectGenerationRequestTests { @Test public void customLanguage() { - this.request.setLanguage("java"); - assertEquals(createDefaultUrl("?language=java&type=test-type"), + this.request.setLanguage("groovy"); + assertEquals(createDefaultUrl("?type=test-type&language=groovy"), this.request.generateUrl(createDefaultMetadata())); } @Test public void customProjectInfo() { - this.request.setGroupId("org.test"); - this.request.setArtifactId("demo"); - this.request.setVersion("0.0.1-SNAPSHOT"); - this.request.setDescription("Spring Boot Demo"); - assertEquals(createDefaultUrl("?artifactId=demo&description=Spring+Boot+Demo" - + "&groupId=org.test&type=test-type&version=0.0.1-SNAPSHOT"), - this.request.generateUrl(createDefaultMetadata())); + this.request.setGroupId("org.acme"); + this.request.setArtifactId("sample"); + this.request.setVersion("1.0.1-SNAPSHOT"); + this.request.setDescription("Spring Boot Test"); + assertEquals(createDefaultUrl("?groupId=org.acme&artifactId=sample&version=1.0.1-SNAPSHOT" + + "&description=Spring+Boot+Test&type=test-type"), + this.request.generateUrl(createDefaultMetadata())); } @Test