2025年Spring Boot项目的打包和解压

Spring Boot项目的打包和解压一 Spring Boot 项目打包 1 SpringBoot 支持 jar 和 war 两种打包方式 但 Spring 建议把项目打包成 jar 2 打包方式 第 1 种 把项目打包成 jar 包 第一步 只需在 pom xml 里加入以下语句就行 packaging jar packaging 第二步

大家好,我是讯享网,很高兴认识大家。

一、Spring Boot项目打包

1、SpringBoot支持jar和war两种打包方式,但Spring建议把项目打包成jar。

2、打包方式

第1种:把项目打包成jar包

第一步:只需在pom.xml里加入以下语句就行

<packaging>jar</packaging>

讯享网

第二步:用Maven把项目打包 

(1)


讯享网

(2)

(3)(4)

(5)

 

(6)包所在的路径和jar包

第2种:把项目打包成war包

参考文章:SpringBoot项目打包成war包 - 简书

第一步:在pom.xml做如下改变(以下配置仅供参考)

讯享网<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.6</version> <relativePath /> </parent> <groupId>com.ken</groupId> <artifactId>elm-boot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>elm-boot</name> <!--第一步--> <!-- spring boot支持两种打包方式:分别为jar和war,需要在packaging里写上需要打包的方式--> <packaging>war</packaging> <description>elm 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> <!-- 移除内置的tomcat插件 --> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!-- 以上第二步 --> <!-- 第三步---将原来的内置tomcat依赖scope改为provided --> <!-- 内置tomcat --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <!--以上第三步--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- spring-boot-devtools 在 项目文档更新后,会自动部署程序 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <!--第四步--> <!--tomcat-servlet-api --> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-servlet-api</artifactId> <version>8.0.36</version> <scope>provided</scope> </dependency> <!--以上第四步--> </dependencies> <!--第五步--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!-- spring boot 默认没有web.xml,打war包时会报错需要排除 --> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> <!--以上第五步--> </project>

第二步:修改SpringBoot项目的启动类,让启动类继承SpringBootServletInitializer类并重写configure()方法(以下代码仅供参考)

import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; @SpringBootApplication @MapperScan("com.ken.elmboot.mapper") public class ElmBootApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(ElmBootApplication.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(ElmBootApplication.class); } } 

第三步:

运行结果

包所在的路径和war包

 

 二、jar包和war包的解压

1、jar包解压

(1)在jar包的所在目录的搜索栏里输入cmd,然后按回车

 

(2)在命令行输入java -jar 包名.jar

(3)效果

2、war解压

(1)war包的解压通常用Tomcat来帮我们自动解压,把war包复制到Tomcat的webapps目录下

(2)进入Tomcat的安装目录下的bin文件夹,双击运行startup.bat(Window系统是.bat,Linux系统是.sh)

bin下的startup

(3)成功

(4)效果

 

小讯
上一篇 2025-01-07 09:28
下一篇 2025-02-21 12:47

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/99375.html