欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot Maven Plugin打包異常解決方案

 更新時間:2020年11月23日 11:38:14   作者:門羅的魔術(shù)師  
這篇文章主要介紹了Spring Boot Maven Plugin打包異常解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

【背景】spring-boot項目,打包成可執(zhí)行jar,項目內(nèi)有兩個帶有main方法的類并且都使用了@SpringBootApplication注解(或者另一種情形:你有兩個main方法并且所在類都沒有使用@SpringBootApplication注解),pom.xml如下

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <version>1.5.3.RELEASE</version>
  <executions>
    <execution>
      <goals>
        <goal>repackage</goal>
      </goals>
    </execution>
  </executions>
</plugin>

【問題】

執(zhí)行mvn clean package,報錯如下(說點不相關(guān)的,使用install同理。因為spring-boot:repackage目標(goal)(下文會說)被綁定在package構(gòu)建階段(phases),而package階段在install階段之前,指定構(gòu)建階段之前的階段都會執(zhí)行。詳細參見:Introduction to the Build Lifecycle

  [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.3.RELEASE:repackage (default) on project webapps-api-bid: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.3.RELEASE:repackage failed: Unable to find a single main class from the following candidates [com.xx.api.main.ApiBidMain, com.xx.webapps.api.main.WebappsApiBidMain]

執(zhí)行mvn clean package spring-boot:repackage,報錯如下,不如上面日志詳細

  [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.3.RELEASE:repackage (default) on project webapps-api-bid: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.3.RELEASE:repackage failed: Unable to find main class

【解決】

  Note:參考官網(wǎng)描述,沒有指定<mainClass>或者繼承了spring-boot-starter-parent并且<start-class>屬性未配置時,會自動尋找簽名是public static void main(String[] args)的方法... 所以插件懵逼了,兩個妹子和誰在一起呢...

[推薦] 通用解決方法:<configuration>下配置mainClass,指定程序入口。

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <version>1.5.3.RELEASE</version>
  <configuration>
    <mainClass>com.xx.webapps.api.main.WebappsApiBidMain</mainClass>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>repackage</goal>
      </goals>
    </execution>
  </executions>
</plugin>

  Spring Boot Maven Plugin提供了幾個目標(goal),我們在<executions>標簽里配置的<goal>repackage</goal>對應spring-boot:repackage這個目標。

  • repackage: create a jar or war file that is auto-executable. It can replace the regular artifact or can be attached to the build lifecyle with a separate classifier.
  • run: run your Spring Boot application with several options to pass parameters to it.
  • start and stop: integrate your Spring Boot application to the integration-test phase so that the application starts before it.

  The plugin rewrites your manifest, and in particular it manages theMain-ClassandStart-Classentries, so if the defaults don't work you have to configure those there (not in the jar plugin). TheMain-Classin the manifest is actually controlled by thelayoutproperty of the boot plugin

  [譯] 該插件重寫了清單文件(MANIFEST.MF,也就是jar里面的清單文件),此文件管理著主類(Main-Class)和開始類(Start-Class)入口。清單文件中的Main-Class由layout控制

  這里的Start-Class就是我們配置的<mainClass>,而Main-Class受layout屬性的控制,別被名字搞亂了(是不是很詭異?看看解決方法二就明白為啥如此詭異了).... 來張圖直觀的感受下,對應使用上面xml配置打包后的清單文件(MANIFEST.MF):

  

  layout屬性默認不需要配置,插件會自動推斷。不同的layout屬性清單文件里面的Main-Class也會相應的不同。比如layout不配置或者配置為JAR對應的Main-Class是JarLauncher,layout配置為WAR對應的Main-Class是WarLauncher。

[有限制條件]解決方法二:如果你的pom繼承自spring-boot-starter-parent(注意此前提),也可以直接在<properties>配置<start-class>(其實這里的start-class直接對應清單文件里的Start-Class):

<properties>
  <start-class>com.xx.webapps.api.main.WebappsApiBidMain</start-class>
</properties>

解決方法三:打包的的時候注釋掉其他的@SpringBootApplication... 或者你有兩處main方法并且都沒有使用@SpringBootApplication注解,注釋掉一個main方法..... 這就是第三種解決方法233333

【隨便說說】

  說說spring-boot:repackage這個目標。Spring Boot Maven Plugin這個插件包含一系列目標(goal),我們在<executions>標簽里配置的<goal>repackage</goal>對應spring-boot:repackage這個目標,看下官方介紹

  spring-boot:repackage repackages your jar/war to be executable.

  Repackages existing JAR and WAR archives so that they can be executed from the command line using java -jar. Withlayout=NONEcan also be used simply to package a JAR with nested dependencies (and no main class, so not executable).

  簡單點說,這貨重新打包個可執(zhí)行的jar/war,可以在命令行使用-jar執(zhí)行。如果指定layout為NONE那就沒有主類只是打個普通的jar(不可執(zhí)行),一般不會這么做。

  一般情況,這個目標會打一個新的jar/war,并把maven默認打的jar/war添加.original后綴,在target目錄下可以看到:

【參考】

1.https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/

2.https://docs.spring.io/spring-boot/docs/2.0.0.BUILD-SNAPSHOT/maven-plugin//repackage-mojo.html

3.https://stackoverflow.com/questions/23217002/how-do-i-tell-spring-boot-which-main-class-to-use-for-the-executable-jar

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 基于Java實現(xiàn)Redis多級緩存方案

    基于Java實現(xiàn)Redis多級緩存方案

    這篇文章主要介紹了Redis多級緩存方案分享,傳統(tǒng)緩存方案、多級緩存方案、JVM本地緩存,舉例說明這些方案,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-03-03
  • 基于Spring Boot不同的環(huán)境使用不同的配置方法

    基于Spring Boot不同的環(huán)境使用不同的配置方法

    下面小編就為大家分享一篇基于Spring Boot不同的環(huán)境使用不同的配置方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • 淺談java 字符串,字符數(shù)組,list間的轉(zhuǎn)化

    淺談java 字符串,字符數(shù)組,list間的轉(zhuǎn)化

    下面小編就為大家?guī)硪黄獪\談java 字符串,字符數(shù)組,list間的轉(zhuǎn)化。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-11-11
  • Spring中@Transactional注解的使用詳解

    Spring中@Transactional注解的使用詳解

    @Transactional注解是Spring提供的一種聲明式事務管理方式,這篇文章主要為大家詳細介紹了@Transactional注解的原理分析及使用,需要的可以參考一下
    2023-05-05
  • Mybatis中SqlSession接口中selectList方法詳解

    Mybatis中SqlSession接口中selectList方法詳解

    這篇文章主要給大家介紹了關(guān)于Mybatis中SqlSession接口中selectList方法的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2023-03-03
  • IDEA報錯:Unable to save settings Failed to save settings

    IDEA報錯:Unable to save settings Failed to save settings

    這篇文章主要介紹了IDEA報錯:Unable to save settings Failed to save settings的相關(guān)知識,本文給大家分享問題原因及解決方案,需要的朋友可以參考下
    2020-09-09
  • SpringBoot中?Jackson?日期的時區(qū)和日期格式問題解決

    SpringBoot中?Jackson?日期的時區(qū)和日期格式問題解決

    因為最近項目需要國際化,需要能夠支持多種國際化語言,目前需要支持三種(法語、英語、簡體中文),這篇文章主要介紹了SpringBoot中?Jackson?日期的時區(qū)和日期格式問題,需要的朋友可以參考下
    2022-12-12
  • java split用法詳解及實例代碼

    java split用法詳解及實例代碼

    這篇文章主要介紹了java split用法的相關(guān)資料,并附實例代碼,幫助大家學習參考,需要的朋友可以參考下
    2016-09-09
  • Java全能工具類之Hutool的用法詳解

    Java全能工具類之Hutool的用法詳解

    Hutool是一個Java工具類庫,由國內(nèi)的程序員loolly開發(fā),目的是提供一些方便、快捷、實用的工具類和工具方法,本文就來詳細聊聊它的使用吧
    2023-03-03
  • 詳細講解Java的泛型

    詳細講解Java的泛型

    這篇文章主要介紹了Java的泛型,是Java入門學習中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-09-09

最新評論