今天新寫的SpringBoot項目打jar包部署的時候遇到一個問題,xxx.jar包中沒有主清單屬性。如下圖所示:
主清單屬性是jar包中MANIFEST.MF文件中的一個屬性,MANIFEST.MF文件位於jar包的META_INF路徑下
打開MANIFEST.MF文件,里面有如下屬性:
Manifest-Version: 1.0 Implementation-Title: com.imooc Implementation-Version: 1.0-SNAPSHOT Archiver-Version: Plexus Archiver Built-By: liu Implementation-Vendor-Id: com.zh Created-By: Apache Maven 3.6.3 Build-Jdk: 1.8.0_111 Implementation-URL: http://www.example.com Implementation-Vendor: Pivotal Software, Inc.
再貼一個正常的MANIFEST.MF:
Manifest-Version: 1.0 Implementation-Title: common Implementation-Version: 0.0.1-SNAPSHOT Spring-Boot-Version: 2.1.5.RELEASE Main-Class: org.springframework.boot.loader.JarLauncher Start-Class: com.zh.common.management.CommonApplication Spring-Boot-Classes: BOOT-INF/classes/ Spring-Boot-Lib: BOOT-INF/lib/ Build-Jdk-Spec: 1.8 Created-By: Maven Archiver 3.4.0
對比發現有問題的jar包文件中多了Archiver-Version、Built-By、Implementation-Vendor-Id三個屬性,少了Spring-Boot-Version、Main-Class、Start-Class、Spring-Boot-Classes、Spring-Boot-Lib五個Springboot相關的屬性。
Main-Class:屬性值代表了Spring Boot中啟動jar包的程序,值為 org.springframework.boot.loader.JarLauncher,這個就是Springboot啟動項目的類
Start-Class:屬性值代表了Spring Boot程序的入口類,即XXXApplication類
Spring-Boot-Classes:屬性值代表了類路徑,所有編譯后的class文件,以及配置文件,都存儲在該路徑下
Spring-Boot-Lib:屬性值代表了表示依賴的jar包存儲的位置
以上這些屬性是Springboot打包插件默認生成,缺少這些屬性項目無法運行。
所以需要再pom文件中添加Springboot打包插件:
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
按照一個正統的教程到這里應該就結束了,但是....
一頓操作之后你的項目可能會OK了,但也可能仍然有問題
我項目的POM文件是自動生成的在build標簽里面附帶了一個pluginManagement標簽,我們知道pluginManagement一般是在父pom中,pluginManagement中包裹的插件,子pom可以直接引用,但也必須在子pom中寫出來。當我百思不得其解的時候 ,我比較了我之前項目的pom文件,發現沒有這個標簽,然后我嘗試刪除pluginManagement標簽之后。再次編譯的jar包終於運行成功。這里pluginManagement的某種我不知道的特性可能對Springboot打包插件的生效產生了某種不可名狀的干擾。
<build> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins> </build>