SpringBoot Maven repackage failed: Unable to find a single main class from the following candidates

SpringBoot项目用maven插件打包异常及解决

【背景】spring-boot项目,打包成可执行jar。报错,原因大概知道,但是不了解原理,所以搜索一下答案。

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.3.RELEASE:repackage (repackage) on project facesf: Execution repackage of goal org.springframework.boot:spring-boot-maven-plugin:2.1.3.RELEASE:repackage failed: Unable to find a single main class from the following candidates [com.newcap.ActStart, com.newcap.face.encryption.EncryptionFactory, com.newcap.face.encryption.MD5, com.newcap.face.encryption.MD5Util, com.newcap.face.service.ReadFaceService, com.newcap.face.service.TestService, com.newcap.face.thread.StdiServerThread1, com.newcap.face.thread.ThreadDemo, com.newcap.face.thread.ThreadLocalDemo, com.newcap.face.util.ArrayUtils, com.newcap.face.util.DateUtils, com.newcap.face.util.HexCover, com.newcap.face.util.HttpIOUtil, com.newcap.face.util.OSinfo, com.newcap.face.util.ObjectUtils, com.newcap.face.util.PropertyUtils, com.newcap.face.util.SystemUtil, com.newcap.face.util.Test] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException

【解决】参考官网描述,没有指定或者继承了spring-boot-starter-parent并且属性未配置时,会自动寻找签名是public static void main(String[] args)的方法… 所以插件无法区分项目带有main方法的类。

[推荐]
通用解决方法:pom.xml中<configuration>标签下配置mainClass,指定程序入口。

<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<configuration>
        <mainClass>com.newcap.ActStart</mainClass>
	</configuration>
</plugin>

Spring Boot Maven Plugin提供了几个目标(goal),我们在标签里配置的repackage对应spring-boot:repackage这个目标。
在这里插入图片描述
这里的Start-Class就是我们配置的,而Main-Class受layout属性的控制,别被名字搞乱了(是不是很诡异?看看解决方法二就明白为啥如此诡异了)… 来张图直观的感受下,对应使用上面xml配置打包后的清单文件(MANIFEST.MF):
在这里插入图片描述
layout属性默认不需要配置,插件会自动推断。不同的layout属性清单文件里面的Main-Class也会相应的不同。比如layout不配置或者配置为JAR对应的Main-Class是JarLauncher,layout配置为WAR对应的Main-Class是WarLauncher。

[有限制条件]
解决方法二:如果你的pom.xml有继承自spring-boot-starter-parent(注意此前提),也可以直接在配置(其实这里的start-class直接对应清单文件里的Start-Class):

<properties>
    <start-class>com.newcap.ActStart</start-class>
</properties>

解决方法三:打包的的时候注释掉其他的@SpringBootApplication… 或者你有两处main方法并且都没有使用@SpringBootApplication注解,注释掉一个main方法…

【结果】
解决方法三很实用,于是我选择用第一种方法,233333…

附上成功图:
在这里插入图片描述


天下英雄出我辈,一入江湖岁月催
我是爱生活的「无间行者」,努力把实践过的解决方案分享给大家
一个赞、一个评论、一个关注,真的好开心,努力没有白费。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

無间行者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值