背景
:项目A 和项目B 是两个独立的多模块项目,项目A 和项目B ,均为独立的数据源 。其中项目B 有两个数据原。 需要将项目B 以多模块的方式融合进项目A。
解决版本。建立项目C,只含有pom的,空项目,项目A和项目B做为多模块引入。其中项目B的启动类某块排除掉,只包含项目B的业务快块模块。
融合前后项目结构
融合前A 项目
融合前B 项目结构
融合后的项目C结构
步骤
1. 在gitLab 创建C项目代码仓库,并下载到本地(或者在本地创建项目空项maven目后在同步gitLab也可以)
2. 将项目A、B文件夹复制至项目C
3. 导入项目A、B的项目modul
4. 修改项目的modul 父子关系
C项目pom.xml结构
<?xml version="1.0" encoding="UTF-8"?>
<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"><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><modelVersion>4.0.0</modelVersion><groupId>C</groupId><artifactId>avicit-vpdm</artifactId><version>1.0</version><packaging>pom</packaging><properties><vvv.version>cfcustom-2.0.3-SNAPSHOT</platform6.version><!--平台版本--><spring-boot.version>2.1.3.RELEASE</spring-boot.version><!--springboot版本--><java-source.version>8</java-source.version><!--jdk版本--><java-target.version>8</java-target.version><!--jdk版本--></properties><modules><module>A</module><module>B</module></modules><repositories><repository><id>xxx</id><name>xxx</name><url>http:/xxx/xxx/xxx/mvn-group/</url></repository></repositories>
</project>
A 项目pom结构
<?xml version="1.0" encoding="UTF-8"?>
<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"><modelVersion>4.0.0</modelVersion><parent><groupId>C</groupId><artifactId>C</artifactId><version>1.0</version><relativePath/></parent><groupId>xxxx</groupId><artifactId>A</artifactId><version>0.0.1-SNAPSHOT</version><packaging>pom</packaging><description>组件父工程</description><name>avicit-pdm</name><url>https://maven.aliyun.com/mvn/search</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version>………………<prod.version>6.4.2</prod.version></properties><modules><module>A-api</module><module>A-pub</module><module>A-service</module><module>A-boot</module></modules><dependencyManagement>…………………………</dependencies></dependencyManagement><!-- Maven私服 --><repositories><repository><id>avic-public</id><name>avic-public</name><url>http://xxxx:9999/rxxxx/</url></repository></repositories>
</project>
配置说明
指定工程C的相对路径 默认为 …/pom.xml
A.service 工程配置说明
<?xml version="1.0"?>
<projectxsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><modelVersion>4.0.0</modelVersion><parent><groupId>xxx</groupId><artifactId>A</artifactId><version>0.0.1-SNAPSHOT</version><relativePath/></parent><artifactId>A-service</artifactId><description>组件的后台服务项目,提供组件的后台服务,包括rest、service、dao和mapper</description><name>avicit-pdm-service</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>avicit.platform6</groupId><artifactId>A-pub</artifactId><version>${component.version}</version></dependency></dependencies><build><resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes></resource></resources></build>
</project>
标签表示要将A-Service 模块下的资源文件也要打进包
A-pom.xml 配置
<?xml version="1.0"?>
<projectxsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><modelVersion>4.0.0</modelVersion><parent><groupId>xxx</groupId><artifactId>A</artifactId><version>0.0.1-SNAPSHOT</version><relativePath/></parent><artifactId>A-boot</artifactId><description>组件的后台服务启动工程</description><name>A-boot</name><url>http://maven.apache.org</url><profiles><!-- 开发环境配置 --><profile><id>dev</id><properties><profiles.active>dev</profiles.active></properties><!-- 默认激活该配置 --><activation><activeByDefault>true</activeByDefault></activation><build><resources><!-- 复制logback-spring.xml--><resource><directory>src/main/resources</directory><filtering>false</filtering><includes><include>logback-spring.xml</include></includes></resource></resources></build></profile></profiles><dependencies><dependency><groupId>xxxx</groupId><artifactId>A-service</artifactId><version>${component.version}</version><exclusions><exclusion><artifactId>spring-plugin-core</artifactId><groupId>org.springframework.plugin</groupId></exclusion></exclusions></dependency><dependency><groupId>xxxx</groupId><artifactId>B-order-service</artifactId><version>${component.version}</version><exclusions><exclusion><artifactId>spring-plugin-core</artifactId><groupId>org.springframework.plugin</groupId></exclusion></exclusions></dependency>……</dependencies><build><finalName>${project.artifactId}</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><mainClass>A.Application</mainClass></configuration><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>8</source><target>8</target></configuration></plugin><plugin><artifactId>maven-resources-plugin</artifactId><configuration><encoding>utf-8</encoding><useDefaultDelimiters>true</useDefaultDelimiters><nonFilteredFileExtensions><nonFilteredFileExtension>lrc</nonFilteredFileExtension><nonFilteredFileExtension>res</nonFilteredFileExtension></nonFilteredFileExtensions></configuration></plugin></plugins><resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes></resource><resource><directory>src/main/resources</directory><includes><include>application-${profiles.active}.yml</include><include>application-common.yml</include></includes><filtering>false</filtering></resource><resource><directory>src/main/resources</directory><excludes><exclude>logback-spring.xml</exclude><exclude>bootstrap.yml</exclude><exclude>application*.yml</exclude><exclude>prod/*</exclude></excludes><filtering>false</filtering></resource><resource><directory>src/main/resources</directory><includes><include>application.yml</include></includes><filtering>true</filtering></resource></resources></build>
</project>
- A-boot作为项目的启动类,需要把B的业务代码需要导入进来,需要进入
3.需要指定打包的启动类入口
pring-boot-maven-plugin,一半只需要在打包的启动类中包含,不需要在别的modul模块包含
<plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><mainClass>A.Application</mainClass></configuration><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>8</source><target>8</target></configuration></plugin><plugin><artifactId>maven-resources-plugin</artifactId><configuration><encoding>utf-8</encoding><useDefaultDelimiters>true</useDefaultDelimiters><nonFilteredFileExtensions><nonFilteredFileExtension>lrc</nonFilteredFileExtension><nonFilteredFileExtension>res</nonFilteredFileExtension></nonFilteredFileExtensions></configuration></plugin>
打包有可能会包
SpringBoot 打包异常:Unable to find main class
问题原因:因为 pom 集成了 boot-parent,打包时,在没有指定 mainClass 入口时,会找所有子模块下的 main 方法,除去 web 目录下有之外,其他模块下面均没有,导致打包异常。
1、指定对应模块的 main 入口即可。
参考
https://blog.csdn.net/azhegps/article/details/98246273
B模块的pom结构同样配置如上,只不过B模块不需要引入B-boot