基于Maven构建SpringBoot项目并发布到外部tomcat
一、基于Maven构建SpringBoot项目
1、springboot版本
官网最新版本:2.6.1(https://spring.io/)
当前使用版本:2.1.1
2、构建方式
(1)方式一:通过官网https://spring.io/进行springboot项目构建,然后在本地通过IDEA导入打开即可
(2)方式二:本地IDEA构建配置
a.新建maven项目
选择JDK,确认包名项目名,选择maven仓库等
b.在pom.xml进行依赖配置
1 pom.xml配置参考: 2 3 <parent> 4 <groupId>org.springframework.bootgroupId> 5 <artifactId>spring-boot-starter-parentartifactId> 6 <version>2.1.1.RELEASEversion> 7 <relativePath/> 8 parent> 9 10 <groupId>org.lkwgroupId> 11 <artifactId>SpringBootTestartifactId> 12 <version>1.0-SNAPSHOTversion> 13 <name>SpringBootTestname> 14 <description>Demo project for Spring Bootdescription> 15 16 17 <properties> 18 <java.version>1.8java.version> 19 properties> 20 21 <dependencies> 22 23 <dependency> 24 <groupId>org.springframework.bootgroupId> 25 <artifactId>spring-boot-starter-webartifactId> 26 dependency> 27 28 <dependency> 29 <groupId>org.springframework.bootgroupId> 30 <artifactId>spring-boot-starter-testartifactId> 31 <scope>testscope> 32 dependency> 33 37 <dependency> 38 <groupId>org.springframework.bootgroupId> 39 <artifactId>spring-boot-devtoolsartifactId> 40 <optional>trueoptional> 41 dependency> 42 dependencies> 43 44 <build> 45 <plugins> 46 47 <plugin> 48 <groupId>org.springframework.bootgroupId> 49 <artifactId>spring-boot-maven-pluginartifactId> 50 plugin> 51 plugins> 52 build>
c.创建SpringBoot项目启动类
1 //SpringBoot启动类 2 import org.springframework.boot.SpringApplication; 3 import org.springframework.boot.autoconfigure.SpringBootApplication; 4 5 6 @SpringBootApplication //springboot的全局的自动配置注解 7 public class Application { 8 9 public static void main(String[] args) { 10 // 固定的代码 启动springboot程序 初始化spring容器 11 SpringApplication.run(Application.class, args); 12 } 13 14 }
d.创建控制器类进行测试
//控制类测试 @RestController public class HelloController { @RequestMapping("/hello/getHello") public String getHello(){ System.out.println("receive one request=>/hello/getHello"); return "Hello SpringBoot!"; } }
e.运行项目,在浏览器输入ip:8080/hello/getHello(默认端口)进行测试即可
二、把项目打包并发布到外部tomcat上
1、打包配置过程
//(1)排除springboot自带的tomcat插件org.springframework.boot spring-boot-starter-web //(2)引入依赖javax.servlet-api org.springframework.boot spring-boot-starter-tomcat provided //(3)更改项目名和打包方式 javax.servlet javax.servlet-api 3.1.0 provided org.lkw SpringBootTest 1.0-SNAPSHOT war SpringBootTest Demo project for Spring Boot //(4)创建SpringBootStartApplicatin类,SpringBootServletInitializer //该类相当于spring里的web.xml,用于对serlvet、过滤器filter和监听器listener进行初始化 public class SpringBootStartApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { //一定要指向springboot启动类,即@SpringBootApplication标记的类 return builder.sources(Application.class); } } SpringBootTest org.springframework.boot spring-boot-maven-plugin
2、tomcat发布
(1)方式一:把war包置入webapps目录里
直接把war包丢到webapps目录下,tomcat会对其自动解压,其相关编译文件会出现在work文件中,访问路径为ip:端口/项目名(执行优先级:第三)
(2)方式二:通过配置conf/servlet.xml,增加
1 <Host name="localhost" appBase="webapps" 2 unpackWARs="true" autoDeploy="true"> 3 4 6 9 #此<Context>为新增配置,path为指定外部访问项目名,docBase为项目实际路径,reloadable为true时当项目文件变动不需重启 10 <Context path="/B" docBase="/opt/bigdata/java/project/SpringBootTest" debug="0" reloadable="true"/> 11 12 15 <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" 16 prefix="localhost_access_log" suffix=".txt" 17 pattern="%h %l %u %t "%r" %s %b" /> 18 19 Host>
(3)方式三:在conf/Catalina/localhost下新增xxx.xml文件,在里面添加即可,不用重启,访问路径为ip:端口/xxx,如果xml命名为ROOT.xml则访问路径为ip:端口/(不需要带项目名)(执行优先级:第二)
#xxx.xml <?xml version="1.0" encoding="UTF-8"?> <Context docBase="/opt/bigdata/java/project/SpringBootTest" reloadable="true" />
3、FAQ(问题集锦)
答:server.xml为不可重载资源,当tomcat服务已经启动的时候,则需要重启