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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Maven 项目中,父模块和子模块的 pom.xml 文件都需要进行配置。子模块的 pom.xml 文件需要引入父模块的 pom.xml 文件,以便继承父模块的配置信息。子模块的 pom.xml 文件一般是手动创建的,可以通过以下步骤生成: 1. 在父模块的根目录下创建子模块的文件夹,比如子模块名称为 child-module。 2. 进入 child-module 文件夹,执行以下命令创建子模块的 pom.xml 文件: ``` mvn archetype:generate -DgroupId=com.example -DartifactId=child-module -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false ``` 其中,-DgroupId 表示 Maven 项目的 Group ID,-DartifactId 表示 Maven 项目的 Artifact ID,-DarchetypeArtifactId 表示使用的 Maven Archetype,-DinteractiveMode=false 表示不需要交互式输入。 3. 执行完命令后,会在 child-module 文件夹下生成一个 pom.xml 文件,该文件中包含了子模块所需的基本配置信息。在该文件中添加父模块的配置信息,即可完成子模块的配置。 注意,子模块的 pom.xml 文件中需要指定父模块的坐标信息,如下所示: ``` <project> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>child-module</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>com.example</groupId> <artifactId>parent-module</artifactId> <version>1.0-SNAPSHOT</version> </parent> <!-- 子模块的其他配置信息 --> </project> ``` 其中,parent 标签内的 groupId、artifactId 和 version 分别指定了父模块的 Group ID、Artifact ID 和版本号。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值