当前位置:   article > 正文

maven父子工程---子模块相互依赖打包时所遇到的问题:依赖的程序包找不到_java maven 依赖包中的依赖包没有

java maven 依赖包中的依赖包没有

转自:https://blog.csdn.net/g56467467464/article/details/102686608

场景:

因为之前用到的是,基于springboot框架所搭建的maven工程,而且都是相互独立的。现研发经理要求将所有工程进行整合和规范化,所以抽出一个parent父工程,base基础模块(包含一些公用的实体类和工具类等),以及其他子模块(Module A、 Module B …)。Module A 以及Module B工程都需要依赖base工程。
问题:

在对Module A进行打包时,出现问题:Module A中所依赖的base工程的util程序包不存在。即使能打包成功,用java -jar启动jar包也会报Class Not Found,依赖的base工程的类找不到。
解决方案:

未解决之前在base工程的pom.xml中maven插件的配置如下:

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-maven-plugin</artifactId>
  6. </plugin>
  7. </plugins>
  8. </build>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

解决base工程的pom.xml的maven配置如下:

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-maven-plugin</artifactId>
  6. <configuration>
  7. <classifier>exec</classifier>
  8. </configuration>
  9. </plugin>
  10. </plugins>
  11. </build>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

原因分析:

spring-boot-maven-plugin打包出来的jar是不可依赖的

我们现在整合后的maven项目有一个parent工程,打包类型为pom,下面多个spring-boot工程作为它的module,分别为base和moduleA,moduleB。假如moduleA依赖于base。如果你在base中使用了spring-boot-maven-plugin的默认配置build,或者在parent工程中使用spring-boot-maven-plugin的默认配置build。那么在clean package的时候会发现moduleA找不到base中的类。原因就是默认打包出来的jar是不可依赖的。

解决方案:

  1. 官方告诉我们,你如果不想移代码,好吧,我这样来给你解决,给你打两个jar包,一个用来直接执行,一个用来依赖。于是,你需要指定一个属性classifier,这个属性为可执行jar包的名字后缀。比如我设置<classifier>exec</classifier>,原项目名为Vehicle-business。那么会得到两个jar:Vehicle-business.jar和Vehicle-bussiness-exec.jar
  2. 官方文档位置:84.5 Use a Spring Boot application as a dependency
  3. 总结:回到聚合maven上,如果你在parent工程中使用了spring-boot-maven-plugin作为builder,那么你的依赖module一定要用解决方案二来设置。否则你不在parent工程中用spring-boot-maven-plugin作为builder,而在需要打包的module上使用。
  • 1
  • 2
  • 3
  • 4
  • 5

一般parent工程的maven插件配置如下:

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-maven-plugin</artifactId>
  6. <configuration>
  7. <source>1.8</source>
  8. <target>1.8</target>
  9. </configuration>
  10. <executions>
  11. <execution>
  12. <goals>
  13. <goal>repackage</goal>
  14. </goals>
  15. </execution>
  16. </executions>
  17. </plugin>
  18. </plugins>
  19. </build>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

被依赖的maven子模块的maven插件配置如下(其余maven子模块就不需要配置):

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-maven-plugin</artifactId>
  6. <configuration>
  7. <classifier>exec</classifier>
  8. </configuration>
  9. </plugin>
  10. </plugins>
  11. </build>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

其他的坑:

1.jdk8一定要指明

     不指明的话在开发工具里运行没有一点问题,如果你没有用到java8的特性打包也没有问题。一旦你用到了java8的特性,而且使用spring-boot-maven-plugin作为builder,一定要指明jdk版本。不然你会收到类似不识别Lambda,请使用resource8这样的错误。
  • 1
  1. <properties>
  2. <java.version>1.8</java.version>
  3. <maven.compiler.source>1.8</maven.compiler.source>
  4. <maven.compiler.target>1.8</maven.compiler.target>
  5. </properties>
  • 1
  • 2
  • 3
  • 4
  • 5

2.BOOT-INF陷阱

  1. 这个问题就很恶心了。这个时候你已经打包成功,你会发现运行jar的时候报错为file not found,而且不告诉你是什么文件。你打开jar去看,发现需要的lib,配置文件,class一样也不缺。
  2. 其实这里要说一个概念,spring-boot在打包后,会把文件拷贝到BOOT-INF/Classes之下,这个时候你原来定义的扫描包路径将失效。而这个问题官方文档根本没讲,还是我没有看到。
  3. 这个陷阱在你使用packages定义扫描路径的时候等着你。或者获取工程下文件的时候。对于获取文件的话,可以在原路径前加上classes,当然你要区分开发环境或生产环境的话,你可以使用profile或者conditional来解决。如果是扫描包路径就恶心了,因为你加上classes之后,不报file not found了。而是不报错,只是警告你找不到hibernate的某些xml。但是你很可能根本没有使用hibernate。
  • 1
  • 2
  • 3
  • 4
  • 5

参考博客:https://blog.csdn.net/guduyishuai/article/details/60968728

parent工程pom文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>1.5.6.RELEASE</version>
  10. </parent>
  11. <groupId>com.demo</groupId>
  12. <artifactId>demo-parent</artifactId>
  13. <packaging>pom</packaging>
  14. <version>1.0-SNAPSHOT</version>
  15. <modules>
  16. <module>demo-base</module>
  17. <module>demo-sync</module>
  18. <module>demo-pattern</module>
  19. </modules>
  20. <properties>
  21. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  22. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  23. <java.version>1.8</java.version>
  24. <springboot.version>1.5.6</springboot.version>
  25. </properties>
  26. <dependencies>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-devtools</artifactId>
  30. </dependency>
  31. <!--WEB层-->
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-web</artifactId>
  35. </dependency>
  36. <!--测试-->
  37. <dependency>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-starter-test</artifactId>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework.cloud</groupId>
  43. <artifactId>spring-cloud-starter-eureka</artifactId>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.springframework.cloud</groupId>
  47. <artifactId>spring-cloud-starter-ribbon</artifactId>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.springframework.boot</groupId>
  51. <artifactId>spring-boot-starter-actuator</artifactId>
  52. </dependency>
  53. <!-- framework启动器 模板引擎 -->
  54. <dependency>
  55. <groupId>org.springframework.boot</groupId>
  56. <artifactId>spring-boot-starter-freemarker</artifactId>
  57. </dependency>
  58. <dependency>
  59. <groupId>redis.clients</groupId>
  60. <artifactId>jedis</artifactId>
  61. </dependency>
  62. <dependency>
  63. <groupId>org.springframework.data</groupId>
  64. <artifactId>spring-data-redis</artifactId>
  65. </dependency>
  66. <dependency>
  67. <groupId>org.springframework.boot</groupId>
  68. <artifactId>spring-boot-starter-redis</artifactId>
  69. <version>1.3.7.RELEASE</version>
  70. </dependency>
  71. <dependency>
  72. <groupId>org.springframework</groupId>
  73. <artifactId>spring-jdbc</artifactId>
  74. </dependency>
  75. <dependency>
  76. <groupId>org.springframework</groupId>
  77. <artifactId>spring-aspects</artifactId>
  78. </dependency>
  79. <!-- Mybatis -->
  80. <dependency>
  81. <groupId>org.mybatis</groupId>
  82. <artifactId>mybatis</artifactId>
  83. <version>3.2.8</version>
  84. </dependency>
  85. <dependency>
  86. <groupId>org.mybatis</groupId>
  87. <artifactId>mybatis-spring</artifactId>
  88. <version>1.3.2</version>
  89. </dependency>
  90. <!--fastjson-->
  91. <dependency>
  92. <groupId>com.alibaba</groupId>
  93. <artifactId>fastjson</artifactId>
  94. <version>1.2.8</version>
  95. </dependency>
  96. <!-- 分页助手 -->
  97. <dependency>
  98. <groupId>com.github.pagehelper</groupId>
  99. <artifactId>pagehelper</artifactId>
  100. <version>3.7.5</version>
  101. </dependency>
  102. <dependency>
  103. <groupId>com.github.jsqlparser</groupId>
  104. <artifactId>jsqlparser</artifactId>
  105. <version>0.9.1</version>
  106. </dependency>
  107. <!-- 通用Mapper -->
  108. <dependency>
  109. <groupId>com.github.abel533</groupId>
  110. <artifactId>mapper</artifactId>
  111. <version>2.3.4</version>
  112. </dependency>
  113. <!-- MySql连接驱动 -->
  114. <dependency>
  115. <groupId>mysql</groupId>
  116. <artifactId>mysql-connector-java</artifactId>
  117. </dependency>
  118. <!-- 连接池 -->
  119. <dependency>
  120. <groupId>com.jolbox</groupId>
  121. <artifactId>bonecp-spring</artifactId>
  122. <version>0.8.0.RELEASE</version>
  123. </dependency>
  124. <dependency>
  125. <groupId>org.springframework.boot</groupId>
  126. <artifactId>spring-boot-starter</artifactId>
  127. <exclusions>
  128. <exclusion> <!-- exclude掉spring-boot的默认log配置 -->
  129. <groupId>org.springframework.boot</groupId>
  130. <artifactId>spring-boot-starter-logging</artifactId>
  131. </exclusion>
  132. </exclusions>
  133. </dependency>
  134. <!-- log4j日志 -->
  135. <dependency>
  136. <groupId>org.springframework.boot</groupId>
  137. <artifactId>spring-boot-starter-log4j2</artifactId>
  138. </dependency>
  139. <!-- 有其他jar依赖log4j -->
  140. <dependency>
  141. <groupId>org.apache.logging.log4j</groupId>
  142. <artifactId>log4j-1.2-api</artifactId>
  143. <version>2.8.2</version>
  144. </dependency>
  145. <dependency>
  146. <groupId>com.lmax</groupId>
  147. <artifactId>disruptor</artifactId>
  148. <version>3.3.6</version>
  149. </dependency>
  150. <!-- https://mvnrepository.com/artifact/org.dom4j/dom4j -->
  151. <dependency>
  152. <groupId>org.dom4j</groupId>
  153. <artifactId>dom4j</artifactId>
  154. <version>2.1.0</version>
  155. </dependency>
  156. <!--在线文档-->
  157. <!--swagger本身不支持spring mvc的,springfox把swagger包装了一下,让他可以支持springmvc-->
  158. <dependency>
  159. <groupId>io.springfox</groupId>
  160. <artifactId>springfox-swagger2</artifactId>
  161. <version>2.6.1</version>
  162. </dependency>
  163. <dependency>
  164. <groupId>io.springfox</groupId>
  165. <artifactId>springfox-swagger-ui</artifactId>
  166. <version>2.6.1</version>
  167. </dependency>
  168. </dependencies>
  169. <dependencyManagement>
  170. <dependencies>
  171. <dependency>
  172. <groupId>org.springframework.cloud</groupId>
  173. <artifactId>spring-cloud-dependencies</artifactId>
  174. <version>Camden.SR7</version>
  175. <type>pom</type>
  176. <scope>import</scope>
  177. </dependency>
  178. </dependencies>
  179. </dependencyManagement>
  180. <build>
  181. <plugins>
  182. <!-- 资源文件拷贝插件 -->
  183. <plugin>
  184. <groupId>org.apache.maven.plugins</groupId>
  185. <artifactId>maven-resources-plugin</artifactId>
  186. <configuration>
  187. <encoding>UTF-8</encoding>
  188. </configuration>
  189. </plugin>
  190. <!-- java编译插件 -->
  191. <plugin>
  192. <groupId>org.apache.maven.plugins</groupId>
  193. <artifactId>maven-compiler-plugin</artifactId>
  194. <configuration>
  195. <source>1.8</source>
  196. <target>1.8</target>
  197. <encoding>UTF-8</encoding>
  198. </configuration>
  199. </plugin>
  200. <plugin>
  201. <groupId>org.springframework.boot</groupId>
  202. <artifactId>spring-boot-maven-plugin</artifactId>
  203. </plugin>
  204. </plugins>
  205. </build>
  206. </project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221

base工程pom文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <artifactId>demo-base</artifactId>
  6. <modelVersion>4.0.0</modelVersion>
  7. <parent>
  8. <groupId>com.demo</groupId>
  9. <artifactId>demo-parent</artifactId>
  10. <version>1.0-SNAPSHOT</version>
  11. </parent>
  12. <build>
  13. <plugins>
  14. <plugin>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-maven-plugin</artifactId>
  17. <configuration>
  18. <classifier>exec</classifier>
  19. </configuration>
  20. </plugin>
  21. </plugins>
  22. </build>
  23. </project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

sync的pom文件:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  2. <artifactId>demo-sync</artifactId>
  3. <modelVersion>4.0.0</modelVersion>
  4. <parent>
  5. <groupId>com.demo</groupId>
  6. <artifactId>demo-parent</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. </parent>
  9. <dependencies>
  10. <dependency>
  11. <groupId>com.demo</groupId>
  12. <artifactId>demo-base</artifactId>
  13. <version>1.0-SNAPSHOT</version>
  14. </dependency>
  15. </dependencies>
  16. </project>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/908008
推荐阅读
相关标签
  

闽ICP备14008679号