SpringBoot部署
部署方式
- jar
- war
一、打jar包
springboot默认打jar包
1、可运行的项目打包
//进入项目所在的目录 eg:D:\IdeaProgram\SpringBoot>
mvn install
//结果:BUILD SUCCESS
//进入我们的项目目录,我们打的项目包在
//D:\IdeaProgram\SpringBoot\target
2、执行jar
//进入我们的jar所在的位置执行
java -jar springboot01-0.0.1-SNAPSHOT.jar
3、结果:
二、打war包
1、可运行项目打包:
-
1、新增@ServletComponentScan注解
-
2、继承SpringBootServletInitializer
package com.zwh.springboot01; import javax.servlet.annotation.WebServlet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; @ServletComponentScan @SpringBootApplication public class Springboot01Application extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(Springboot01Application.class, args); } }
-
3、修改pom.xml文件
-
1、打war包
<packaging>war</packaging>
-
2、将Tomcat私有化,避免冲突
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
完整的代码
<?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> <packaging>war</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.zwh</groupId> <artifactId>springboot01</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot01</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
-
-
4、创建war包
//进入项目所在的目录 eg:D:\IdeaProgram\SpringBoot> mvn clean package //结果:BUILD SUCCESS //进入我们的项目目录,我们打的项目包在 //D:\IdeaProgram\SpringBoot\target
2、执行war
- 将项目放到本机Tomcat的wabapps中
- 执行就可以了