SpringBoot部署02

本文详细讲解了如何使用SpringBoot将项目打包成jar和war格式,包括jar包的执行、war包的配置与部署,适合初学者理解SpringBoot部署流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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中
  • 执行就可以了
3、结果:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值