Spring Boot Maven Plugin打包异常解决方案
作者:门罗的魔术师 发布时间:2022-04-17 11:23:41
【背景】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
来源:https://www.cnblogs.com/powerwu/articles/12092912.html
猜你喜欢
- 说道线程,肯定会想到使用 java.lang.Thread.java这个类那么创建线程也主要有2种方式第一种方式:public class
- 本文需求实现了java通过方向键控制小球移动的具体过程,供大家参考,具体内容如下需求分析:第一 要画出一个小球第二 要能通过控制方向键控制它
- 一、Java IO流1、概念在Java中,把不同的输入源 / 输出源(如:键盘、文件、网络链接等)抽象的表述为“流”(stream)通过 ”
- Java 8中引入了CompletableFuture类,它是一种方便的异步编程工具,可以处理各种异步操作,如网络请求、文件IO和数据库操作
- centos下搭建GitLab+Jenkins持续集成环境,供大家参考,具体内容如下1、安装JDKyum install -y java2、
- 在 Java 语言中的类初始化块 文章中我们简单的介绍了下 Java 中的实例初始化块 ( IIB )。不过我觉得介绍的有点简单了,于是,再
- 一、音乐播放器的实现原理 Javase的多媒体功能很弱,所以有一个专门处理多媒体的插件叫JMF,JMF提供的模型可大致分为七类*
- Alt+Enter快捷键是Idea中比较特殊的一个快捷键。它有很多功能,比如:导入包,自动修正代码 解决出现的问题 也可以生成返回值。这里有
- springboot整合redis主从sentinel一主二从三sentinel配置1、master:127.0.0.1:63792、sla
- 前言兄弟们,刚刚又给seata社区修了一个BUG,有用户提了issue反应TransactionHook在某些情况下不会被调用:相关issu
- Maven --> Gradle首先安装gradle:Mac安 * rew install gradleUbuntu安装apt inst
- 使用Post添加数据到数据库出现方块乱码解决方法,在web.xml里最前面添加过滤器,代码如下,放在最前面,因为有优先级,要首先拦截<
- 最近总是有人问如何通过Silverlight上传图片并保存的后台服务器?众所周知,Silverlight是客户端程序,不能很好与服务器进行“
- 日记基础操作编程期调试代码运营期记录信息记录日常运营重要信息(峰值流量,平均响应时长...)记录应用报错信息(错误堆栈)记录运维过程数据(扩
- 概述:@ResponseBody @RequestMapping(value="/download/{fileName:[a-zA
- 短网址,忽然一下子就冒出来的东西,长长的一个URL,提交过去,出来就只有短短的一个URL了,看起来似乎挺神奇,其实简单分析一下,明白其中的原
- 客户端import java.io.BufferedReader;import java.io.InputStreamReader;impo
- 一、简介在Spring中,有这么2个接口:BeanFactory和FactoryBean,名字很相似,很多小伙伴经常混淆,在面试的时候也经常
- protected bool IsChineseLetter(string input,int index){int code = 0;in
- 本文实例为大家分享了Android简单使用PopupWindow的的具体代码,供大家参考,具体内容如下思路1.在res下面创建一个menu文