一般SpringBoot打包后生成的是jar包,且自带了tomcat运行环境,是可以直接用java -jar app-0.0.1-SNAPSHOT.jar运行的,不过如果要让它跑在Tomcat下呢?
实现
Tomcat下运行的话,是需要war包,所以需要告诉pom是需要打包成war包,且不需要把tomcat运行环境导入war包。
需要修改pom.xml
- 修改<packaging>war</packaging>,告诉pom打成war包(否则会打包成jar包)
- spring-boot-starter-tomcat都scope修改成provided,就是说编译的时候会用到,但是打包的时候不需要打入最终的war包
<?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 https://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.4.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.terrynow</groupId> <artifactId>vdc</artifactId> <version>0.0.1-SNAPSHOT</version> <name>app</name> <description>SpringBoot App</description> <properties> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--spring-boot-starter-tomcat的scope修改成provided--> <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> <includeSystemScope>true</includeSystemScope> </configuration> </plugin> </plugins> </build> <!--打成war包--> <packaging>war</packaging> </project>
好了,修改好后,maven执行package后,就会生成war包,解压或放到tomcat的webapps下就可以了。
文章评论