From a71d4182d8c7fb87cea60a1612c90411dbe7e496 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 1 Nov 2013 14:39:56 +0000 Subject: [PATCH] Add support for pom.xml generation as well as test in starter project --- app.groovy | 26 +++++++++++++++++++------- logback.xml | 2 +- templates/ApplicationTests.java | 17 +++++++++++++++++ templates/home.html | 4 ++-- templates/starter-pom.xml | 5 +++++ 5 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 templates/ApplicationTests.java diff --git a/app.groovy b/app.groovy index aa4d2baf..c55dba79 100644 --- a/app.groovy +++ b/app.groovy @@ -1,6 +1,6 @@ package app -@Grab("org.springframework.boot:spring-boot-starter-actuator:0.5.0.BUILD-SNAPSHOT") +@Grab("spring-boot-starter-actuator") @Grab("org.codehaus.groovy:groovy-ant:2.1.6") @Controller @@ -25,7 +25,7 @@ class MainController { model["styles"] << [name:"Security", value:"security"] model["styles"] << [name:"Batch", value:"batch"] model["styles"] << [name:"JPA", value:"data-jpa"] - model["types"] = [[name:"Maven POM", value:"pom", selected: false], [name:"Maven Project", value:"pomproject", selected: true]] + model["types"] = [[name:"Maven POM", value:"pom.xml", selected: false], [name:"Maven Project", value:"starter.zip", selected: true]] template "home.html", model } @@ -60,11 +60,12 @@ class MainController { File src = new File(new File(dir, "src/main/java"),request.packageName.replace(".", "/")) src.mkdirs() - - def body = template "Application.java", model - log.info("Creating: " + src + "/Application.java") - new File(src, "Application.java").write(body) + write(src, "Application.java", model) + File test = new File(new File(dir, "src/test/java"),request.packageName.replace(".", "/")) + test.mkdirs() + write(test, "ApplicationTests.java", model) + File download = new File(tmpdir, dir.name + ".zip") log.info("Creating: " + download) tempFiles << download @@ -80,6 +81,12 @@ class MainController { result } + def write(File src, String name, def model) { + def body = template name, model + log.info("Creating: " + src + "/" + name) + new File(src, name).write(body) + } + @RequestMapping("/pom") @ResponseBody ResponseEntity pom(PomRequest request, Map model) { @@ -87,6 +94,9 @@ class MainController { def style = request.style log.info("Styles requested: " + style) + def type = request.type + log.info("Type requested: " + type) + model.groupId = request.groupId model.artifactId = request.artifactId model.version = request.version @@ -100,6 +110,7 @@ class MainController { if (!style.class.isArray() && !(style instanceof Collection)) { style = [style] } + style = style.collect{ it=="jpa" ? "data-jpa" : it } model["styles"] = style.collect{ it=="" ? "" : "-" + it } log.info("Model: " + model) @@ -163,10 +174,11 @@ class PomRequest { def style = [] String name = "demo" + String type = "starter" String description = "Demo project for Spring Boot" String groupId = "org.test" String artifactId - String version = "0.0.1.SNAPSHOT" + String version = "0.0.1-SNAPSHOT" String packageName String getArtifactId() { artifactId == null ? name : artifactId diff --git a/logback.xml b/logback.xml index 3ac8b951..abe7cce7 100644 --- a/logback.xml +++ b/logback.xml @@ -3,7 +3,7 @@ - + diff --git a/templates/ApplicationTests.java b/templates/ApplicationTests.java new file mode 100644 index 00000000..57132396 --- /dev/null +++ b/templates/ApplicationTests.java @@ -0,0 +1,17 @@ +package ${packageName}; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.SpringApplicationContextLoader; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = Application.class, loader=SpringApplicationContextLoader.class) +public class ApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/templates/home.html b/templates/home.html index 0d2add84..09ad63f0 100644 --- a/templates/home.html +++ b/templates/home.html @@ -34,7 +34,7 @@

Spring Initializr

-
+ @@ -49,7 +49,7 @@ <% types.each { %> <% } %> diff --git a/templates/starter-pom.xml b/templates/starter-pom.xml index 3fda49fa..35d5a3ae 100644 --- a/templates/starter-pom.xml +++ b/templates/starter-pom.xml @@ -21,6 +21,11 @@ org.springframework.boot spring-boot-starter${it} <% } %> + + org.springframework.boot + spring-boot-starter-test + test +