maven-idea构建多模块项目-maven的聚合和继承

多模块效果图

1. 操作步骤

将多个子项目聚合在一个父工程中,子项目中,可引入parent标签,即:继承父工程。

第一步:选择maven
第二步:填写信息
第三步:点击完成
第四步:删除src目录
第五步:选择module
第六步:选择maven工程
第七步:配置module
第八步:配置完成
父pom:maven的聚合
子pom:maven的继承

创建其他的module模块。

2. 依赖关系

1. 父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>

    <groupId>com.tellme</groupId>
    <artifactId>springboot-client</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--自动生成的子模块依赖-->
    <modules>
        <module>mm-web</module>
        <module>mm-service</module>
        <module>mm-repo</module>
        <module>mm-entity</module>
    </modules>
    <packaging>pom</packaging>

    <!--properties总体配置文件-->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <commons.lang3.version>3.5</commons.lang3.version>
    </properties>

    <!--继承springboot提供的父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <!--版本统一声明,声明子模块的版本号-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.tellme</groupId>
                <artifactId>mm-web</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>  <dependency>
                <groupId>com.tellme</groupId>
                <artifactId>mm-service</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>  <dependency>
                <groupId>com.tellme</groupId>
                <artifactId>mm-repo</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.tellme</groupId>
                <artifactId>mm-entity</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!--公共jar包声明-->
    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons.lang3.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!--插件管理-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

需要注意的点:

  1. pom.xml的打包方式:<packaging>pom</packaging>
  2. modules标签包含的是子模块;
  3. properties标签里面是总体参数配置;
  4. dependencyManagement标签并不引入依赖,是总体的依赖声明;
  5. dependencies标签引入依赖,子类共享;
  6. build插件管理,需要注意的是daoserviceentity这三个module并不需要build标签,父pom设置的子类共享。

2. entity模块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">
    <parent>
        <artifactId>springboot-client</artifactId>
        <groupId>com.tellme</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>mm-entity</artifactId>
</project>

需要注意的点:

  1. parent标签声明父标签;
  2. artifactId声明子标签,因为groupIdversion都是继承了父pom,故可以省略;
  3. 因为是最底层依赖,故可以不依赖其他子模块;

3. service模块的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">
    <parent>
        <artifactId>springboot-client</artifactId>
        <groupId>com.tellme</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>mm-service</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.tellme</groupId>
            <artifactId>mm-entity</artifactId>
        </dependency>
    </dependencies>
</project>

需要注意的点:

  1. parent标签声明父pom;
  2. service模块需要使用entity模块的数据,故需要dependencies标签引入entity模块;
  3. 注意循环引用问题,即service模块依赖entity模块;entity模块也依赖service模块

4. 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>springboot-client</artifactId>
        <groupId>com.tellme</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>mm-web</artifactId>

    <dependencies>
        <!--依赖service层-->
        <dependency>
            <groupId>com.tellme</groupId>
            <artifactId>mm-service</artifactId>
        </dependency>
        <!--依赖entity层-->
        <dependency>
            <groupId>com.tellme</groupId>
            <artifactId>mm-entity</artifactId>
        </dependency>
    </dependencies>

    <!--多模块打包-->
    <build>
        <defaultGoal>package</defaultGoal>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!-- 多模块项目仅仅需要在启动类所在的模块添加打包插件? -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

需要注意的点:

  1. web层需要依赖service层和entity层,故需将其dependency引入;
  2. 在多模块项目中仅仅需要在启动类所在的模块添加打包插件,将其打成可运行的jar。

3. 打包可执行jar

打包执行步骤
  1. 在每次打包前,需要clean。
  2. 双击运行package,看到BUILD SUCCESS 就证明打包成功了。
clean成功
package成功

打包的重点是每一个模块下的pom要配置好,谁需要打包,谁不需要打包,谁依赖谁,父工程是否声明了子模块,子模块是否声明了父工程。

执行jar包

运行jar包

测试成功

执行java -jar xxx.jar命令来测试运行打包是否成功。

聚合工程举一个简单的例子:
整个工程就好像一个公司,父工程(退休了什么都不干)只需要声明有几个儿子(子模块)就完事了。
子模块web声明父工程是谁,就当他是大儿子,公司归他管,pom.xml需要打包,需要build配置,需要其他子模块的帮助。
其他模块声明父工程是谁,之间关系都是兄弟,不需要打包,哪里需要就去哪里。

反编译mm-web.jar

我们可以看到mm-web.jar实际上在lib文件下包含了所有的jar包。

4. 需要注意的点

  1. 父pom.xml打包方式,jar要更改为pom,build需要更改;
  2. 不需要打包的模块pom.xml文件不要写<build>,全删掉,例如有些工程的common模块,utils模块都不需要打包;
  3. 声明父工程时,填写父类工程位置<relativePath>../pom.xml</relativePath>
  4. 关于application.properties配置文件,只需要在启动的模块中配置就可以了。
  5. 关于打包为什么打包jar包,不打war包,打war包目的是war包可以运行在tomcat下,但是SpringBoot是内置tomcat,如果你打war包,前提是干掉内置的tomcat,然后才能打包,各种麻烦,直接打包可执行jar包,使用java -jar 命令就可以完美的运行起来很方便!
  6. 真实开发中使用@Autowired 注解 来实现注入,而不是new对象这种方式,所以可能会产生注入以后报错,是因为你的启动类上没有配置扫描,使用

@ComponentScan(basePackages = "你的路径")注解来解决,如果你使用的持久层是Mybatis,那么你的mapper也需要扫描,在启动类上使用

@MapperScan("你的mapper文件地址")注解来解决,算了还是贴个图片吧
真实开发中使用@Autowired 注解 来实现注入,而不是new对象这种方式,所以可能会产生注入以后报错,是因为你的启动类上没有配置扫描,使用

@ComponentScan(basePackages = "你的路径")注解来解决,如果你使用的持久层是Mybatis,那么你的mapper也需要扫描,在启动类上使用

@MapperScan("你的mapper文件地址")注解来解决,算了还是贴个图片吧

多模块使用注解开启全局扫描

4. 聚合和继承的关系

区别:

  • 对于聚合模块来说,他知道哪些被聚合的模块,但那些被聚合的模块不知道这个模块的存在;

  • 对于继承关系的父POM来说,它不知道哪些子模块继承了它,但是子模块都必须知道自己的父POM是什么。

共同点:

  • 聚合POM与继承关系的父POM的packaging都是POM。
  • 聚合模块与继承关系的父模块除了POM之外都没有实际的内容。
聚合和继承

详细参看:
https://blog.csdn.net/baidu_41885330/article/details/81875395

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容