Gradle进阶:1: 结合spring boot进行web开发
在前面的文章中介绍了如何使用gradle的基础知识,这篇文章在某种程度上才是真正意义上的第一个hello world,这里使用一个简单的spring boot的例子,通过gradle进行编译和构建以及确认。
事前准备
环境准备
组件/框架 | 版本 |
---|---|
JDK | 1.8.0 |
Gradle | 4.10.2 |
Spring Boot | 2.1.1 |
liumiaocn:springboot liumiao$ gradle -v------------------------------------------------------------
Gradle 4.10.2
------------------------------------------------------------Build time: 2018-09-19 18:10:15 UTC
Revision: b4d8d5d170bb4ba516e88d7fe5647e2323d791ddKotlin DSL: 1.0-rc-6
Kotlin: 1.2.61
Groovy: 2.4.15
Ant: Apache Ant(TM) version 1.9.11 compiled on March 23 2018
JVM: 1.8.0_191 (Oracle Corporation 25.191-b12)
OS: Mac OS X 10.14 x86_64liumiaocn:springboot liumiao$
spring boot demo应用
目录结构
使用https://start.spring.io/或者自己手动创建如下结构的目录结构
liumiaocn:springboot liumiao$ tree
.
├── build.gradle
├── settings.gradle
└── src└── main├── java│ └── com│ └── liumiaocn│ └── springbootdemo│ └── SpringbootdemoApplication.java└── resources└── application.properties7 directories, 4 files
liumiaocn:springboot liumiao$
spring boot相关说明
详细请参看spring boot相关说明,本文不再赘述:
https://blog.csdn.net/liumiaocn/article/details/83548217
liumiaocn:springboot liumiao$ cat src/main/resources/application.properties
liumiaocn:springboot liumiao$ cat src/main/java/com/liumiaocn/springbootdemo/SpringbootdemoApplication.java
package com.liumiaocn.springbootdemo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;@RestController
@SpringBootApplication
public class SpringbootdemoApplication {@RequestMapping("/")String home() {return "Hello, Spring Boot 2";}public static void main(String[] args) {SpringApplication.run(SpringbootdemoApplication.class, args);}
}
liumiaocn:springboot liumiao$
gradle设定: settings.gradle
settings.gradle中仅设定rootProject.name
信息,用于作为缺省的jar文件的名称等用处。
liumiaocn:springboot liumiao$ cat settings.gradle
rootProject.name = 'springbootdemo'
liumiaocn:springboot liumiao$
gradle设定:build.gradle
build.gradle中做如下设定,简单说明如下:
- ext: 用于定义自定义属性,属性的多种使用方式在基础篇进行过详细介绍,这里不再赘述
- repositories:用于设定maven仓库,这里的例子使用了mavenCentral,如果使用其他的私库,可使用maven{ url ‘http://xxx’}的方式替换即可
- dependencies:比照spring boot的maven的写法,可看到这里spring-boot-gradle-plugin和spring-boot-starter-web的引入
- GAV:maven坐标,在gradle里可以通过group和version进行直接设定
- sourceCompatibility:用于设定java版本
- apply plugin:引入了java/org.springframework.boot/io.spring.dependency-management
liumiaocn:springboot liumiao$ cat build.gradle
buildscript {ext {springBootVersion = '2.1.1.RELEASE'}repositories {mavenCentral()}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")}
}apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'group = 'com.liumiaocn'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8repositories {mavenCentral()
}dependencies {implementation('org.springframework.boot:spring-boot-starter-web')
}
liumiaocn:springboot liumiao$
可以通过gradle tasks查看apply plugin所引入的task信息
liumiaocn:springboot liumiao$ gradle tasks> Task :tasks------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------Application tasks
-----------------
bootRun - Runs this project as a Spring Boot application.Build tasks
-----------
assemble - Assembles the outputs of this project.
bootJar - Assembles an executable jar archive containing the main classes and their dependencies.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'springbootdemo'.
components - Displays the components produced by root project 'springbootdemo'. [incubating]
dependencies - Displays all dependencies declared in root project 'springbootdemo'.
dependencyInsight - Displays the insight into a specific dependency in root project 'springbootdemo'.
dependencyManagement - Displays the dependency management declared in root project 'springbootdemo'.
dependentComponents - Displays the dependent components of components in root project 'springbootdemo'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'springbootdemo'. [incubating]
projects - Displays the sub-projects of root project 'springbootdemo'.
properties - Displays the properties of root project 'springbootdemo'.
tasks - Displays the tasks runnable from root project 'springbootdemo'.Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.Rules
-----
Pattern: clean<TaskName>: Cleans the output files of a task.
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.To see all tasks and more detail, run gradle tasks --allTo see more detail about a task, run gradle help --task <task>BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
liumiaocn:springboot liumiao$
构建
通过使用gradle build或者bootJar进行构建
liumiaocn:springboot liumiao$ ls
build.gradle settings.gradle src
liumiaocn:springboot liumiao$ gradle buildBUILD SUCCESSFUL in 1s
3 actionable tasks: 3 executed
liumiaocn:springboot liumiao$ ls
build build.gradle settings.gradle src
liumiaocn:springboot liumiao$
可以看到,gradle build生成了一个build的目录,此构建结果目录详细信息如下:
liumiaocn:springboot liumiao$ tree build
build
├── classes
│ └── java
│ └── main
│ └── com
│ └── liumiaocn
│ └── springbootdemo
│ └── SpringbootdemoApplication.class
├── libs
│ └── springbootdemo-0.0.1-SNAPSHOT.jar
├── resources
│ └── main
│ └── application.properties
└── tmp├── bootJar│ └── MANIFEST.MF└── compileJava12 directories, 4 files
liumiaocn:springboot liumiao$
结果确认
启动spring boot应用
使用java -jar 启动生成的springboot的jar文件,也可以使用插件封装的bootRun任务来执行,这里使用gradle bootRun来执行
liumiaocn:springboot liumiao$ gradle bootRun> Task :bootRun. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v2.1.1.RELEASE)2018-12-11 07:24:42.354 INFO 49488 --- [ main] c.l.s.SpringbootdemoApplication : Starting SpringbootdemoApplication on liumiaocn with PID 49488 (/Users/liumiao/gradle/springboot/build/classes/java/main started by liumiao in /Users/liumiao/gradle/springboot)
2018-12-11 07:24:42.362 INFO 49488 --- [ main] c.l.s.SpringbootdemoApplication : No active profile set, falling back to default profiles: default
2018-12-11 07:24:44.593 INFO 49488 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2018-12-11 07:24:44.649 INFO 49488 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-12-11 07:24:44.649 INFO 49488 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/9.0.13
2018-12-11 07:24:44.674 INFO 49488 --- [ main] 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: [/Users/liumiao/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2018-12-11 07:24:44.876 INFO 49488 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-12-11 07:24:44.876 INFO 49488 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2420 ms
2018-12-11 07:24:45.398 INFO 49488 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2018-12-11 07:24:45.869 INFO 49488 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-12-11 07:24:45.876 INFO 49488 --- [ main] c.l.s.SpringbootdemoApplication : Started SpringbootdemoApplication in 4.26 seconds (JVM running for 5.059)
<=========----> 75% EXECUTING [17s]
> :bootRun
确认结果
可以通过页面访问确认应用信息
也可以通过curl来确认
liumiaocn:~ liumiao$ curl http://localhost:8080/
Hello, Spring Boot 2liumiaocn:~ liumiao$
其他章节
- https://blog.csdn.net/liumiaocn/article/details/84341611
总结
通过使用spring boot和java的相关插件,可以很容易地使用gradle来进行spring boot的web项目的构建,在下篇文章中将结合Junit的实施方式进行示例说明。
Gradle进阶:1: 结合spring boot进行web开发相关推荐
- Spring Boot之Web开发
Web开发 简单了解 快速上手 JSON 的支持 RESTful 的支持 支持热部署 快速上手 拦截器与过滤器 自定义过滤器 示例 自定义拦截器 示例 打包部署 多环境配置 服务器配置 项目打包 简单 ...
- Spring Boot(5) web开发(3)拦截器、文件上传、异常处理
Spring Boot(5) web开发(3)拦截器.文件上传.异常处理 学习视频: https://www.bilibili.com/video/BV19K4y1L7MT?p=49&spm_ ...
- Spring Boot与web开发
Spring Boot与web开发 1. 2. 3. 4. 5. 6. 7. 8. 9. 0. 四.Web开发 1.简介 使用SpringBoot: 1).创建SpringBoot应用,选中我们需要的 ...
- 【spring boot】第3篇:spring boot 进行 web 开发
spring boot 对 web 静态资源的管理 经过前两篇文章我们知道spring boot最大的功能就是自动配置.主要是通过以下两个类实现该功能: xxxxAutoConfiguration : ...
- spring boot 整合web开发之文件上传、静态资源访问、异常处理、返回JSON数据
目录 springboot 整合web开发 返回json数据 静态资源访问 文件上传 全局异常 1.返回json数据 springboot默认的是jackson-databind做为json处理器.也 ...
- Spring Boot与Web开发简介||SpringBoot对静态资源的映射规则
Web开发 1.简介 使用SpringBoot: 1).创建SpringBoot应用,选中我们需要的模块: 2).SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可以运 ...
- SpringBoot资料合集-04【Spring Boot与Web开发】
1.SpringMVC快速使用 1.基于restful http接口 的CURD 2.调用rest http接口 3.通过postman调用 4.通过swagger调用 2.SpringMVC自动配置 ...
- Spring boot的Web开发
开发步骤 第一步,创建SpringBoot应用,选中我们需要的模块 第二步,SpringBoot已经默认,将这些场景配置好了 只需要在配置文件中,指定少量配置就可以运行起来 第三步,自己编写业务代码 ...
- spring boot 整合web开发(二)
目录 自定义错误页 CORS支持(前端跨域请求) 拦截器HandlerInterceptor 启动系统任务 springboot配置AOP 整合servlet.filter.listener 下图为本 ...
最新文章
- Python 标准库之 time
- 安装eAccelerator
- C++结构体,联合体
- NOIP2018初赛提高组复习提纲(By HGOI LJC)
- python的span方法_Python Span.set_extension方法代码示例
- VTK:Points之UnsignedDistance
- VC++实现获取DNS服务器
- tomcat 系统访问日志配置
- 后台getshell常用技巧总结
- 201521123050 《Java程序设计》第13周学习总结
- HTML页面用ajax方式传递表单数据给后台,数据库插入信息后将json对象传回前端JS处理(前后台交互)
- 如何用100美元和TensorFlow来造一个能“看”东西的机器人
- A5如何备份oracle数据库,oracle的数据库的导入导出
- mtk开机声音与关机声音修改(原创)
- 联想重装系统去掉保护_经验:联想硬盘保护系统EDU 7.0清除日志
- 如何配置服务器的安全?
- 计算机系统中的数据计量单位-位(bit)、字节(Byte)、字(word)
- 画一个椭圆形带扩散效果波纹展示效果
- 前端,移动端开发框架
- 【ybt高效进阶1-5-2】【luogu P3456】山峰和山谷 / GRZ-Ridges and Valleys