专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

maven(多模块和继承)

1、多模块和继承

(1)存在的问题

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

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

(2)解决方案

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

(3)继承

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

(4)多模块与继承

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

2、多模块开发

(1)特点:

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

父模块的packaging必须为POM

(2)创建父模块:

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

84_1.png

创建坐标等

(3)创建子模块:

新建:

84_2.png

选择web项目:

84_3.png

子模块名称:

84_4.png

84_5.png

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>

父项目的标签配置的是多模块

(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://tech.souyunku.com/23079

未经允许不得转载:搜云库技术团队 » maven(多模块和继承)

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们