IDEA创建新maven module

1、idea创建maven多模块工程

1.1 创建父工程和pom

File -> New -> project,选择maven-archetype-site,如下图
在这里插入图片描述
父pom的packaging改为pom。比如创建父工程Spring_Mybaits_Maven_Usage,例如下图
pom依赖在这里插入图片描述

1.2.创建子模块

通常项目都有common、mapper、service子模块,创建common、mapper、service子模块,同样创建module不要勾选create from archetype,parent处是父pom。
创建common子模块如下所示,
创建第一步
在这里插入图片描述
在这里插入图片描述

点击finish就创建完成common子模块,同理,其他service,mapper等子模块也是这样创建。

父模块pom,现在的情况,
在这里插入图片描述
新建的两个子module已经自动引进来了,service和mapper已经可以了。注意修改子模块的packaging方式为:jar
在这里插入图片描述
最后父模块的pom,以及项目结构
在这里插入图片描述
各个模块之间的依赖可以通过maven 库来进行配置,和平时用依赖一样.

一个项目通用工程就是这样了,当然也可以加web、schedule、mq模块
一个完整的项目maven结构如下

parent父工程
​ |-dubbo-api --> dubbo api模块,给别人依赖,提供服务
​ |-common --> 通用模块
​ |-mapper --> 数据库模块,放数据库实体和mapper
​ |-service --> 服务的具体实现,包含dubbo服务和业务服务。通常作为一个应用
​ |-schedule --> job模块,通常用于补偿、定时操作等,通常作为一个应用

1.3.创建dubbo api模块

用于提供dubbo服务api,但是不属于父工程,独立的子模块。
选中父工程,选择New->module,直接next,不要勾选create from archetype,接着在parent这里选择None。
在这里插入图片描述
原因是对于dubbo-api模块,是要提供jar依赖给别人使用,如果选择了parent,那么需要把parent也要推送到仓库,别人才可以拉取到dubbo-api模块,这样是不规范的,因此这里要选择none,这样只把dubbo-api模块推送到仓库,别人即可下载到dubbo-api依赖.

主工程的pom如下所示,不包含dubbo-api的子模块。
在这里插入图片描述

2.父工程和子模块的依赖

2.1.parent pom添加

2.1.1.添加spring-boot-starter-parent#
添加内容如下,

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.7.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

2.1.2.添加properties,用于控制依赖的版本#
内容如下,建议版本号都加这里

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <skipTests>true</skipTests>
    <pagehelper-starter.version>1.2.10</pagehelper-starter.version>
    <pagehelper.version>5.1.8</pagehelper.version>
    <druid.version>1.1.10</druid.version>
    <minio.version>3.0.10</minio.version>
    <common.version>1.0-SNAPSHOT</hulk-common.version>
    <mapper.version>1.0-SNAPSHOT</hulk-mapper.version>
    <service.version>1.0-SNAPSHOT</hulk-mapper.version>
</properties>

2.1.3.添加dependencyManagement#
其它依赖声明也加这里

<dependencyManagement>
    <dependencies>
        <!--通用模块-->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>common</artifactId>
            <version>${common.version}</version>
        </dependency>
        <!--中MBG生成模块-->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>map</artifactId>
            <version>${mapper.version}</version>
        </dependency>
        <!--MyBatis分页插件starter-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>${pagehelper-starter.version}</version>
        </dependency>
        <!--MyBatis分页插件-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>${pagehelper.version}</version>
        </dependency>
        <!--集成druid连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

父pom通常不加dependencies,如果要加,也只是加每个子模块都需要的dependencies。这里就涉及到dependencies和dependencyManagement的区别了

2.1.4.dependencyManagement vs dependencies区别说明#

dependencyManagement应用场景
为了项目的正确运行,必须让所有的子模块使用依赖项的统一版本,必须确保应用的各个项目的依赖项和版本一致,才能保证测试的和发布的是相同的结果。在我们项目顶层的pom文件中,我们会看到dependencyManagement元素。通过它元素来管理jar包的版本,让子项目中引用一个依赖而不用显示的列出版本号。Maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用在这个dependencyManagement元素中指定的版本号。
这样做的好处:统一管理项目的版本号,确保应用的各个项目的依赖和版本一致,才能保证测试的和发布的是相同的成果,因此,在顶层pom中定义共同的依赖关系。同时可以避免在每个使用的子项目中都声明一个版本号,这样想升级或者切换到另一个版本时,只需要在父类容器里更新,不需要任何一个子项目的修改;如果某个子项目需要另外一个版本号时,只需要在dependencies中声明一个版本号即可。子类就会使用子类声明的版本号,不继承于父类版本号。

dependencies应用场景
相对于dependencyManagement,如果在父pom文件中中通过dependencies引入jar,将默认被所有的子模块继承。
子模块如果希望有自己个性化的内容,可以在子模块中对于其中的某个属性进重新定义。比如父pom声明了spring-web模块,版本是V3.2,如果子模块想使用V3.3,那么在子模块显式指定V3.3即可。

dependencyManagement与dependencies区别
dependencyManagement里只是声明依赖,并不实现引入,因此子项目需要显式的声明需要用的依赖。如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom;另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。

dependencies即使在子模块中不写该依赖项,那么子模块仍然会从父项目中继承该依赖项(全部继承)。

在实际的项目开发中,推荐在父pom中使用dependencyManagement对项目中使用到的依赖包进行统一的管理。

2.2.子模块添加依赖#
2.2.1.dubbo-api模块添加依赖#
dubbo api模块只是dubbo入参、出参以及定义的dubbo接口,实际并不需要依赖什么,当然这个模块要最小依赖,尽量少的依赖。

2.2.2.common模块#
通常工具类和通用类放这个模块,比如apache工具包,hutool工具包等,依赖这些第三方工具类

<dependencies>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons</artifactId>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

2.2.3.mapper模块#
用于数据库交互,依赖数据库以及数据源,通常也会依赖hulk-common模块

<dependencies>
        <dependency>
            <groupId>父pom中的groupId</groupId>
            <artifactId>common</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

2.2.4.service模块#
用于业务服务,通常要依赖common和mapper,也依赖spring aop等,依赖dubbo。同时需要打包,要增加spring-boot-maven-plugin插件,内容大概如下

<dependencies>
    <dependency>
        <groupId>父pom中的groupId</groupId>
        <artifactId>mapper</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>0.2.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.ctrip.framework.apollo</groupId>
        <artifactId>apollo-client</artifactId>
        <version>1.7.0</version>
    </dependency>
</dependencies>

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

3.小结#
记录下使用idea快速创建maven多module,只有两个地方需要注意:
给别人提供dubbo的依赖module,不能依赖父pom,这个工作中许多项目组都未这样做,导致别人使用依赖时候,直接炸红,原因是这样创建情况下,虽然dubbo接口推送了maven,但是对方父pom没推送maven仓库导致。
dependencyManagement和dependencies区别,前者用于定义统一的的版本号,是声明,并未实际依赖。后者是实际依赖。

删除子模块

2.2、删除模块

按下ctrl+shift+alt+s,就会弹出下面这个窗口,选中你要删除的module,然后点击上面的 “—” 号 。点击yes。
在这里插入图片描述

day02module被删除了?

其实在idea中删除不是真的删除,而只是一个小小的障眼法而已,右击day01module,点击show in Explorer,打开module所在位置。把day02文件夹删除,这样才算是真正的删除了。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值