欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot 部署war包

程序员文章站 2022-05-26 16:05:56
...

1.修改打包pom.xml配置

 

 <packaging>war</packaging>

 

 

 

2.移除嵌入式tomcat插件,在pom.xml的spring-boot-starter-web节点,添加如下代码

 

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter</artifactId>
	<exclusions>
		<exclusion>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
		</exclusion>
	</exclusions>
</dependency>

 

 

3.加servlet-api的依赖(可不添加)

 

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>

 

 

4.通过maven定义项目名称

 

<build>
	<finalName>project name</finalName>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
                        <configuration>
	                    <executable>true</executable>
	                    <includeSystemScope>true</includeSystemScope>
                        </configuration>
		</plugin>
	</plugins>
</build>

 

 

   如果包含本地加载lib,添加一下节点

 

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-war-plugin</artifactId>
	<configuration>
		<warName>project name</warName>

 

		<webResources>
			<resource>
				<directory>src/main/resources/lib</directory>
				<targetPath>WEB-INF/lib</targetPath>
				<includes>
					<include>**/*.jar</include>
				</includes>
			</resource>
		</webResources>
	</configuration>
</plugin>

 

5.修改启动类,修改启动类,并重写configure方法。

我们需要类似于web.xml的配置方式来启动spring上下文了,在Application类的同级添加一个SpringBootStartApplication类,其代码如下:

 

/**
 * 修改启动类,继承 SpringBootServletInitializer 并重写 configure 方法
 */
public class SpringBootStartApplication extends SpringBootServletInitializer {
   public static void main( String[] args ){
       SpringApplication.run(SpringBootStartApplication .class, args);
   }

   /**
    *新增方法
    */
   @Override
   protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
       // 注意这里要指向原先用main方法执行的SpringBootStartApplication 启动类
       return builder.sources(SpringBootStartApplication .class);
   }  
}