springboot打包jar中沒有主清單屬性問題
開場廢話
不看過程的可以直接看原因和解決方案
現(xiàn)在大多都是springboot項目了,甚至大多公司使用了多模塊用于dubbo和cloud。這里主要簡單講一下新手建項目,可以運行,但是打包沒有主清單屬性的問題。
出現(xiàn)這種問題的情況肯定不止以下的情況,
以下兩種依賴情況:
- a.使用了spring-boot-starter-parent + 插件=打包成功
- b.沒有使用spring-boot-starter-parent,用插件+排除=打包成功
項目結構

依賴1-能運行,打包沒有清單
主pom:
spring-boot-starter-parent
子pom:
spring-boot-starter
spring-boot-starter-web 主pom文件內容
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>diy</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modules>
<module>diy-common</module>
<module>diy-api</module>
<module>diy-service</module>
</modules>
</project>
service pom
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>diy</artifactId>
<groupId>com.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>diy-service</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
依賴1-改-能運行,打包正常
增加打包插件
主pom:
spring-boot-starter-parent
子pom:
spring-boot-starter
spring-boot-starter-web
spring-boot-maven-pluginserver pom
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>diy</artifactId>
<groupId>com.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>diy-service</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
依賴2-能運行,打包沒有清單
主pom中不加parent依賴,在子版本中指定springboot版本
主pom:
子pom:
spring-boot-starter
spring-boot-starter-web
spring-boot-maven-plugin主 pom
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>diy</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>diy-common</module>
<module>diy-api</module>
<module>diy-service</module>
</modules>
</project>
service pom
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>diy</artifactId>
<groupId>com.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>diy-service</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.5.3</version>
</plugin>
</plugins>
</build>
</project>
依賴2改-能運行,打包正常
在spring-boot-maven-plugin插件中增加executions
主pom:
子pom:
spring-boot-starter
spring-boot-starter-web
spring-boot-maven-plugin
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions><?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>diy</artifactId>
<groupId>com.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>diy-service</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.5.3</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
原因分析
使用springboot時需要使用打包插件,插件有很多,這里使用spring-boot-maven-plugin
當我們?yōu)榱俗鑫⒎諘r,在主pom中不增加多余的依賴時,子中只能設置一個parent,就是主pom,這時spring-boot-starter-parent沒有依賴,打包就會沒有清單,但是我們是添加了插件的,所以原因可能出在spring-boot-start-parent包中。
查看spring-boot-start-parent內容

這個pom里面添加了一個打包排除
解決方案
使用了 spring-boot-starter-parent依賴的直接加入插件就行
沒有使用spring-boot-starter-parent依賴的,除了加插件,在插件中加個排除
<!--這里是有spring-boot-starter-parent依賴的處理-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<!--這里是沒有spring-boot-starter-parent依賴的處理-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--因為沒有依賴parent,所以這里版本不能少,一般情況下和上面的starter版本一樣,或者可以低那么幾個小版本-->
<version>2.5.3</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java 快速排序(QuickSort)原理及實現(xiàn)代碼
這篇文章主要介紹了Java 快速排序(QuickSort)原理及實現(xiàn)代碼,有需要的朋友可以參考一下2014-01-01
springboot如何通過controller層實現(xiàn)頁面切換
在Spring Boot中,通過Controller層實現(xiàn)頁面切換背景,Spring Boot的默認注解是@RestController,它包含了@Controller和@ResponseBody,@ResponseBody會將返回值轉換為字符串返回,因此無法實現(xiàn)頁面切換,將@RestController換成@Controller2024-12-12
MyBatis傳入List集合查詢數(shù)據(jù)問題
這篇文章主要介紹了MyBatis傳入List集合查詢數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
IDEA創(chuàng)建javaee項目依賴war exploded變紅失效的解決方案
在使用IntelliJ IDEA創(chuàng)建JavaEE項目時,可能會遇到Tomcat部署的warexploded文件出現(xiàn)問題,解決方法是首先刪除有問題的warexploded依賴,然后根據(jù)圖示重新導入項目,此外,調整虛擬路徑有時也能有效解決問題2024-09-09
SpringBoot+MybatisPlus+Mysql+Sharding-JDBC分庫分表
本文主要介紹了SpringBoot+MybatisPlus+Mysql+Sharding-JDBC分庫分表,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03

