maven(多模块和继承)

1、多模块和继承

(1)存在的问题

对于大型项目,需要很多人同时开发,但是不想让其他人看到自己的开发内容

下载整个项目代码,非常大,包也特别多

(2)解决方案

将项目按照模块进行拆分,形成很多小的项目,每个开发人员只需要开发自己负责的小的项目

(3)继承

同时开发多个项目,这些项目都依赖于SSH的jar包,如果没一个项目都配置那么多的坐标会比较复杂。可以将坐标引入,定义父项目,开发人员开发的项目模块继承此项目即可

(4)多模块与继承

多模块解决的是项目较大不利于开发和维护的问题,而继承解决的是项目使用相同的POM配置,继承父项目,将公共配置放入到父项目。实际情况是多模块和继承是一起使用的

2、多模块开发

(1)特点:

Maven bulid父模块的时候,会自动bulid子模块

父模块的packaging必须为POM

(2)创建父模块:

新建一个Maven的web项目(maven project):

 创建坐标等

 (3)创建子模块:

新建:

选择web项目:

 子模块名称:

 

3、子模块与父模块

(1)父模块

一个子模块:

<?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>pers.zhb.test</groupId>
    <artifactId>parentmaven</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>child_1</module>
    </modules>
</project>

两个子模块:

<?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>pers.zhb.test</groupId>
    <artifactId>parentmaven</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>child_1</module>
        <module>child_2</module>
    </modules>
</project>

父项目的</modules>标签配置的是多模块

(2)子模块

<?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>parentmaven</artifactId>
        <groupId>pers.zhb.test</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>child_1</artifactId>
    <packaging>war</packaging>

    <name>child_1 Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>child_1</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

子模块继承父模块

(3)好处

子模块项目继承父模块项目的POM配置,单独开发,通过操作父模块对多个子模块一起操作(父模块执行打包操作,那么子模块也会被打包)

原文地址:https://www.cnblogs.com/zhai1997/p/12752545.html