SpringBoot:SpringBoot项目部署之Jar与War

SpringBoot:SpringBoot项目部署之Jar与War

一、Jar包部署(默认)

1.Jar包部署pom文件中packaging为Jar。
<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 http://maven.apache.org/maven-v4_0_0.xsd">
    ......
    <packaging>jar</packaging>
    ......
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
<project>

2.因为SpringBoot项目内嵌tomcat,所以编译完成后,target目录中的jar包是可以直接用java -jar *.jar来运行的。服务的端口和请求根路径可以设置:

server.context-path=/project
server.port=8088

二、War包部署

1.修改pom文件
<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 http://maven.apache.org/maven-v4_0_0.xsd">
    ......
    <packaging>war</packaging>
    ......
    <dependencies>
    	......
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter</artifactId>
		<scope>provided</scope>
	</dependency>
	......
    <dependencies>
    <build>
  	<plugins>
    		 <plugin>
        		<groupId>org.springframework.boot</groupId>
        		<artifactId>spring-boot-maven-plugin</artifactId>
        		<executions>
        		   <execution>
        		      <goals>
        		         <goal>repackage</goal>
        		      </goals>
        		   </execution>
        		</executions>
     		</plugin>
  	 </plugins>
    </build>
<project>

spring-boot-starter-tomcat 是原来被传递过来的依赖,默认会打到包里,所以我们再次引入此依赖,并指定依赖范围为provided,这样tomcat 相关的jar就不会打包到war 里了。
2.添加ServletInitializer
A.方式一:Application.java直接继承SpringBootServletInitializer

@SpringBootApplication
public class Application extends SpringBootServletInitializer{
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }
}

B.方式二:在Application同包下新建类

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
     @Override
      protected SpringApplicationBuilder configure(SpringApplicationBuilderapplication) {
              return application.sources(Application.class);
      }
}

3.使用package指令生成War包,放置到Tomcat的webapp目录中,启动tomcat尝试访问。

©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页