Commit 0b8fd675 authored by Eddú Meléndez's avatar Eddú Meléndez Committed by Stephane Nicoll

Add extra attributes to the init command

Update the CLI init command to expose additional attributes supported
by Spring Initializr. These are: groupId, artifactId, version, name,
description and language.

Closes gh-2793 and gh-2907
parent d4dfa8d9
......@@ -37,6 +37,7 @@ import org.springframework.util.Assert;
* {@link Command} that initializes a project using Spring initializr.
*
* @author Stephane Nicoll
* @author Eddú Meléndez
* @since 1.2.0
*/
public class InitCommand extends OptionParsingCommand {
......@@ -96,6 +97,18 @@ public class InitCommand extends OptionParsingCommand {
private OptionSpec<Void> force;
private OptionSpec<String> language;
private OptionSpec<String> groupId;
private OptionSpec<String> artifactId;
private OptionSpec<String> name;
private OptionSpec<String> version;
private OptionSpec<String> description;
InitOptionHandler(InitializrService initializrService) {
this.serviceCapabilitiesReport = new ServiceCapabilitiesReportGenerator(
initializrService);
......@@ -140,6 +153,24 @@ public class InitCommand extends OptionParsingCommand {
"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();
}
private void otherOptions() {
......@@ -209,6 +240,24 @@ public class InitCommand extends OptionParsingCommand {
if (options.has(this.type)) {
request.setType(options.valueOf(this.type));
}
if(options.has(this.language)) {
request.setLanguage(options.valueOf(this.language));
}
if(options.has(this.groupId)) {
request.setGroupId(options.valueOf(this.groupId));
}
if (options.has(this.artifactId)) {
request.setArtifactId(options.valueOf(this.artifactId));
}
if(options.has(this.name)) {
request.setName(options.valueOf(this.name));
}
if(options.has(this.version)) {
request.setVersion(options.valueOf(this.version));
}
if(options.has(this.description)) {
request.setDescription(options.valueOf(this.description));
}
request.setExtract(options.has(this.extract));
if (nonOptionArguments.size() == 1) {
String output = (String) nonOptionArguments.get(0);
......
......@@ -59,6 +59,18 @@ class ProjectGenerationRequest {
private String type;
private String language;
private String groupId;
private String artifactId;
private String name;
private String version;
private String description;
/**
* The URL of the service to use.
* @see #DEFAULT_SERVICE_URL
......@@ -199,6 +211,72 @@ class ProjectGenerationRequest {
this.type = type;
}
/**
* The programming language to use or {@code null} if it should not be customized.
*/
public String getLanguage() {
return this.language;
}
public void setLanguage(String language) {
this.language = language;
}
/**
* 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.
*/
public String getVersion() {
return this.version;
}
public void setVersion(String version) {
this.version = version;
}
/**
*
*/
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
/**
* Generates the URI to use to generate a project represented by this request
* @param metadata the metadata that describes the service
......@@ -217,6 +295,9 @@ 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);
}
......@@ -225,15 +306,30 @@ class ProjectGenerationRequest {
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.language != null) {
builder.setParameter("language", this.language);
}
if (this.name != null) {
builder.setParameter("name", this.name);
}
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);
}
return builder.build();
}
......
......@@ -49,6 +49,7 @@ import static org.mockito.Mockito.verify;
* Tests for {@link InitCommand}
*
* @author Stephane Nicoll
* @author Eddú Meléndez
*/
public class InitCommandTests extends AbstractHttpClientMockTests {
......@@ -280,8 +281,9 @@ public class InitCommandTests extends AbstractHttpClientMockTests {
@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");
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<String> dependencies = this.handler.lastRequest.getDependencies();
assertEquals(2, dependencies.size());
......@@ -292,6 +294,12 @@ public class InitCommandTests extends AbstractHttpClientMockTests {
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
......
......@@ -37,6 +37,7 @@ import static org.junit.Assert.assertEquals;
* Tests for {@link ProjectGenerationRequest}
*
* @author Stephane Nicoll
* @author Eddú Meléndez
*/
public class ProjectGenerationRequestTests {
......@@ -112,6 +113,24 @@ public class ProjectGenerationRequestTests {
this.request.generateUrl(metadata));
}
@Test
public void customLanguage() {
this.request.setLanguage("java");
assertEquals(createDefaultUrl("?language=java&type=test-type"),
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()));
}
@Test
public void buildNoMatch() {
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