springBoot项目jar包改war包并部署到tomcat
要将Spring Boot项目的jar包改成war包并部署到Tomcat,你需要做以下几步:
- 修改pom.xml文件,设置打包方式为war。
- 移除Spring Boot的内置Tomcat依赖。
- 添加servlet-api的依赖。
- 如果你的项目中有Thymeleaf、JSP或其他web视图,还需要添加相应的依赖。
以下是修改后的pom.xml文件的一个示例:
<project ...>
...
<packaging>war</packaging>
<dependencies>
...
<!-- 添加servlet-api的依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- 如果使用Thymeleaf,添加Thymeleaf的依赖 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.12.RELEASE</version>
</dependency>
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 移除repackage目标,因为war包将由maven的war插件打包 -->
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>prepare-resources</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
确保你的Spring Boot启动类继承了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 MyApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
最后,使用mvn clean package
命令打包你的应用程序,得到war文件
评论已关闭