一、关于war打包
1、修改POM的打包方式,由JAR改为WAR
<version>0.0.1-SNAPSHOT</version>
<name>scheduled</name>
<description>scheduled</description>
<packaging>war</packaging>
2、删除打包依赖,剔除SpringBoot内置的TOMCAT相关的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
如果未添加下面的依赖,打成的WAR包内会包含关于内置TOMCAT的多余的JAR包,如果实际使用的TOMCAT与内置的TOMCAT版本一致的话,并不影响项目的部署与运行,如果不一致则会出现项目版本冲突的问题。
3、移除内置的WEB容器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 移除内置的WEB容器 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
4、提供一个 SpringBootServletInitializer 的子类,重写其 configure 方法
新增一个重写 configure 方法的,实现 SpringBootServletInitializer 的子类。
public class MySpringBootServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
// 程序的入口
return application.sources(ServicexApplication.class);
}
}
5、在添加两个依赖
<!-- 添加servlet-api的依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!--这样写的意思是,编写servlet时用我们自己引用的包,而运行时用tomcat自己的包,
这样就不用冲突了,又能正常开发代码-->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>8.5.81</version>
<scope>provided</scope>
</dependency>
clean一下,package一下就OK了
常见问题
①. 原有端口失效
当采用WAR部署到TOMCAT的WEB容器中时,在application-dev.yml中通过server.port: 9099设置的端口不在起作用,如果要修改端口,则需要修改TOMCAT的WEB容器的端口为9099。
②. 原有访问路径失效
在前后端分离的项目中,当使用JAR包部署且未设置访问前缀的情况下,前端的访问路径一般为:http://localhost:9099/+XXX,当使用WAR包部署时,访问路径变为:http://localhost:9099/servicex-test-1.0-SNAPSHOT/+XXX。
SWAGGER:http://localhost:9099/servicex-test-1.0-SNAPSHOT/swagger-ui.html
二、关于jar打包
1、配置打包的xml文件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<descriptors>
<descriptor>/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
2、它可以将项目打包成war文件,并生成部署描述符web.xml等。 该插件可以根据您的 配置自定义web.xml,添加资源文件,依赖项等。同时,它还可以帮助您配置应用程序的上 下文根和servlet版本。
<plugin>
<!-- 它可以将项目打包成war文件,并生成部署描述符web.xml等。 该插件可以根据您的
配置自定义web.xml,添加资源文件,依赖项等。同时,它还可以帮助您配置应用程序的上
下文根和servlet版本。-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
3、maven的单元测试插件
<plugin>
<!-- 必须显式的声明测试插件,否则无法执行测试 -->
<!-- maven的单元测试插件-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
4、maven 的默认编译使用的 jdk 版本有时候不通用,使用 maven-compiler-plugin 插件可以指定项目源码的 jdk 版本,编译后的 jdk 版本,以及编码。
<plugin>
<!-- maven 的默认编译使用的 jdk 版本有时候不通用,使用 maven-compiler-plugin
插件可以指定项目源码的 jdk 版本,编译后的 jdk 版本,以及编码。-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>