maven⽗⼦⼯程---⼦模块相互依赖打包时所遇到的问题:依赖的程序包不到
场景:
因为之前⽤到的是,基于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⼯程的l中maven插件的配置如下:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
解决base⼯程的l的maven配置如下:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</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包的名字后缀。⽐如我设置<classifier>exec</classifier>,原项⽬名为Vehicle-business。那么会得到两个jar:Vehicle-business.jar和Ve hicle-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这样的错误。
parent⼯程pom⽂件:
<?xml version="1.0" encoding="UTF-8"?>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<groupId>com.demo</groupId>
<artifactId>demo-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>demo-base</module>
<module>demo-sync</module>
<module>demo-pattern</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>
<java.version>1.8</java.version>
<springboot.version>1.5.6</springboot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--WEB层-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- framework启动器模板引擎 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
<artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId&batis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId&batis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.8</version>
</dependency>
<!-- 分页助⼿ -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>3.7.5</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>0.9.1</version>
</dependency>
<!-- 通⽤Mapper -->
<dependency>
<groupId>com.github.abel533</groupId>
<artifactId>mapper</artifactId>
<version>2.3.4</version>
</dependency>
<!-- MySql连接驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.jolbox</groupId>
<groupId>com.jolbox</groupId>
<artifactId>bonecp-spring</artifactId>
<version>0.8.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion> <!-- exclude掉spring-boot的默认log配置 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- log4j⽇志 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- 有其他jar依赖log4j -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.3.6</version>
</dependency>
<!-- mvnrepository/artifact/org.dom4j/dom4j -->
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.0</version>
</dependency>
<!--在线⽂档-->
<!--swagger本⾝不⽀持spring mvc的,springfox把swagger包装了⼀下,让他可以⽀持springmvc--> <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<build>
<plugins>
<!-- 资源⽂件拷贝插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
mysql下载jar包<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
base⼯程pom⽂件: <?xml version="1.0" encoding="UTF-8"?>
<artifactId>demo-base</artifactId>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.demo</groupId>
<artifactId>demo-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
sync的pom⽂件:
demo-sync
4.0.0
com.demo demo-parent 1.0-SNAPSHOT com.demo demo-base 1.0-SNAPSHOT
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论