小編這次要給大家分享的是Springboot jar主清單屬性丟失了怎么辦,文章內容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。
在開發中,用到springboot項目,當打包后部署運行時,出現了這個問題,網上搜了好多,又是加META-INF配置,又是加啥的,感覺springboot這么方便,這種問題怎么可能會搞這么復雜,于是研究了一下:
首先我們項目要依賴springboot的parent或者引入spring-boot-dependencies

或者

這樣就將springboot的pom文件導入了我們的項目,然后還要再要運行的jar包中寫入插件:

當使用繼承spring-boot-starter-parent時,就會出現標志,表示是繼承自父類的,然后我們點到spring-boot-starter-parent的pom文件中,查看插件部分:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>${start-class}</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
<resource>META-INF/spring.factories</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${start-class}</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.12.RELEASE</version>
</dependency>
</dependencies>
<configuration>
<keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</plugin>注意到里面有一個${start-class}變量,這個變量在parent的pom文件中并沒有定義,那么我們就在自己要打jar包運行的模塊定義這個變量:

然后再打包,就可以直接通過java -jar *.jar 運行項目了
如果不是繼承自parnetxml,而是選擇第一種,導入dependencies的方式:

那么就要改一下前面的spring-boot-maven-plugin插件,

我們需要指定打包路徑的main方法,這樣就可以直接打包通過 java -jar *.jar的方式運行了
重要的是一定要定義start-class變量
看完這篇關于Springboot jar主清單屬性丟失了怎么辦的文章,如果覺得文章內容寫得不錯的話,可以把它分享出去給更多人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。