利用idea构建spring boot多项目(二)

发布时间:2018-07-11 浏览次数:1588 文章来源:个人博客

上一篇文章中,已经介绍了用IDEA创建父级工程和四个子项目,本篇文章中,将介绍父级和各个子模块pom.xml的基本依赖。

第三步:编辑模块依赖

(1)父级模块的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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.demo</groupId>
    <artifactId>demo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>web</module>
        <module>service</module>
        <module>model</module>
        <module>dao</module>
    </modules>

    <!-- 继承说明:这里继承SpringBoot提供的父工程 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
    </parent>


    <!-- 版本说明:这里统一管理依赖的版本号 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.demo</groupId>
                <artifactId>web</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.demo</groupId>
                <artifactId>service</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.demo</groupId>
                <artifactId>model</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.demo</groupId>
                <artifactId>dao</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

父级模块主要就是管理版本,已经继承springboot提供的父工程。

至此,父级模块的maven已经配置完成。


(2)web子模块:

web子模块作为网站的入口,所以应该依赖spring-boot-starter-web。

web子模块的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>
        <artifactId>demo</artifactId>
        <groupId>com.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>web</artifactId>

    <dependencies>
        <!--需要service模块-->
        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>service</artifactId>
        </dependency>
        <!--需要model模块-->
        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>model</artifactId>
        </dependency>
        <!--spring boot的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>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

这里需要提及到的是:各个子模块之间的互相依赖关系如下:

web模块:依赖(service)(model)

service模块:依赖(model)(dao)

model模块:无

dao模块:依赖(model)

这是基本的关系,所以下面的子模块pom.xml基本就是围绕上述依赖来进行。

如:

(3)service模块的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>
        <artifactId>demo</artifactId>
        <groupId>com.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>service</artifactId>

    <dependencies>
        <!--依赖model-->
        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>model</artifactId>
        </dependency>
        <!--依赖dao-->
        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>dao</artifactId>
        </dependency>
    </dependencies>


</project>

model,dao模块的也是类似,就不列举出来,自行配置即可。


配置完毕后,对着项目进行右键->maven->reimport


至此,就完成了基本的设置了。


下一篇将介绍子模块的目录创建,和其他扩展的依赖。

key-word
springboot多项目 多项目构建 spring boot pom.xml