Spring Boot-Spring Tool Suit + Gradle 构建第一个Spring Boot 项目01
2018-03-07 15:37
603 查看
系列博文介绍
使用Spring Tool Suite构建Spring Boot项目
下载STS
插件安装
搭建第一个Spring Boot项目
启动项目
我们将会使用 Spring Boot
的自动配置功能来构建应用,这样的话,就能完全避免传统开发Spring工程使用样板式的配置文件。
同时,使用 Gradle 和 Java 8 .
同时将代码托管到github上,所以也会接触常用的Git 命令。
最后将借助 Heroku 部署应用,所以建议从一开始就使用 Git 对代码进行版本
通常,构建一个Spring Boot项目有如下方式
使用IDE (比如Spring Tool Suite, IntelliJ IDEA )生成 Starter 代码;
借助 Spring 站点,从 http://start.Spring.io 上下载可配置的 ZIP 文件;
使用到 http://start.Spring.io 站点的 curl 命令来达到相同的效果
方便起见,我们直接使用IDE开发Spring Boot 工程即可。
在 Dashboard 中,可以看到“Manage IDE Extensions”按钮,然后,需要在“Language and framework tooling”区域中选择下载“Gradle Support”。
Spring已经不推荐使用该插件管理Gradle项目了,推荐使用BuildShip,暂未找到如何安装,先使用该插件。
同时推荐下载“Groovy Eclipse”以及“Groovy 2.4 compiler”,后面使用 geb 构建验收测试时会用到它们
安装后,重启STS即可生效。
如果你关闭了Dashboard页面,可以通过Help | Dashboard 进入Dashboard 页面。
这是一个 Gradle 应用,会生成 JAR文件并使用 Java 8。
为 Web 应用生成一个 JAR 文件?将 Web 应用打包为JAR 文件,这一点你们可能会觉得有些诡异。尽管仍然可以将其打包为 WAR,但这并不是推荐的实践
在默认情况下,Spring Boot 将会创建一个胖 JAR 包(fat JAR),这个 JAR 包中包含了应用所有的依赖,提供了通过“java-jar”命令便捷启动 Web 应用的方法。 我们的应用将会打包为JAR文件,如果你想创建WAR文件 的话,可以参考http://spring.io/guides/gs/convert-jar-to-war/。
选择正确的信息后,Next,因为我们构建Spring MVC Web项目,因此只需要选择Web即可
在配置窗口的底部会有一些复选框,代表各种 Spring Boot starter 库。它们是可以添加到构建文件中的依赖项,针对各种 Spring 项目,它们提供了自动配置功能。
现在只关心 Spring MVC,所以只选中 Web 这个复选框。
等待几分钟后,控制台输出如下信息说明成功。
成功后我们将会得到如下的工程结构
主类MasterSpringMvcApplication 及其测试类 MasterSpringMvcApplicationTests
两个空的文件夹 — static 和templates,它们分别用来存放静态Web 资源(图片、样式文件等)和模板(jsp、freemarker 或Thymeleaf)。
application.properties,它是 Spring Boot 默认的配置文件,默认是空的
构建文件 build.gradle
运行 MasterSpringMvcApplication#main方法 Run As Spring Boot App
启动日志
此时,tomcat容器已经启动成功,访问 http://localhost:8080,此时会产生一个错误,不必担心。
使用Spring Tool Suite构建Spring Boot项目
下载STS
插件安装
搭建第一个Spring Boot项目
启动项目
系列博文介绍
在本系列博文中我们将会使用 Spring Boot
的自动配置功能来构建应用,这样的话,就能完全避免传统开发Spring工程使用样板式的配置文件。
同时,使用 Gradle 和 Java 8 .
同时将代码托管到github上,所以也会接触常用的Git 命令。
最后将借助 Heroku 部署应用,所以建议从一开始就使用 Git 对代码进行版本
通常,构建一个Spring Boot项目有如下方式
使用IDE (比如Spring Tool Suite, IntelliJ IDEA )生成 Starter 代码;
借助 Spring 站点,从 http://start.Spring.io 上下载可配置的 ZIP 文件;
使用到 http://start.Spring.io 站点的 curl 命令来达到相同的效果
方便起见,我们直接使用IDE开发Spring Boot 工程即可。
使用Spring Tool Suite构建Spring Boot项目
Spring Tool Suite(STS)是一个自定义版本的 Eclipse,它被用来与各种 Spring 项目进行协作,它同时还包括 Groovy 和 Gradle 功能,它通过“Getting Started”项目,能够让你快速地了解 Spring 广阔的生态系统。下载STS
访问 https://Spring.io/tools/sts/all,并下载 STS 的最新发布版插件安装
构建第一个 Spring Boot 项目之前,首先需要安装 Gradle 对 STS 的支持。在 Dashboard 中,可以看到“Manage IDE Extensions”按钮,然后,需要在“Language and framework tooling”区域中选择下载“Gradle Support”。
Spring已经不推荐使用该插件管理Gradle项目了,推荐使用BuildShip,暂未找到如何安装,先使用该插件。
同时推荐下载“Groovy Eclipse”以及“Groovy 2.4 compiler”,后面使用 geb 构建验收测试时会用到它们
安装后,重启STS即可生效。
如果你关闭了Dashboard页面,可以通过Help | Dashboard 进入Dashboard 页面。
搭建第一个Spring Boot项目
使用“File | New | Spring Starter Project”导航菜单,这里的可选项是与 http://start.Spring.io 相同的,只不过嵌入到了 IDE 中。这是一个 Gradle 应用,会生成 JAR文件并使用 Java 8。
为 Web 应用生成一个 JAR 文件?将 Web 应用打包为JAR 文件,这一点你们可能会觉得有些诡异。尽管仍然可以将其打包为 WAR,但这并不是推荐的实践
在默认情况下,Spring Boot 将会创建一个胖 JAR 包(fat JAR),这个 JAR 包中包含了应用所有的依赖,提供了通过“java-jar”命令便捷启动 Web 应用的方法。 我们的应用将会打包为JAR文件,如果你想创建WAR文件 的话,可以参考http://spring.io/guides/gs/convert-jar-to-war/。
选择正确的信息后,Next,因为我们构建Spring MVC Web项目,因此只需要选择Web即可
在配置窗口的底部会有一些复选框,代表各种 Spring Boot starter 库。它们是可以添加到构建文件中的依赖项,针对各种 Spring 项目,它们提供了自动配置功能。
现在只关心 Spring MVC,所以只选中 Web 这个复选框。
等待几分钟后,控制台输出如下信息说明成功。
[sts] ----------------------------------------------------- [sts] Starting Gradle build for the following tasks: [sts] :cleanEclipse [sts] :eclipse [sts] ----------------------------------------------------- Support for clients using a tooling API version older than 3.0 was deprecated and will be removed in Gradle 5.0. You are currently using tooling API version 2.2.1. You should upgrade your tooling API client to version 3.0 or later. :cleanEclipseClasspath UP-TO-DATE :cleanEclipseJdt UP-TO-DATE :cleanEclipseProject UP-TO-DATE :cleanEclipse UP-TO-DATE :eclipseClasspath :eclipseJdt :eclipseProject :eclipse BUILD SUCCESSFUL in 2s 6 actionable tasks: 3 executed, 3 up-to-date [sts] ----------------------------------------------------- [sts] Build finished succesfully! [sts] Time taken: 0 min, 2 sec [sts] -----------------------------------------------------
成功后我们将会得到如下的工程结构
主类MasterSpringMvcApplication 及其测试类 MasterSpringMvcApplicationTests
两个空的文件夹 — static 和templates,它们分别用来存放静态Web 资源(图片、样式文件等)和模板(jsp、freemarker 或Thymeleaf)。
application.properties,它是 Spring Boot 默认的配置文件,默认是空的
构建文件 build.gradle
启动项目
到这里,我们就可以运行应用的主方法,这样就会启动一个 Web 服务器运行 MasterSpringMvcApplication#main方法 Run As Spring Boot App
启动日志
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.0.RELEASE) 2018-03-07 16:14:01.863 INFO 21784 --- [ main] m.MasterSpringMvcApplication : Starting MasterSpringMvcApplication on Mr with PID 21784 (D:\workspace\workspace-sts-3.9.2\masterSpringMvc\bin\main started by Mr.Yang in D:\workspace\workspace-sts-3.9.2\masterSpringMvc) 2018-03-07 16:14:01.868 INFO 21784 --- [ main] m.MasterSpringMvcApplication : No active profile set, falling back to default profiles: default 2018-03-07 16:14:01.996 INFO 21784 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2cbb3d47: startup date [Wed Mar 07 16:14:01 CST 2018]; root of context hierarchy 2018-03-07 16:14:03.704 INFO 21784 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2018-03-07 16:14:03.738 INFO 21784 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2018-03-07 16:14:03.738 INFO 21784 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.28 2018-03-07 16:14:03.745 INFO 21784 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jre1.8.0_161\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_161/bin/server;C:/Program Files/Java/jre1.8.0_161/bin;C:/Program Files/Java/jre1.8.0_161/lib/amd64;C:\ProgramData\Oracle\Java\javapath;E:\Program Files\Java\jdk1.7.0_51\bin;E:\Program Files\Java\jdk1.7.0_51\jre\bin;C:\windows\system32\;E:\Program Files\Python27;E:\Program Files\Git\cmd;E:\libs\apache-maven-3.5.0-bin\apache-maven-3.5.0\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Users\Mr.Yang\AppData\Local\Microsoft\WindowsApps;C:\Users\Mr.Yang\AppData\Local\GitHubDesktop\bin;%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;;E:\Program Files\spring-tool-suite-3.9.2.RELEASE\sts-3.9.2.RELEASE;;.] 2018-03-07 16:14:03.928 INFO 21784 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2018-03-07 16:14:03.929 INFO 21784 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1938 ms 2018-03-07 16:14:04.227 INFO 21784 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/] 2018-03-07 16:14:04.235 INFO 21784 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2018-03-07 16:14:04.236 INFO 21784 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2018-03-07 16:14:04.236 INFO 21784 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2018-03-07 16:14:04.236 INFO 21784 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2018-03-07 16:14:04.811 INFO 21784 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2cbb3d47: startup date [Wed Mar 07 16:14:01 CST 2018]; root of context hierarchy 2018-03-07 16:14:04.891 INFO 21784 --- [ 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.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2018-03-07 16:14:04.894 INFO 21784 --- [ 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.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2018-03-07 16:14:04.952 INFO 21784 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-03-07 16:14:04.952 INFO 21784 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-03-07 16:14:05.035 INFO 21784 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-03-07 16:14:05.344 INFO 21784 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2018-03-07 16:14:05.405 INFO 21784 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2018-03-07 16:14:05.412 INFO 21784 --- [ main] m.MasterSpringMvcApplication : Started MasterSpringMvcApplication in 4.06 seconds (JVM running for 5.889)
此时,tomcat容器已经启动成功,访问 http://localhost:8080,此时会产生一个错误,不必担心。
相关文章推荐
- Spring Boot-Spring Tool Suit + Gradle 构建第一个Spring Boot 项目02
- 快速创建一个基于Gradle构建的SpringBoot Web项目(SpringBoot-01)
- 构建第一个Spring Boot项目
- SpringBoot学习教程 - 01 - 第一个SpringBoot项目
- SpringBoot(一)——构建第一个SpringBoot项目
- SpringBoot指南|第一篇:构建第一个SpringBoot项目
- 01:搭建第一个springboot项目
- Spring精藏(一)第一个SpringBoot项目构建及eclipse安装Springboot
- Spring Boot 项目构建 之 使用 Gradle 构建 Java 项目(Building Java Projects with Gradle)
- 使用Gradle构建SpringBoot工程系列:第三篇 创建Spring Boot项目
- 第一个SpringBoot web 应用使用maven构建Docker镜像并容器化运行
- 开启第一个spring boot项目
- IntelliJ IDEA创建第一个Spring Boot项目
- spring boot项目构建出错,编译失败提示少包解决办法
- 【springBoot】之快速构建一个web项目
- 【Spring boot】第一个项目 Springboot + mysql + hibernate
- 构建微服务:快速搭建Spring Boot项目
- spring-boot实战【01】:Hello World项目搭建
- Springboot整合dubbo构建maven多模块项目(四) - 集成mybatis
- 构建 Zookeeper + Dubbo + Spring Boot 的分布式调用项目