0
点赞
收藏
分享

微信扫一扫

maven--使用Idea打包SpringBoot项目--方法/实例

SDKB英文 2022-02-25 阅读 142


简介

        maven是后端开发常用的构建工具,本文用实例介绍如何使用maven打包SpringBoot项目。

SpringBoot打包实例

项目布局

maven--使用Idea打包SpringBoot项目--方法/实例_maven

启动程序

@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
System.out.println("[DemoApplication.main]: 9");
SpringApplication.run(DemoApplication.class, args);
}
}

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.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>test-SpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test-SpringBoot</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-aop</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

maven页面

maven--使用Idea打包SpringBoot项目--方法/实例_spring_02

先点击LifeCycle的package(会编译出两个jar)

maven--使用Idea打包SpringBoot项目--方法/实例_jar_03

运行jar:java -jar test-SpringBoot-0.0.1-SNAPSHOT.jar

[DemoApplication.main]: 9

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.0.RELEASE)

2020-08-21 14:57:51.139 INFO 90408 --- [ main] com.example.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT on DESKTOP-LOU6VCO with PID 90408 (E:\work\Idea_proj\demo\test-SpringBoot\target\test-SpringBoot-0.0.1-SNAPSHOT.jar started by 27165 in E:\work\Idea_proj\demo\test-SpringBoot\target) ......

SpringBoot打包分析

spring-boot-maven-plugin插件功能


  1. build-info:生成项目的构建信息文件 build-info.properties
  2. repackage:这个是默认 goal,在 mvnpackage 执行之后,这个命令再次打包生成可执行的 jar,同时将 mvnpackage 生成的 jar 重命名为 *.origin
  3. run:这个可以用来运行 Spring Boot 应用
  4. start:这个在 mvn integration-test 阶段,进行 SpringBoot 应用生命周期的管理
  5. stop:这个在 mvn integration-test 阶段,进行 SpringBoot 应用生命周期的管理

默认情况下使用就是 repackage 功能,其他功能要使用,则需要开发者显式配置。

注意​:

若直接点击“spring-boot:repackage”,会报错: 

Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.3.0.RELEASE:repackage (default-cli) on project test-SpringBoot: Execution default-cli of goal org.springframework.boot:spring-boot-maven-plugin:2.3.0.RELEASE:repackage failed: Source file must not be null

解答

        spring-boot:repackage首先读取已经打包的jar文件,然后进行必要的启动设置,并最终输出可执行的jar文件。

        spring-boot:repackage是Spring Boot Maven Plugin的默认goal,尝试mvn package,同样输出了可执行的jar文件。

        因此,为了重新打包得到Spring Boot可执行的jar文件,要避免直接执行mvn spring-boot:repackage。执行mvn package spring-boot:repackage或者mvn package即可。

test-SpringBoot-0.0.1-SNAPSHOT.jar与test-SpringBoot-0.0.1-SNAPSHOT.jar.original

test-SpringBoot-0.0.1-SNAPSHOT.jar 这是最终被打包成的可执行 jar 包,

test-SpringBoot-0.0.1-SNAPSHOT.jar.original 则是在打包过程中,被重命名的 jar

test-SpringBoot-0.0.1-SNAPSHOT.jar 的META-INF/MENIFEST.MF

maven--使用Idea打包SpringBoot项目--方法/实例_jar_04

Start-Class:可执行 jar 的入口类

Spring-Boot-Classes :自己代码编译后的位置

Spring-Boot-Lib:项目依赖的 jar 的位置。 BOOT-INF/lib/下有所有的jar依赖包(太多了,本处就不展开了)

test-SpringBoot-0.0.1-SNAPSHOT.jar.original

本处先去掉后缀,不然打不开。

maven--使用Idea打包SpringBoot项目--方法/实例_jar_05

可见没有启动类等相关信息,此jar只能被其他项目依赖,而不能直接运行。

其他网址

​​SpringBoot打包的jar包与普通的jar包有什么区别​​


举报

相关推荐

0 条评论