Maven的模块管理

262 阅读2分钟

这是我参与 8 月更文挑战的第6天,活动详情查看: 8月更文挑战

在进行Maven管理时,会出现重复配置以及多个项目时想统一编译的场景,那么可以使用继承pom 以及 利用Maven的聚合特性实现上述的场景。这两个场景中,父类pom文件打包目标都需要是pom(因为打包成pom,其它pom文件才能继承使用吧)

继承父类

在编写Maven pom文件时,如果多个项目存在同样的配置时,可采用父类pom将共有属性抽象出来。项目继承该父模块即可在一定程度上消除重复代码。

  • 父类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>org.example</groupId>
    <artifactId>mavenDemoParent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <!--表示共有属性 -->
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <spring.version>4.2.3.RELEASE</spring.version>
        <java.version>1.8</java.version>
    </properties>

    <!-- 表示公有的依赖 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <compilerVersion>${java.version}</compilerVersion>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
  • 子项目A的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>mavenDemoParent</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>moduleA</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>


</project>
  • 子项目B的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>mavenDemoParent</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>moduleB</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

这里父类打包成pom,项目A、B在pom文件中指定parent的坐标。这里父类pom引入了spring-core依赖,那么项目A、B也会引入spring-core依赖。

聚合项目管理

如果有多个项目,我们想一次性构建的话,可使用Maven的多模块特性来实现这一功能。这样就不需要到各个项目模块下分别执行mvn命令了。

例如有项目moduleA 和 moduleB 这样的模块,那么要使用多模块时,需要将这两个模块定义在父类pom的module属性中。

  • 父类项目 mavenDemoParent
<?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>org.example</groupId>
    <artifactId>mavenDemoParent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <!--管理子模块-->
    <modules>
        <module>moduleA</module>
        <module>moduleB</module>
    </modules>


    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <spring.version>4.2.3.RELEASE</spring.version>
        <java.version>1.8</java.version>
    </properties>

    <!-- 聚合管理中的项目依赖声明,保证子模块的版本一致-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>


</project>

  • moduleA模块的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>mavenDemoParent</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>moduleA</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>
    </dependencies>

</project>
  • moduleB模块的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>mavenDemoParent</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>moduleB</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

那么编译mavenDemoParent父类模块时,会将modules里面的子模块都编译一遍。这里父类模块声明了spring-core依赖,子模块A依赖了spring-core就不需要写版本号了,做到了统一版本,减少依赖冲突的几率(当然使用${spring.version}属性也能实现同样的效果,但是这里可以减少子pom的version配置),而子模块B没有声明依赖spring-core,那么子模块B是不会依赖spring-core的。

ps:如果不想逐一模块编译,又不能使用父子模块时,就需要根据业务场景编写脚本处理了。

依赖管理与插件管理

  • dependencyManagement元素可以在父模块中声明dependency引用,实现依赖管理;
  • pluginManagement 元素可声明plugins插件,实现插件管理。

子类如果不需要父类声明的版本,那么只需要重写覆盖即可。

继承与聚合的关系

继承与聚合很相似,但目的是不一样的。前者是为了消除重复配置,后者是为了方便快速构建项目。

对于聚合模块来说,它知道有哪些模块被聚合;而那些被聚合的模块不知道其它模块的存在。

对于继承关系的父pom来说,它不知道有哪些子模块继承于它,但那些子模块知道自己的父pom是什么。