Spring Boot项目部署到外部Tomcat服务器
linux-dash
A beautiful web dashboard for Linux
项目地址:https://gitcode.com/gh_mirrors/li/linux-dash
·
前言
Spring Boot项目一般都是内嵌tomcat或者jetty服务器运行,很少用war包部署到外部的服务容器,即使放到linux中,一般也是直接启动Application类,但是有些时候我们需要部署到外部的服务器,这对于Spring Boot来说却有点麻烦
部署步骤
一、 修改pom
- 首先把package改为war
<packaging>war</packaging>
- 由于spring-boot-starter-web中内置了tomcat,所以要把它移除
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
但是为了测试,最好这样引用:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!-- 要为provided-->
<scope>provided</scope>
</dependency>
二、提供一个SpringBootServletInitializer子类,并重写configure方法
@SpringBootApplication
public class EasyDeployWarApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(EasyDeployWarApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(EasyDeployWarApplication.class, args);
}
}
如果这里报异常,Caused by: java.lang.IllegalStateException: No SpringApplication sources have been defined. Either override the configure method or add an @Configuration annotation,这是由于项目中有两个Application启动类导致的,删除原来那个即可
三、可以通过war插件来指定war包的名字
<build>
<plugins>
<!-- war部署使用-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>easydeploy</warName>
</configuration>
</plugin>
</plugins>
</build>
假如项目的controller路径为index,则此时的访问路径为:
localhost:8080/easydeploy/index
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e
added ecosystem file for PM2 5 年前
5def40a3
Add host customization support for the NodeJS version 5 年前
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐



所有评论(0)