使用intellij idea打包并部署到外部的tomcat
1.使用intellij idea创建项目demotest
File -> New -> Project-> Spring Initializr,根据提示一步步操作
会生成一个带有 main() 方法的类 DemotestApplication,用于启动应用程序
2.新建class,HelloController
package com.example.demotest; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/hello") public String hello() { return "Hello Spring Boot!"; } }
3.运行
在DemotestApplication这个类中,然后右键点击运行
浏览器中输入http://localhost:8080/hello
4.打war包
(1)Run-> Edit Configurations
OK
(2)修改pom.xml
war包
<packaging>war</packaging>
添加一个spring-boot-maven-plugin打包插件,打包后的名字为packageTest
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <finalName>packageTest</finalName> </build>
运行mvn就可以生成jar包
输出下面提示,打包成功
在项目的target目录可以看到生成的war包
打开cmd,到war 包所在目录 运行命令
java -jar packageTest.war
浏览器输入http://localhost:8080/hello
输出hello信息
4.发布到外部的tomcat
把webtest.war放到tomcat下的webapp下,重启tomcat,
解决当前这个在外部tomcat没办法运行起来并访问的问题,设置启动配置
新建一个SpringBootStartApplication 继承自 SpringBootServletInitializer
说明:
A.在外部容器部署的话,不能依赖于Application的main函数
B.在启动类中继承SpringBootServletInitializer并实现configure方法,以类似于web.xml文件配置的方式来启动Spring应用上下文
C.新建的类与springboot的启动类是同级的
package com.example.demotest; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; public class SpringBootStartApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure( SpringApplicationBuilder application) { //DemotestApplication类为springboot 项目的启动类 return application.sources(DemotestApplication.class); } }
重新打包
把webtest.war放到tomcat下的webapp下,重启tomcat
发布成功
补充:
如果,设置启动配置后还是不行,需要让springboot内嵌的tomcat在运行时不起作用,修改pom.xml
因为SpringBoot默认的容器为Tomcat,依赖包在spring-boot-starter-web下
使用如下方式:
<!--web支持--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
或者
<!--web支持--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--声明spring boot内嵌tomcat的作用范围 在运行时不起作用--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
相关文章
- Tomcat 配置 项目 到tomcat目录外面 和 域名绑定访问(api接口、前端网站、后台管理网站)
- tomcat 启动日志乱码,idea中运行Tomcat也出现中文乱码:“淇℃伅”
- 【调试】Idea如何远程debug之tomcat war包启动
- Linux下Tomcat的启动、关闭、杀死进程
- java:eclipse4.4 安装tomcat插件
- idea给web项目添加tomcat
- Tomcat详解(二)——tomcat安装与配置
- Tomcat详解(一)——tomcat基础知识
- idea配置tomcat的方法
- IntelliJ IDEA部署tomcat时Edit Configuration无artifact选项
- excel导出大数据量时服务器cpu过高tomcat卡死问题排查
- WebClient UI和Tomcat的启动器
- 推荐一款功能强大的Tomcat 管理监控工具,可替代Tomcat Manager
- Could not load the Tomcat server configuration at /Servers/Tomcat v7.0 Server at localhost-config.
- 003-tomcat配置文件-server、tomcat-users
- 008-mac下idea -apache tomcat 测试授权问题
- Java-idea-运行tomcat 报內存溢出 PermGen space
- 17.[CVE-2017-12615]Tomcat任意文件上传漏洞
- 使用IntelliJ IDEA 2021.2 配置Tomcat
- tomcat Context容器(中):Tomcat如何隔离Web应用?
- Tomcat 多实例部署
- Tomcat 一文带你了解tomcat体系结构
- Tomcat 虚拟主机实现多站点与热部署