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

转载:https://blog.csdn.net/DamonREN/article/details/85091900#commentBox

场景:

因为之前用到的是,基于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>

解决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>

原因分析:

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是不可依赖的。

解决方案:

        官方告诉我们,你如果不想移代码,好吧,我这样来给你解决,给你打两个jar包,一个用来直接执行,一个用来依赖。于是,你需要指定一个属性classifier,这个属性为可执行jar包的名字后缀。比如我设置exec,原项目名为Vehicle-business。那么会得到两个jar:Vehicle-business.jar和Vehicle-bussiness-exec.jar

         官方文档位置:84.5 Use a Spring Boot application as a dependency

         总结:回到聚合maven上,如果你在parent工程中使用了spring-boot-maven-plugin作为builder,那么你的依赖module一定要用解决方案二来设置。否则你不在parent工程中用spring-boot-maven-plugin作为builder,而在需要打包的module上使用。

一般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>

被依赖的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.jdk8一定要指明

         不指明的话在开发工具里运行没有一点问题,如果你没有用到java8的特性打包也没有问题。一旦你用到了java8的特性,而且使用spring-boot-maven-plugin作为builder,一定要指明jdk版本。不然你会收到类似不识别Lambda,请使用resource8这样的错误。


 
 
   
   
   
   
  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>

2.BOOT-INF陷阱

          这个问题就很恶心了。这个时候你已经打包成功,你会发现运行jar的时候报错为file not found,而且不告诉你是什么文件。你打开jar去看,发现需要的lib,配置文件,class一样也不缺。

          其实这里要说一个概念,spring-boot在打包后,会把文件拷贝到BOOT-INF/Classes之下,这个时候你原来定义的扫描包路径将失效。而这个问题官方文档根本没讲,还是我没有看到。

          这个陷阱在你使用packages定义扫描路径的时候等着你。或者获取工程下文件的时候。对于获取文件的话,可以在原路径前加上classes,当然你要区分开发环境或生产环境的话,你可以使用profile或者conditional来解决。如果是扫描包路径就恶心了,因为你加上classes之后,不报file not found了。而是不报错,只是警告你找不到hibernate的某些xml。但是你很可能根本没有使用hibernate。
 

参考博客: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. <dependency>
  32. <groupId>org.springframework.boot groupId>
  33. <artifactId>spring-boot-starter-web artifactId>
  34. dependency>
  35. <dependency>
  36. <groupId>org.springframework.boot groupId>
  37. <artifactId>spring-boot-starter-test artifactId>
  38. dependency>
  39. <dependency>
  40. <groupId>org.springframework.cloud groupId>
  41. <artifactId>spring-cloud-starter-eureka artifactId>
  42. dependency>
  43. <dependency>
  44. <groupId>org.springframework.cloud groupId>
  45. <artifactId>spring-cloud-starter-ribbon artifactId>
  46. dependency>
  47. <dependency>
  48. <groupId>org.springframework.boot groupId>
  49. <artifactId>spring-boot-starter-actuator artifactId>
  50. dependency>
  51. <dependency>
  52. <groupId>org.springframework.boot groupId>
  53. <artifactId>spring-boot-starter-freemarker artifactId>
  54. dependency>
  55. <dependency>
  56. <groupId>redis.clients groupId>
  57. <artifactId>jedis artifactId>
  58. dependency>
  59. <dependency>
  60. <groupId>org.springframework.data groupId>
  61. <artifactId>spring-data-redis artifactId>
  62. dependency>
  63. <dependency>
  64. <groupId>org.springframework.boot groupId>
  65. <artifactId>spring-boot-starter-redis artifactId>
  66. <version>1.3.7.RELEASE version>
  67. dependency>
  68. <dependency>
  69. <groupId>org.springframework groupId>
  70. <artifactId>spring-jdbc artifactId>
  71. dependency>
  72. <dependency>
  73. <groupId>org.springframework groupId>
  74. <artifactId>spring-aspects artifactId>
  75. dependency>
  76. <dependency>
  77. <groupId>org.mybatis groupId>
  78. <artifactId>mybatis artifactId>
  79. <version>3.2.8 version>
  80. dependency>
  81. <dependency>
  82. <groupId>org.mybatis groupId>
  83. <artifactId>mybatis-spring artifactId>
  84. <version>1.3.2 version>
  85. dependency>
  86. <dependency>
  87. <groupId>com.alibaba groupId>
  88. <artifactId>fastjson artifactId>
  89. <version>1.2.8 version>
  90. dependency>
  91. <dependency>
  92. <groupId>com.github.pagehelper groupId>
  93. <artifactId>pagehelper artifactId>
  94. <version>3.7.5 version>
  95. dependency>
  96. <dependency>
  97. <groupId>com.github.jsqlparser groupId>
  98. <artifactId>jsqlparser artifactId>
  99. <version>0.9.1 version>
  100. dependency>
  101. <dependency>
  102. <groupId>com.github.abel533 groupId>
  103. <artifactId>mapper artifactId>
  104. <version>2.3.4 version>
  105. dependency>
  106. <dependency>
  107. <groupId>mysql groupId>
  108. <artifactId>mysql-connector-java artifactId>
  109. dependency>
  110. <dependency>
  111. <groupId>com.jolbox groupId>
  112. <artifactId>bonecp-spring artifactId>
  113. <version>0.8.0.RELEASE version>
  114. dependency>
  115. <dependency>
  116. <groupId>org.springframework.boot groupId>
  117. <artifactId>spring-boot-starter artifactId>
  118. <exclusions>
  119. <exclusion>
  120. <groupId>org.springframework.boot groupId>
  121. <artifactId>spring-boot-starter-logging artifactId>
  122. exclusion>
  123. exclusions>
  124. dependency>
  125. <dependency>
  126. <groupId>org.springframework.boot groupId>
  127. <artifactId>spring-boot-starter-log4j2 artifactId>
  128. dependency>
  129. <dependency>
  130. <groupId>org.apache.logging.log4j groupId>
  131. <artifactId>log4j-1.2-api artifactId>
  132. <version>2.8.2 version>
  133. dependency>
  134. <dependency>
  135. <groupId>com.lmax groupId>
  136. <artifactId>disruptor artifactId>
  137. <version>3.3.6 version>
  138. dependency>
  139. <dependency>
  140. <groupId>org.dom4j groupId>
  141. <artifactId>dom4j artifactId>
  142. <version>2.1.0 version>
  143. dependency>
  144. <dependency>
  145. <groupId>io.springfox groupId>
  146. <artifactId>springfox-swagger2 artifactId>
  147. <version>2.6.1 version>
  148. dependency>
  149. <dependency>
  150. <groupId>io.springfox groupId>
  151. <artifactId>springfox-swagger-ui artifactId>
  152. <version>2.6.1 version>
  153. dependency>
  154. dependencies>
  155. <dependencyManagement>
  156. <dependencies>
  157. <dependency>
  158. <groupId>org.springframework.cloud groupId>
  159. <artifactId>spring-cloud-dependencies artifactId>
  160. <version>Camden.SR7 version>
  161. <type>pom type>
  162. <scope>import scope>
  163. dependency>
  164. dependencies>
  165. dependencyManagement>
  166. <build>
  167. <plugins>
  168. <plugin>
  169. <groupId>org.apache.maven.plugins groupId>
  170. <artifactId>maven-resources-plugin artifactId>
  171. <configuration>
  172. <encoding>UTF-8 encoding>
  173. configuration>
  174. plugin>
  175. <plugin>
  176. <groupId>org.apache.maven.plugins groupId>
  177. <artifactId>maven-compiler-plugin artifactId>
  178. <configuration>
  179. <source>1.8 source>
  180. <target>1.8 target>
  181. <encoding>UTF-8 encoding>
  182. configuration>
  183. plugin>
  184. <plugin>
  185. <groupId>org.springframework.boot groupId>
  186. <artifactId>spring-boot-maven-plugin artifactId>
  187. plugin>
  188. plugins>
  189. build>
  190. project>

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>

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>

 

你可能感兴趣的:(后端,maven,部署打包)