将若依项目打包为WAR文件并在Tomcat中运行
要将一个基于Spring Boot的项目打包成WAR文件并部署到Tomcat中,你需要做以下几步:
- 修改
pom.xml
配置,添加或更新以下配置以支持WAR包:
<packaging>war</packaging>
<dependencies>
<!-- 移除内嵌的Tomcat容器依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- 其他依赖... -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<!-- 其他插件... -->
</plugins>
</build>
- 创建一个继承自
SpringBootServletInitializer
的启动类,并重写configure
方法:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- 使用Maven打包你的项目:
mvn clean package
- 将生成的WAR文件复制到Tomcat的
webapps
目录下。 - 启动Tomcat服务器:
cd /path/to/tomcat/bin
./startup.sh
Tomcat启动后,你的Spring Boot应用将作为WAR部署并运行在Tomcat容器中。访问应用的相应路径即可。
评论已关闭