有一个spring boot多模块项目,将数据库操作的相关代码,封装在一个名为dao的模块里,编译成jar包,供同项目的其他模块引用。开发的时候,编译、运行,都没有问题。但发布的时候却失败了,提示找不到这个dao的相关对象。
原来,jar包有2种,一种是可运行的jar包,类似exe文件;一种是普通jar包,类似dll。我们的dao模块,既然是供大家引用,应该编译成普通jar包;但由于它本身依赖于spring boot,系统默认编译成可执行jar包,所以依赖于它的其他项目发布时就报找不到的错误了。
关键在于声明jar包为普通包。dao/pom.xml
<groupId>com.monkey.test</groupId>
<artifactId>dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dao</name>
<description>Demo project for Spring Boot</description>
<packaging>jar</packaging>
。。。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 加上以下这几句 -->
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
2020.12.28
如上所述,dao这个模块编译成JAR包为项目中其他模块所调用,在开发阶段,项目间子模块引用比较方便,但发布的时候,被依赖的模块,像dao,需要先部署到本地maven库,即发布的时候要用install。