Spring Boot Maven Plugin打包异常解决方案

 更新时间:2020年11月23日 13:00  点击:1819

【背景】spring-boot项目,打包成可执行jar,项目内有两个带有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>

【问题】

执行mvn clean package,报错如下(说点不相关的,使用install同理。因为spring-boot:repackage目标(goal)(下文会说)被绑定在package构建阶段(phases),而package阶段在install阶段之前,指定构建阶段之前的阶段都会执行。详细参见: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]

执行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:参考官网描述,没有指定<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).

  简单点说,这货重新打包个可执行的jar/war,可以在命令行使用-jar执行。如果指定layout为NONE那就没有主类只是打个普通的jar(不可执行),一般不会这么做。

  一般情况,这个目标会打一个新的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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

[!--infotagslink--]

相关文章

  • 解决执行maven命令时提示Process terminated的问题

    这篇文章主要介绍了解决执行maven命令时提示Process terminated的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-09-25
  • Spring AOP 对象内部方法间的嵌套调用方式

    这篇文章主要介绍了Spring AOP 对象内部方法间的嵌套调用方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-08-29
  • 解决springboot使用logback日志出现LOG_PATH_IS_UNDEFINED文件夹的问题

    这篇文章主要介绍了解决springboot使用logback日志出现LOG_PATH_IS_UNDEFINED文件夹的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-04-28
  • Spring Cloud 中@FeignClient注解中的contextId属性详解

    这篇文章主要介绍了Spring Cloud 中@FeignClient注解中的contextId属性详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-25
  • Springboot如何实现Web系统License授权认证

    这篇文章主要介绍了Springboot如何实现Web系统License授权认证,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-05-28
  • 解决idea2020 maven无法自动导包的问题

    这篇文章主要介绍了解决idea2020 maven无法自动导包的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-13
  • SpringBoot实现excel文件生成和下载

    这篇文章主要为大家详细介绍了SpringBoot实现excel文件生成和下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09
  • 解决IDEA maven 项目修改代码不生效,mvn clean、install后才生效

    这篇文章主要介绍了解决IDEA maven 项目修改代码不生效,mvn clean、install后才生效的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-09-25
  • idea 打包maven项目忽略test文件的操作

    这篇文章主要介绍了idea 打包maven项目忽略test文件的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-05
  • 详解Maven profile配置管理及激活profile的几种方式

    这篇文章主要介绍了详解Maven profile配置管理及激活profile的几种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-26
  • 如何在Spring WebFlux的任何地方获取Request对象

    这篇文章主要介绍了如何在Spring WebFlux的任何地方获取Request对象,帮助大家更好的理解和使用springboot框架,感兴趣的朋友可以了解下...2021-01-26
  • 详解springBoot启动时找不到或无法加载主类解决办法

    这篇文章主要介绍了详解springBoot启动时找不到或无法加载主类解决办法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-16
  • 详解SpringCloudGateway内存泄漏问题

    这篇文章主要介绍了详解SpringCloudGateway内存泄漏问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-07-16
  • SpringBoot集成Redis实现消息队列的方法

    这篇文章主要介绍了SpringBoot集成Redis实现消息队列的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-10
  • 解决Springboot get请求是参数过长的情况

    这篇文章主要介绍了解决Springboot get请求是参数过长的情况,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-09-17
  • Spring Boot项目@RestController使用重定向redirect方式

    这篇文章主要介绍了Spring Boot项目@RestController使用重定向redirect方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-02
  • Springboot+TCP监听服务器搭建过程图解

    这篇文章主要介绍了Springboot+TCP监听服务器搭建过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-10-28
  • springBoot 项目排除数据库启动方式

    这篇文章主要介绍了springBoot 项目排除数据库启动方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-10
  • Spring为什么不推荐使用@Autowired注解详析

    @Autowired 注解的主要功能就是完成自动注入,使用也非常简单,但这篇文章主要给大家介绍了关于Spring为什么不推荐使用@Autowired注解的相关资料,需要的朋友可以参考下...2021-11-03
  • Springboot如何使用mybatis实现拦截SQL分页

    这篇文章主要介绍了Springboot使用mybatis实现拦截SQL分页,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-19