maven install时报错 + Unable to start embedded Tomcat
最近这几天在使用maven打包spring微服务项目时总是出错:
-
第一个问题是找不到类,运行子模块时
FkptApplication
函数没找到小总结:
- maven打包出错,大概率是
pom.xml
出现问题,小概率是IDEA缓存的问题,小概率是.idea
文件夹的问题 - compile是将编译的项目保存在target文件夹下
- install是将编译的项目打包成jar保存在target文件夹下,这个jar包内嵌tomcat,包含项目可运行的servlet环境,可以在服务器上运行
问题描述:先进行maven clean,然后重新编译项目,要么maven在compile之后报错,出现
com.dpf.source.service.dto
类没找到,要么在maven的install之后报错,出现同样的问题。主要原因:在运行的application类下的target包中确实没找到相应的dto,该dto在其他模块中,只不过不同模块在命名时有重叠的地方,比如在
dpf-fkpt
模块下package为com.dpf.source
,而dpf-service
模块下的package为com.dpf.source.service
,两个package下都有dto包,导致dto包找错 - maven打包出错,大概率是
-
第二个问题是
Unable to start embedded Tomcat
,这个的原因因人的错误操作而已,解决方法也不通用,这里简单介绍我是怎么处理的:小总结:tomcat服务器报错,并不是说tomcat配置出错,而是tomcat容器在启动时,其他bean对象在进行装配时出现了问题,比如我的问题是
Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.
,即有个对象在自动注入时出错了。经检查,是因为我的
mapper.xml,mapper
接口注释掉了,但是springboot还是会去解析,导致mapper.xml
没有mapper接口对应,解决方法是mapper.xml删除掉即可(注释没用)
关于maven install时报错,参考maven install时报错 程序包不存在以及找不到类的情况
下面即为转载自https://blog.csdn.net/sinat_38570489/article/details/89504048的内容,主要修改spring-boot-maven-plugin
依赖即可
1 原因
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project ly-item-interface: Compilation failure: Compilation failure:
[ERROR] /D:/javacode/idea/leyou/ly-item/ly-item-interface/src/main/java/com/leyou/item/api/GoodsApi.java:[3,28] 程序包com.leyou.common.dto不存在
[ERROR] /D:/javacode/idea/leyou/ly-item/ly-item-interface/src/main/java/com/leyou/item/api/GoodsApi.java:[4,27] 程序包com.leyou.common.vo不存在
[ERROR] /D:/javacode/idea/leyou/ly-item/ly-item-interface/src/main/java/com/leyou/item/api/GoodsApi.java:[23,5] 找不到符号
[ERROR] 符号: 类 PageResult
[ERROR] 位置: 接口 com.leyou.item.api.GoodsApi
[ERROR] /D:/javacode/idea/leyou/ly-item/ly-item-interface/src/main/java/com/leyou/item/api/GoodsApi.java:[37,42] 找不到符号
[ERROR] 符号: 类 CartDTO
[ERROR] 位置: 接口 com.leyou.item.api.GoodsApi
[ERROR] -> [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/MojoFailureException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR] mvn <goals> -rf :ly-item-interface
Process finished with exit code 1
1.1 根本原因(以其中一个error为例分析)
ly-item-interface: Compilation failure: Compilation failure:
[ERROR] com/leyou/item/api/GoodsApi. 程序包com.leyou.common.dto不存在
ly-item-interface在打包时要依赖com.leyou.common包
就很奇怪 明明这个包这个类都是存在的啊,为什么就是找不到???
其次做了几项检查:
pom.xml依赖是否成功引入或者存在不存在单词拼写错误(一般情况不会有错啊)
common包中dto是否成功import (这么简单必然成功import了啊…)
2 解决办法
最终原因,其实就是没有依赖。。。原因就是spring-boot-maven-plugin
这个坑!!!用这个插件打包的Jar包可以直接运行,但是不可依赖!!!所以interface自始至终就没有依赖,自然会说找程序包不存在或者找不到类
最后修改pom.xml的依赖:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>execute</classifier>
</configuration>
</plugin>
</plugins>
</build>
3 总结
一个微服务通常有两个子module,一般一个写实体类和接口一个写实现方法
建议
common类或者实体类或者被被依赖的类,打包插件配置为:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<phase>none</phase>
</execution>
</executions>
<configuration>
<classifier>execute</classifier>
</configuration>
</plugin>
</plugins>
</build>
注:
<executions>
<execution>
<phase>none</phase>
</execution>
</executions>
是为了解决Unable to find main class的问题,当然,如果写了简单的main函数这几行可以不写~
其他类或者微服务可以正常配置:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
一晚上将近三个小时,碰到打包时的各种奇葩问题(为什么老师一按install好好的,我就???),还好最后看到了BUILD SUCCESS,等待他打包的过程好紧张,就怕有错,哭辽~