Spring boot项目打包成war部署到tomcat总结

Spring boot项目打包成war部署到tomcat总结

因项目要在tomcat上部署,所以需要发布成war包,要发布时遇到很多坑,现总结如下:
一、pom.xml文件配置需要修改5点。示例
<?xml version=”1.0″ encoding=”UTF-8″?>
<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”>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!– lookup parent from repository –>
</parent>
<groupId>cn.dalink.ooask</groupId>
<artifactId>boottest2</artifactId>
<version>3.0</version>
<packaging>war</packaging><!– ①设置成war –>
<name>boottest2</name>
<description>survey data manage</description>

<properties><!– 设置属性编码与版本–>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions><!– ②Tomcat依赖移除 –>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency><!– ③设置Tomcat依赖,一定要加上 –>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope><!– ④一定要设置成provided –>
</dependency>

<!–jsp页面使用jstl标签–>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>

<!–用于编译jsp–><!– Jasper是tomcat中使用的JSP引擎,运用tomcat-embed-jasper可以将项目与tomcat分开 –>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<finalName>ROOT</finalName><!–⑤设置打包时包的名称,设置名称方便部署–>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

二、启动类由jar的一个改成2个。都是套路,几乎不用管它。
示例:
@SpringBootApplication
public class Boottest2Application {

public static void main(String[] args) {
SpringApplication.run(Boottest2Application.class, args);
}
}

public class ServletInitializer extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Boottest2Application.class);
}
}
——–
或改成一个文件:
/*以下代码为SpringBoot war包部署到Tomcat服务器时启用*/
@SpringBootApplication
public class SurveyDmApp extends SpringBootServletInitializer{

public static void main(String[] args) {
SpringApplication.run(SurveyDmApp.class, args);
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SurveyDmApp.class);
}
}
—-
发布:Run As > maven clean(清除已打包的其他文件) > maven install(打包)
然后在target目录下便可以看到设置了<finalName>ROOT</finalName>的war包,
之后发布即可。

四、部署

把target目录下的war包放到tomcat的webapps目录下,启动tomcat,即可自动解压部署:(tomcat/bi目录下,双击startup.bat即可启动tomcat,成功启动,自动解压部署运行)

五、最后

最后在浏览器中输入
http://localhost:[端口号]/[打项目名]/

注意:端口号以tomcat配置时指定的为准,不是springboot中配置的了,项目名称一定要加,即war包的名字,然后才能访问你之前的请求路径

参考:
Spring boot如何打war包发布到tomcat http://blog.51cto.com/lxqybyq/2106975
Spring boot之打war包并把配置文件打在包外 https://jingyan.baidu.com/article/b907e62768ec7046e7891cc9.html
通过eclipse把spring boot项目打包成war包并部署到tomcat服务器上的步骤
https://blog.csdn.net/huangyaa729/article/details/78031337

人已赞赏
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索