When creating a new Spring Boot app, I think that many people create development projects by one of the following methods.
You can search for Specifiable Dependent Artifs using the Web UI or IDE wizard, and you can easily create a development project simply by specifying a value in the input form and clicking. This is very convenient and I love it, but sometimes I find the UI operation itself to be a hassle.
In such a case, it's CLI.
Spring Boot also supports the method of creating a development project using the commands provided by Spring Boot, but this time I will show you how to use the curl
and tar
commands.
You can create a development project for a stand-alone application (= non-WEB) by executing the following command.
$ curl -s https://start.spring.io/starter.tgz -d baseDir=demo | tar -xzvf -
x demo/mvnw
x demo/
x demo/.mvn/
x demo/.mvn/wrapper/
x demo/src/
x demo/src/main/
x demo/src/main/java/
x demo/src/main/java/com/
x demo/src/main/java/com/example/
x demo/src/main/resources/
x demo/src/test/
x demo/src/test/java/
x demo/src/test/java/com/
x demo/src/test/java/com/example/
x demo/.gitignore
x demo/.mvn/wrapper/maven-wrapper.jar
x demo/.mvn/wrapper/maven-wrapper.properties
x demo/mvnw.cmd
x demo/pom.xml
x demo/src/main/java/com/example/DemoApplication.java
x demo/src/main/resources/application.properties
x demo/src/test/java/com/example/DemoApplicationTests.java
Directory structure
$ tree demo
demo
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── example
│ │ └── DemoApplication.java
│ └── resources
│ └── application.properties
└── test
└── java
└── com
└── example
└── DemoApplicationTests.java
Try to build.
$ cd demo
$ ./mvnw clean package
Try to run it.
$ java -jar target/demo-0.0.1-SNAPSHOT.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.3.RELEASE)
2017-01-03 15:38:28.749 INFO 31974 --- [ main] com.example.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT on Kazuki-no-MacBook-Pro.local with PID 31974 (/usr/local/apps/demo/target/demo-0.0.1-SNAPSHOT.jar started by shimizukazuki in /usr/local/apps/demo)
2017-01-03 15:38:28.751 INFO 31974 --- [ main] com.example.DemoApplication : No active profile set, falling back to default profiles: default
2017-01-03 15:38:28.794 INFO 31974 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5b37e0d2: startup date [Tue Jan 03 15:38:28 JST 2017]; root of context hierarchy
2017-01-03 15:38:29.312 INFO 31974 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-01-03 15:38:29.323 INFO 31974 --- [ main] com.example.DemoApplication : Started DemoApplication in 0.843 seconds (JVM running for 1.151)
2017-01-03 15:38:29.324 INFO 31974 --- [ Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@5b37e0d2: startup date [Tue Jan 03 15:38:28 JST 2017]; root of context hierarchy
2017-01-03 15:38:29.325 INFO 31974 --- [ Thread-1] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
:clap: :clap: :clap:
You can create a development project for a web application by executing the following command (specify the web for dependencies).
$ curl -s https://start.spring.io/starter.tgz -d name=web-demo -d artifactId=web-demo -d dependencies=web -d baseDir=web-demo | tar -xzvf -
x web-demo/mvnw
x web-demo/
x web-demo/.mvn/
x web-demo/.mvn/wrapper/
x web-demo/src/
x web-demo/src/main/
x web-demo/src/main/java/
x web-demo/src/main/java/com/
x web-demo/src/main/java/com/example/
x web-demo/src/main/resources/
x web-demo/src/main/resources/static/
x web-demo/src/main/resources/templates/
x web-demo/src/test/
x web-demo/src/test/java/
x web-demo/src/test/java/com/
x web-demo/src/test/java/com/example/
x web-demo/.gitignore
x web-demo/.mvn/wrapper/maven-wrapper.jar
x web-demo/.mvn/wrapper/maven-wrapper.properties
x web-demo/mvnw.cmd
x web-demo/pom.xml
x web-demo/src/main/java/com/example/WebDemoApplication.java
x web-demo/src/main/resources/application.properties
x web-demo/src/test/java/com/example/WebDemoApplicationTests.java
Directory structure
$ tree web-demo
web-demo
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── example
│ │ └── WebDemoApplication.java
│ └── resources
│ ├── application.properties
│ ├── static
│ └── templates
└── test
└── java
└── com
└── example
└── WebDemoApplicationTests.java
Try to build.
$ cd web-demo
$ ./mvnw clean package
Try to run it.
$ java -jar target/web-demo-0.0.1-SNAPSHOT.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.3.RELEASE)
2017-01-03 15:48:49.606 INFO 32190 --- [ main] com.example.WebDemoApplication : Starting WebDemoApplication v0.0.1-SNAPSHOT on Kazuki-no-MacBook-Pro.local with PID 32190 (/usr/local/apps/web-demo/target/web-demo-0.0.1-SNAPSHOT.jar started by shimizukazuki in /usr/local/apps/web-demo)
2017-01-03 15:48:49.608 INFO 32190 --- [ main] com.example.WebDemoApplication : No active profile set, falling back to default profiles: default
2017-01-03 15:48:49.658 INFO 32190 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4bf558aa: startup date [Tue Jan 03 15:48:49 JST 2017]; root of context hierarchy
2017-01-03 15:48:50.658 INFO 32190 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-01-03 15:48:50.670 INFO 32190 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-01-03 15:48:50.671 INFO 32190 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.6
2017-01-03 15:48:50.744 INFO 32190 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-01-03 15:48:50.744 INFO 32190 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1089 ms
2017-01-03 15:48:50.855 INFO 32190 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-01-03 15:48:50.858 INFO 32190 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-01-03 15:48:50.858 INFO 32190 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-01-03 15:48:50.858 INFO 32190 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-01-03 15:48:50.858 INFO 32190 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-01-03 15:48:51.071 INFO 32190 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4bf558aa: startup date [Tue Jan 03 15:48:49 JST 2017]; root of context hierarchy
2017-01-03 15:48:51.123 INFO 32190 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-01-03 15:48:51.124 INFO 32190 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-01-03 15:48:51.147 INFO 32190 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-01-03 15:48:51.147 INFO 32190 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-01-03 15:48:51.173 INFO 32190 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-01-03 15:48:51.282 INFO 32190 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-01-03 15:48:51.335 INFO 32190 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-01-03 15:48:51.338 INFO 32190 --- [ main] com.example.WebDemoApplication : Started WebDemoApplication in 2.123 seconds (JVM running for 2.45)
Try to access the started Tomcat.
$ curl http://localhost:8080/
{"timestamp":1483426201099,"status":404,"error":"Not Found","message":"No message available","path":"/"}
I haven't created an endpoint, so it's correct to get a 404 Not Found error.
:clap: :clap: :clap:
Read the Spring Initializr GitHub README.
If you have decided what to make in advance (for example, in the case of the procedure when setting up the development environment) ... Develop with one liner (= copy command) using CLI instead of GUI It is efficient to follow the procedure for creating a project.
Recommended Posts