个人随笔
SpringBoot2应用发布到tomcat容器运行
2020-09-11 22:39:50

我们运行springboot2的项目,都是打包成一个jar包然后用自带的tomcat启动,简单明了,但是如果要修改jar包中的一些页面就得重新打包,所以有时候我们想要将springboot2的应用发布到tomcat容器中,其实很简单,步骤如下:

1、pom.xml中把项目类型改为war

  1. <modelVersion>4.0.0</modelVersion>
  2. <groupId>com.suibibk</groupId>
  3. <artifactId>testtomcat</artifactId>
  4. <version>0.0.1-SNAPSHOT</version>
  5. <packaging>war</packaging>

2、排除自带的tomcat

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. <exclusions>
  5. <exclusion>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-tomcat</artifactId>
  8. </exclusion>
  9. </exclusions>
  10. </dependency>

3、pom.xml中加入tomcat好本地调试

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-tomcat</artifactId>
  4. <scope>provided</scope><!-- 必须要加,不然报错 -->
  5. </dependency>
  6. <dependency>
  7. <groupId>javax.servlet</groupId>
  8. <artifactId>javax.servlet-api</artifactId>
  9. <scope>provided</scope>
  10. </dependency>

provided表示只在本地调试起作用

4、修改启动类

  1. @SpringBootApplication
  2. public class App extends SpringBootServletInitializer {
  3. @Override
  4. protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
  5. return builder.sources(App.class);
  6. }
  7. public static void main(String[] args) {
  8. SpringApplication.run(App.class, args);
  9. }
  10. }

改为继承SpringBootServletInitializer

5、修改tomcat的server.xml指定端口

  1. <Service name="Catalina">
  2. <Connector port="8082" protocol="HTTP/1.1" maxThreads="700" acceptCount="100" connectionTimeout="2000" redirectPort="8445" URIEncoding="UTF-8"/>
  3. <Engine name="Catalina" defaultHost="localhost">
  4. <Host name="localhost" appBase="webapps">
  5. <Context docBase="testtomcat" path="" reloadable="true" />
  6. </Host>
  7. </Engine>
  8. </Service>

6、完成,下面提供完整的pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.suibibk</groupId>
  4. <artifactId>testtomcat</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. <packaging>war</packaging>
  7. <parent>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-parent</artifactId>
  10. <version>2.0.5.RELEASE</version>
  11. </parent>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-web</artifactId>
  16. <exclusions>
  17. <exclusion>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-tomcat</artifactId>
  20. </exclusion>
  21. </exclusions>
  22. </dependency>
  23. <!-- jsp的话还需要tomcat的支持 ,自带的tomcat不支持-->
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-tomcat</artifactId>
  27. <scope>provided</scope><!-- 必须要加,不然报错 -->
  28. </dependency>
  29. <dependency>
  30. <groupId>javax.servlet</groupId>
  31. <artifactId>javax.servlet-api</artifactId>
  32. <scope>provided</scope>
  33. </dependency>
  34. </dependencies>
  35. <build>
  36. <finalName>testtomcat</finalName>
  37. <plugins>
  38. <!-- 可以将项目打包成可执行jar包 -->
  39. <plugin>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-maven-plugin</artifactId>
  42. </plugin>
  43. </plugins>
  44. </build>
  45. </project>
 3
有疑问发邮件到 : suibibk@qq.com 侵权立删
Copyright : 个人随笔   备案号 : 粤ICP备18099399号-2