Apache Maven project与module之间的继承和聚合、Maven的超级pom、有效pom

1. project与module之间的继承和聚合

1.1 概念

maven项目先创建了一个project A,然后在project A(父工程)下面创建了一个module B(子工程)。则B的pom.xml能继承A的pom.xml的配置

1.2 作用

在父工程统一管理所有module的依赖版本。对同一个项目的多个module进行依赖,能方便的使用同一版本

1.3 使用实例

父工程的pom.xml如下。

<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.hh</groupId>
  <artifactId>maven-learn</artifactId>
  <version>0.1</version>
  <packaging>pom</packaging>

  <!-- 表示聚合了哪些module,共同组成一个完整的project -->
  <modules>
    <module>maven-module1</module>
  </modules>

  <properties>
      <flink.version>1.15.0</flink.version>
  </properties>

  <!-- dependencyManagement并不能给当前module引入jar包,所有父工程没有引入任何一个jar包。但dependencies可以 -->
  <dependencyManagement>
    <dependencies>

      <dependency>
        <groupId>org.apache.flink</groupId>
        <artifactId>flink-connector-jdbc</artifactId>
        <version>${flink.version}</version>
        <scope>provided</scope>
      </dependency>

      <dependency>
        <groupId>org.apache.flink</groupId>
        <artifactId>flink-connector-hive_2.12</artifactId>
        <version>${flink.version}</version>
        <scope>provided</scope>
      </dependency>

    </dependencies>
  </dependencyManagement>

</project>

子module的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">
    <modelVersion>4.0.0</modelVersion>

    <!-- 父工程的信息 -->
    <parent>
        <artifactId>maven-learn</artifactId>
        <groupId>com.hh</groupId>
        <version>0.1</version>
    </parent>

    <!-- artifactId和version默认和父工程一样 -->
    <artifactId>maven-module1</artifactId>

    <dependencies>

        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-connector-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-connector-hive_2.12</artifactId>
            <!-- 覆盖父工程的版本号 -->
            <version>1.14.5</version>
            <!-- 覆盖父工程的scope -->
            <scope>compile</scope>
        </dependency>

    </dependencies>

</project>

说明如下:

  • 通过修改父工程的pom.xml的flink.version标签的版本号,就能影响到dependencyManagement中的版本号。再通过继承,能影响子module的版本号
  • 聚合的好处:很多maven构建命令都可以在“父工程”中一键执行。以mvn install 命令为例:在父工程执行mvn install可以一键完成安装,先安装父工程,再安装子module。如果哪个子module被依赖,则优先安装。注意避免死循环依赖

2. 超级pom

2.1 简介

当我们创建一个project时,都会默认继承Maven的超级pom。超级pom定义了Maven在构建过程中有很多默认的设定,如:源文件sources存放的目录、测试源文件test-sources存放的目录、构建输出的目录target等

2.2 超级pom详细说明

超级pom位于maven的安装目录下的apache-maven-3.8.6\lib\maven-model-builder-3.8.6.jar包内。解压该jar包,在org\apache\maven\model目录下,就可以看到一个文件pom-4.0.0.xml,这就是超级pom。文件内容和说明如下:

<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!-- START SNIPPET: superpom -->
<project>
  <modelVersion>4.0.0</modelVersion>

  <repositories>
    <repository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <!-- 在maven2中layout固定为default -->
      <layout>default</layout>
      <snapshots>
        <!-- 关闭snapshot版本的依赖下载 -->
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>

  <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <!-- 构件更新的策略,默认为daily,可选值有never、always、daily、interval:X(其中X是一个数字,表示间隔的时间,单位min) -->
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
  </pluginRepositories>

  <build>
    <!-- 定义项目各个功能的目录 -->
    <directory>${project.basedir}/target</directory>
    <outputDirectory>${project.build.directory}/classes</outputDirectory>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
    <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
    <scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
    <resources>
      <resource>
        <directory>${project.basedir}/src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>${project.basedir}/src/test/resources</directory>
      </testResource>
    </testResources>
    <!-- 定义需要的插件,maven自己的插件可以省略<groupId>org.apache.maven.plugins</groupId>的定义 -->
    <pluginManagement>
      <!-- NOTE: These plugins will be removed from future versions of the super POM -->
      <!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.8</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.5.3</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <!-- site站点生成的目录 -->
  <reporting>
    <outputDirectory>${project.build.directory}/site</outputDirectory>
  </reporting>

  <profiles>
    <!-- release-profile这个以后会移除,就不看什么含义了 -->
    <!-- NOTE: The release profile will be removed from future versions of the super POM -->
    <profile>
      <id>release-profile</id>

      <activation>
        <property>
          <name>performRelease</name>
          <value>true</value>
        </property>
      </activation>

      <build>
        <plugins>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-sources</id>
                <goals>
                  <goal>jar-no-fork</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-javadoc-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-javadocs</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-deploy-plugin</artifactId>
            <configuration>
              <updateReleaseInfo>true</updateReleaseInfo>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

</project>
<!-- END SNIPPET: superpom -->

3. 有效pom

3.1 简介

pom的继承是单继承的。子工程pom继承父工程pom,可以覆盖父工程pom的配置;父工程pom继承超级pom,可以覆盖超级pom的默认配置;最后形成一个有效pom(effective POM)。而Maven的执行构建是按照最终的有效pom来的

3.2 查看有效pom命令

可以使用如下命令查看有效pom

C:\Users\dell\Desktop\maven-learn>mvn help:effective-pom
### 如何在Maven项目中实现继承配置 #### 继承机制概述 Maven支持父级POM的概念,允许子模块共享相同的配置。这不仅减少了重复劳动还提高了维护效率。当多个项目具有相似的构建需求时,可以通过创建一个父POM来集中管理公共部分[^1]。 #### 创建父POM文件 为了建立父子关系,在父项目的`pom.xml`里指定打包方式为`pom`而不是常见的jar或war: ```xml <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> <!-- 定义groupIdartifactId --> <groupId>com.example.parent</groupId> <artifactId>parent-project</artifactId> <!-- 打包类型设为 pom 表明这是一个聚合工程 --> <packaging>pom</packaging> <!-- 版本号由父 POM 控制 --> <version>1.0-SNAPSHOT</version> <!-- 配置依赖项其他插件等通用设置 --> ... </project> ``` #### 子模块声明其父节点 接着,在每一个子模块中的`pom.xml`内指明该模块所属的父亲以及对应的版本号: ```xml <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> <parent> <groupId>com.example.parent</groupId> <artifactId>parent-project</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.example.child</groupId> <artifactId>child-module</artifactId> <version>${project.version}</version> <name>Child Module Name</name> </project> ``` 这里特别注意的是`${project.version}`变量的应用,它会自动获取到父POM所定义的版本信息而无需再次硬编码输入版本字符串[^3]。 #### 使用约定优于配置原则优化继承结构 遵循Maven的最佳实践建议——即“约定大于配置”,意味着应该尽量利用默认路径命名规则来进行开发工作。这样做能够极大程度上降低复杂度并提高团队协作效率。对于大型企业级应用而言尤为重要,因为这样可以确保不同开发者之间的一致性互操作性[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值