SpringBoot多模块打包异常解决办法:Unable to find main class

6,695 阅读1分钟

困扰了两天的一个问题:
自己写了一个SpringCloud项目,下面有很多模块,有的是公共依赖、有的是一个SpringBoot启动项目。打成jar包的时候总是不成功,报以下异常:。
Execution default of goal org.springframew ork.boot:spring-boot-maven-plugin:2.0.8.RELEASE:repackage failed: Unable to find main class

首先先来看下我的pom文件:

这是我的父级Pom文件,在里面依赖了打包插件:
 <build>
      <finalName>${project.artifactId}</finalName>
      <plugins>
           <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
           </plugin>
      </plugins>
 </build>

原因就是多模块的项目不能把打包插件放到父级Pom中,只需在你子模块的SpringBoot独立运行的项目模块中依赖即可,如果是公共依赖的子模块(比如我项目中的core模块是一个公共依赖)也不需要依赖这个打包插件。
最后打包成功后的依赖图如下:

父级Pom去掉打包插件依赖。

子模块SpringBoot服务模块添加打包插件依赖: