maven父子工程 您所在的位置:网站首页 maven打包时指定的依赖包 maven父子工程

maven父子工程

2024-06-29 19:29| 来源: 网络整理| 查看: 265

场景:

因为之前用到的是,基于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插件的配置如下:

org.springframework.boot spring-boot-maven-plugin

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

org.springframework.boot spring-boot-maven-plugin exec 原因分析:

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插件配置如下:

org.springframework.boot spring-boot-maven-plugin 1.8 1.8 repackage

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

org.springframework.boot spring-boot-maven-plugin exec

其他的坑:

1.jdk8一定要指明

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

1.8 1.8 1.8

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文件:

4.0.0 org.springframework.boot spring-boot-starter-parent 1.5.6.RELEASE com.demo demo-parent pom 1.0-SNAPSHOT demo-base demo-sync demo-pattern UTF-8 UTF-8 1.8 1.5.6 org.springframework.boot spring-boot-devtools org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud spring-cloud-starter-ribbon org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-freemarker redis.clients jedis org.springframework.data spring-data-redis org.springframework.boot spring-boot-starter-redis 1.3.7.RELEASE org.springframework spring-jdbc org.springframework spring-aspects org.mybatis mybatis 3.2.8 org.mybatis mybatis-spring 1.3.2 com.alibaba fastjson 1.2.8 com.github.pagehelper pagehelper 3.7.5 com.github.jsqlparser jsqlparser 0.9.1 com.github.abel533 mapper 2.3.4 mysql mysql-connector-java com.jolbox bonecp-spring 0.8.0.RELEASE org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-logging org.springframework.boot spring-boot-starter-log4j2 org.apache.logging.log4j log4j-1.2-api 2.8.2 com.lmax disruptor 3.3.6 org.dom4j dom4j 2.1.0 io.springfox springfox-swagger2 2.6.1 io.springfox springfox-swagger-ui 2.6.1 org.springframework.cloud spring-cloud-dependencies Camden.SR7 pom import org.apache.maven.plugins maven-resources-plugin UTF-8 org.apache.maven.plugins maven-compiler-plugin 1.8 1.8 UTF-8 org.springframework.boot spring-boot-maven-plugin

base工程pom文件:

demo-base 4.0.0 com.demo demo-parent 1.0-SNAPSHOT org.springframework.boot spring-boot-maven-plugin exec

sync的pom文件:

demo-sync 4.0.0 com.demo demo-parent 1.0-SNAPSHOT com.demo demo-base 1.0-SNAPSHOT

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有