1. 程式人生 > >Spring Boot:jar中沒有主清單屬性

Spring Boot:jar中沒有主清單屬性

使用Spring Boot微服務搭建框架,在eclipse和Idea下能正常執行,但是在打成jar包部署或者直接使用java -jar命令的時候,提示了xxxxxx.jar中沒有主清單屬性

D:\hu-git\spring-xxx-xxx\target>java -jar spring-cloud-eureka-0.0.1-SNAPS
HOT.jar
spring-xxx-xxx-0.0.1-SNAPSHOT.jar中沒有主清單屬性

通過maven打jar包:mvn install, 或者在IDE中右擊選擇Run as -> maven install。在這裡有一個問題就是主清單屬性是什麼?

以SpringBoot為例,jar包中包含了三個資料夾:BOOT-INF,META-INF,org,可以把jar包解壓到資料夾下檢視,其中META-INF資料夾下有一個MANIFEST.MF檔案,該檔案指明瞭程式的入口以及版本資訊等內容,如下

Manifest-Version: 1.0
Implementation-Title: spring-xxx-xxx
Implementation-Version: 0.0.1-SNAPSHOT
Archiver-Version: Plexus Archiver
Built-By: XXXX
Implementation-Vendor-Id: com.huyikang.practice
Spring-Boot-Version: 1.5.9.RELEASE
Implementation-Vendor: Pivotal Software, Inc.
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.huyikang.practice.eureka.Application
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Created-By: Apache Maven 3.5.2
Build-Jdk: 1.8.0_151
Implementation-URL: http://maven.apache.org
Main-Class代表了Spring Boot中啟動jar包的程式Start-Class屬性就代表了Spring Boot程式的入口類,這個類中應該有一個main方法Spring-Boot-Classes代表了類的路徑,所有編譯後的class檔案,以及配置檔案,都儲存在該路徑下Spring-Boot-Lib表示依賴的jar包儲存的位置這些值都是SpringBoot打包外掛會預設生成的,如果沒有這些屬性,SpringBoot程式自然不能執行,就會報錯:jar中沒有主清單屬性,也就是說沒有按照SpringBoot的要求,生成這些必須的屬性

解決辦法在pom中新增一個SpringBoot的構建的外掛,然後重新執行 mvn install即可。

<build>
  <plugins>
  	<plugin>
  		<groupId>org.springframework.boot</groupId>
 		<artifactId>spring-boot-maven-plugin</artifactId>
  	</plugin>
  </plugins>
 </build>
在執行mvn install的時候,自動生成這些主清單屬性,執行java -jar xxx.jar時會根據主清單屬性找到啟動類,從而啟動程式。