首先 启动类 需要继承 SpringBootServletInitializer类,并且需要重写 configure 方法,例如:
@SpringBootApplication
public class FirstSpringbootApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(FirstSpringbootApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(FirstSpringbootApplication.class);
}
}
其次将maven配置文件pom.xml中的 packaging 修改为 war,并且添加springboot打包插件
...
<groupId>com.demo</groupId>
<artifactId>first-app</artifactId>
<version>0.0.1</version>
<name>first-app</name>
<packaging>war</packaging>
<description>demo</description>
...
<build>
<finalName>first-app</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 表示引入依赖scope为system的jar,但是只有这项的话,打war包后,
jar会被放在WEB-INF/lib-provided 必须加入下面的maven-war-plugins插件,才会将jar放入WEB-INF/lib -->
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
<!-- 下面的插件主要是为了在打包时引入项目内部已有的jar,
并且打包后会输出到 targetPath 指定的地址下,targetPath是项目根路径的相对地址,
${project.basedir}表示的是项目根路径,如果你的jar放在src/main/resources下,
应该改为 ${project.basedir}/src/main/resources/xxxxx -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<webResources>
<resource>
<directory>${project.basedir}/lib</directory>
<targetPath>WEB-INF/lib</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
使用mvn clean package -Dmaven.test.skip=true打包即可