项目打包编译时出现如下错误:

错误信息:
Unable to find a single main class from the following candidates [com.hxstrive.ehcache.HelloEhcache, com.hxstrive.ehcache.HelloEhcache2]
原因分析:
项目通过 <parent> 继承 Spring Boot 的 pom.xml 来开发,如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!--<version>2.0.1.RELEASE</version>-->
<version>2.6.15</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>但是在项目主目录,即 Spring Boot 项目的启动类目录存在多个拥有 main() 方法的类,而且我们没有手动为项目指定启动类,这就导致项目编译打包时无法确定到底使用哪个拥有 main() 方法的类。
解决办法:
在 pom.xml 文件的 <properties> 标签中,通过 <start-class> 手动指定启动类,就可以解决:
<properties>
<!-- The main class to start by executing java -jar -->
<start-class>com.hxstrive.ehcache.HelloEhcache</start-class>
<!-- ... -->
</properties>注意:这种方式只适合直接继承 Spring Boot 的项目。