xxx.jar或者xxx.war中沒有主清單屬性和spring-boot-maven-plugin的作用


因為springboot本身集成了tomcat插件,所以我們可以直接使用mvn clean package命令打成jar包或者war包,然后使java -jar xxx.jar 或者 java -jar xxx.war命令非常方便的去部署運行項目。
但是在執行命令時提示:

PS F:\wkh_code\my-blog\target> java -jar myblog.war
myblog.war中沒有主清單屬性
PS F:\wkh_code\my-blog\target>

這里引出一個問題:主清單屬性是什么

當我們把springboot項目打成war包或者jar后,解壓之后我們可以發現一個名為META-INF文件夾。其中META-INF文件夾下有一個名為MANIFEST.MF的文件,該文件指明了程序的入口以及版本信息等內容,內容如下

Manifest-Version: 1.0
Implementation-Title: my-blog
Implementation-Version: 1.0-SNAPSHOT
Archiver-Version: Plexus Archiver
Built-By: hua
Implementation-Vendor-Id: com.wangkaihua
Spring-Boot-Version: 1.4.0.RELEASE
Implementation-Vendor: Pivotal Software, Inc.
Main-Class: org.springframework.boot.loader.WarLauncher
Start-Class: com.wangkaihua.myblog.Application
Spring-Boot-Classes: WEB-INF/classes/
Spring-Boot-Lib: WEB-INF/lib/
Created-By: Apache Maven 3.5.0
Build-Jdk: 1.8.0_144
Implementation-URL: http://projects.spring.io/spring-boot/my-blog/

其中有Start-Class:指明了程序的入口類、Spring-Boot-Classes:指明了類的路徑,所有編譯后的class文件還有Spring-Boot-Lib等等,這些都是springboot項目獨立運行必須要知道的。所以應該是我們打包的姿勢不對,我們缺少了一個打包插件:spring-boot-maven-plugin

這里引出第二個問題:spring-boot-maven-plugin作用

Spring Boot Maven plugin能夠將Spring Boot應用打包為可執行的jar或war文件,然后以通常的方式運行Spring Boot應用。

解決方法

在pom.xml中引入對spring-boot-maven-plugin插件的依賴

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

執行mvn clean package,將自動生成必要的主清單屬性,運行java -jar xxx.jar或者java -jar xxx.war時會根據主清單屬性找到啟動類啟動程序。

參考:
沒有主清單屬性
springboot maven插件詳解


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM