【多个SpringBoot模块项目如何变成聚合项目】

【前言】

项目虽然是Eureka、OpenFeign 进行服务注册和服务调用,但是每个模块都是一个单独的SpringBoot,启动每个模块都需要单独启动一个idea,觉得这个过于繁琐,现在想把项目变成一个聚合项目,只需要启动一个idea即可。

【过程】

1、新建maven聚合父工程

【多个SpringBoot模块项目如何变成聚合项目】_第1张图片
【多个SpringBoot模块项目如何变成聚合项目】_第2张图片
【多个SpringBoot模块项目如何变成聚合项目】_第3张图片
快速创建一个maven工程,删除无关文件,只保留pom.xml

【多个SpringBoot模块项目如何变成聚合项目】_第4张图片
pom文件如下:


<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">

    
    <description>SpringBoot 多模块构建示例description>
    <modelVersion>4.0.0modelVersion>
    <name>project-aggregationname>
    <packaging>pompackaging>

    
    <groupId>com.pugroupId>
    <artifactId>project-aggregationartifactId>
    <version>1.0-SNAPSHOTversion>

    
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.5.RELEASEversion>
        <relativePath/>
    parent>

    
    <modules>
        <module>helloworldmodule>
        <module>mybatistestmodule>
    modules>

    
    <properties>
        <java.version>1.8java.version>
        <fastjson.version>1.2.32fastjson.version>
        <spring-cloud.version>Greenwich.SR1spring-cloud.version>
        <feign.version>2.1.1.RELEASEfeign.version>
        <eureka-client.version>2.1.1.RELEASEeureka-client.version>
        <zkclient.version>0.10zkclient.version>
        <poi.version>3.13poi.version>
        <lombok.version>1.18.6lombok.version>
    properties>

    
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>
        <dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poiartifactId>
        dependency>
        <dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poi-ooxml-schemasartifactId>
        dependency>
        <dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poi-ooxmlartifactId>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
        dependency>
    dependencies>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
                <version>${eureka-client.version}version>
            dependency>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-openfeignartifactId>
                <version>${feign.version}version>
            dependency>
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>fastjsonartifactId>
                <version>${fastjson.version}version>
            dependency>
            <dependency>
                <groupId>org.apache.poigroupId>
                <artifactId>poiartifactId>
                <version>${poi.version}version>
            dependency>
            <dependency>
                <groupId>org.apache.poigroupId>
                <artifactId>poi-ooxml-schemasartifactId>
                <version>${poi.version}version>
            dependency>
            <dependency>
                <groupId>org.apache.poigroupId>
                <artifactId>poi-ooxmlartifactId>
                <version>${poi.version}version>
            dependency>
            
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <version>${lombok.version}version>
            dependency>
        dependencies>

    dependencyManagement>

    
    
    
    
    
    
    
    
    

project>

2.建子模块module项目helloworld

父工程右键–》new–>module
【多个SpringBoot模块项目如何变成聚合项目】_第5张图片
【多个SpringBoot模块项目如何变成聚合项目】_第6张图片
【多个SpringBoot模块项目如何变成聚合项目】_第7张图片
【多个SpringBoot模块项目如何变成聚合项目】_第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.0modelVersion>

    
    <groupId>com.pugroupId>
    <artifactId>helloworldartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>
    <name>helloworldname>
    <description>Demo project for Spring Bootdescription>

    
    <parent>
        <groupId>com.pugroupId>
        <artifactId>project-aggregationartifactId>
        <version>1.0-SNAPSHOTversion>
        <relativePath>../pom.xmlrelativePath>
    parent>

    <dependencies>
       
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-redisartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

【细节】

(1)在进行子模块创建的时候,下面的relativePath需要执行父POM的地址才能依赖上

    <parent>
        <groupId>com.examplegroupId>
        <artifactId>intellnmartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <relativePath>relativePath>
    parent>

正确的应该是

    <parent>
        <groupId>com.examplegroupId>
        <artifactId>intellnmartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <relativePath>../pom.xmlrelativePath>
    parent>

这样就可以避免出现下面这个问题了。
Could not find artifact com.example:intellnm:pom:0.0.1-SWAPSHOT and ‘parent.relativePath’ points at no local POM line 11, column 10 -> [Help 2]

你可能感兴趣的:(SpringBoot,spring,boot,后端,java)