Maven进阶系列-继承和聚合

这篇具有很好参考价值的文章主要介绍了Maven进阶系列-继承和聚合。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Maven进阶系列-继承和聚合

1. 继承

Maven工程之间存在继承关系,例如工程B继承工程A,工程C也继承了工程A

ProjectA的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>

    <groupId>com.xmc</groupId>
    <artifactId>ProjectA</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>ProjectB</module>
        <module>ProjectC</module>
    </modules>

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

</project>

ProjectB的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">
    <parent>
        <artifactId>ProjectA</artifactId>
        <groupId>com.xmc</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ProjectB</artifactId>

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

</project>

ProjectC的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">
    <parent>
        <artifactId>ProjectA</artifactId>
        <groupId>com.xmc</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ProjectC</artifactId>

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

</project>

Maven进阶系列-继承和聚合,maven,maven,java

本质上是ProjectB和ProjectC的 pom.xml 中的配置
继承了ProjectA中 pom.xml 的配置。

Maven进阶系列-继承和聚合,maven,maven,java

这里提到了超级pom.xml,对于我们创建的一个maven工程,即便我们自己的pom.xm文件中没有明确指定一个父工程(父POM),其实也默认继承了超级POM,就好比JAVA类继承Object类一样。

maven官网关于超级POM的介绍:
https://maven.apache.org/pom.html#Inheritance
link

超级POM文件的位置:
例如,我使用的是maven 3.6.1版本:
Maven进阶系列-继承和聚合,maven,maven,java

打开这个jar包,可以看到超级POM文件:
Maven进阶系列-继承和聚合,maven,maven,java

超级pom.xml文件

<?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>
      <layout>default</layout>
      <snapshots>
        <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>
        <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>
    <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>

  <reporting>
    <outputDirectory>${project.build.directory}/site</outputDirectory>
  </reporting>

  <profiles>
    <!-- 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 -->

2. 继承的作用

2.1 在父工程中配置依赖的统一管理

  • 在父工厂projectA的pom.xml中定义
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-core</artifactId>
            <version>5.8.15</version>
        </dependency>
    </dependencies>
</dependencyManagement>
  • 然后在子工程projectC的pom.xml中使用的时候就不需要申明版本号,在父工程中统一管理jar包版本
<dependencies>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
    </dependency>
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-core</artifactId>
    </dependency>
</dependencies>

2.2 在父工程中声明自定义属性

<!-- 通过自定义属性,统一指定Spring的版本 -->
<properties>
  <!-- 自定义标签,维护Spring版本数据 -->
  <spring.version>6.0.6</spring.version>
</properties>

在需要的地方使用${}的形式来引用自定义的属性名:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
</dependency>

2.3 父工程中必须要继承的配置

  • 父工程中的依赖
<dependencies>
。。。
<dependencies>

这里的依赖不是 dependencyManagement>中的依赖,dependencyManagement>是定义,并不引入依赖

  • 父工程中的构建

插件设置方法与前面的依赖极为相似,同样也有插件管理 pluginManagement,与 dependencyManagement 类似,这里给出一个 parent 中设置的样例模板

<build>
    <plugins>
		<!-- 声明必须继承的插件 -->
    </plugins>
    <pluginManagement>
        <plugins>
			<!-- 声明可供选择是否继承的插件 -->
        </plugins>
    </pluginManagement>
</build>

3. 聚合

在Maven中,聚合(aggregation)是指将多个项目或模块组合成一个更大的项目的过程。通过使用聚合,可以将多个相关的项目捆绑在一起,以便一次性构建和部署。

在Maven中,可以使用<modules>元素来定义聚合关系。在父项目的pom.xml文件中,可以使用<modules>元素列出所有要聚合的项目或模块。例如:

<modules>
    <module>ProjectB</module>
    <module>ProjectC</module>
</modules>

4. 聚合的作用

  • 一键执行 Maven 命令:很多构建命令都可以在“总工程”中一键执行。

以install 命令为例:Maven 要求有父工程时先安装父工程;有依赖的工程时,先安装被依赖的工程。我们自己考虑这些规则会很麻烦。但是工程聚合之后,在总工程执行 mvn install 可以一键完成安装,而且会自动按照正确的顺序执行。文章来源地址https://www.toymoban.com/news/detail-725466.html

  • 配置聚合之后,各个模块工程会在总工程中展示一个列表,让项目中的各个模块一目了然。

到了这里,关于Maven进阶系列-继承和聚合的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Maven 依赖传递和冲突、继承和聚合

    1.1.1 概念         假如有三个 Maven 项目 A 、 B 和 C ,其中项目 A  依赖 B ,项目 B  依赖 C 。那么我们可以说 A 依赖 C 。也就是说,依赖的关系为: A—B—C , 那么我们执行项目 A 时,会自动把 B 和 C 都下载导入到 A 项目的 jar 包文件夹中,这就是依赖的传递性。 1.1.2 作

    2024年01月21日
    浏览(36)
  • Maven工程开发中的继承与聚合

    设置一个空的maven工程,工程里面只有pom文件,另外将这个工程的打包方式设置为pom。 在聚合工程里面添加聚合工程里面管理的模块 例如下面02工程继承上面的01工程,在02工程的pom文件中要配置要继承的父工程的依赖,并添加上相对路径。 此时,2、3、4中所用公共依赖的模

    2024年02月16日
    浏览(35)
  • Maven分模块-继承-聚合-私服的高级用法

    JavaWeb知识,介绍Maven的高级用法!!! Maven 是一款构建和管理 Java 项目的工具 ,在需要开发一些中大型的项目,此时仅凭前面所学习的 Maven 的基础知识就比较难以应对了。还需要学习 Maven 提供的一些高级的功能,这些功能在构建和管理 Java 项目的时候用的也是非常多的。

    2024年02月13日
    浏览(28)
  • Maven高级操作--分模块设计、聚合、继承和私服

    问题:当项目做大做强的时候,前面的基础Spring开发的框架都无法满足java大型项目的维护和复用,而且团队合作也会造成较大的困难。 所以就需要 分模块设计 :将项目按照功能拆分成若干个子模块,方便项目的 管理维护 、 扩展 ,也方便模块间的 相互调用 , 资源共享 。

    2024年02月11日
    浏览(33)
  • Maven工程 — 继承与聚合 相关知识点详解

     简介:这篇帖子主要讲解Maven工程中的继承与聚合的相关知识点,用简洁的语言和小编自己的理解,深入浅出的说明Maven工程的继承与聚合。 目录 1、继承 1.1 继承关系的实现 1.2 版本锁定 2、聚合 2.1 聚合方法 3、总结 3.1 作用与联系 4、私服 4.1 私服介绍 4.2 资源上传与下载

    2024年01月25日
    浏览(35)
  • Maven 聚合和继承 Inheritance vs Aggregation

    A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects. Examples for this is the build directory, which is  target ; the source directory, which is  src/main/java ; the tes

    2024年02月09日
    浏览(36)
  • 【Maven】依赖、构建管理 继承与聚合 快速学习(3.6.3 )

    Maven 文档 Maven 是一款为 Java 项目构建管理、依赖管理的工具( 软件 ),使用 Maven 可以自动化构建、测试、打包和发布项目,大大提高了开发效率和质量。 依赖管理工具 maven下载版本 使用:apache-maven-3.6.3 软件结构: 环境变量 命令行显示版本 mvn -v 设置: maven/conf/settings.x

    2024年02月21日
    浏览(33)
  • 【热门框架】Maven中聚合,继承指的是什么?有什么作用?

    Maven中的聚合和继承是两个重要的功能,用于管理多个项目的共同部分。 聚合 Maven中的聚合(Aggregation)指的是将多个子项目聚合成一个父项目的过程。聚合的语法如下: 在这个例子中, modules 元素用来指定子项目列表。在父项目中使用聚合的好处在于: 方便对多个子项目进

    2024年02月02日
    浏览(28)
  • JavaWeb开发06-原理-Spring配置优先级-Bean管理-SpringBoot原理-Maven继承和聚合-私服

    不同配置文件,配置同一个属性谁有效 propertiesymlyaml 命令行参数Java系统属性 项目打包后要改变属性: 红色是Java系统属性,绿色是命令行参数 ‘ 获取IOC容器:applicationContext就是IOC容器对象 IOC容器中bean是默认单例的只有一个,这样三个获取的bean是同一个 单例还是多例取决

    2024年04月23日
    浏览(37)
  • DAY07_Maven高级——分模块开发与设计&依赖管理&聚合与继承&属性管理&多环境配置与应用&私服

    问题导入 分模块开发对工程有什么好处? 模块拆分原则 目的:项目的扩展性变强了,方便其他项目引用相同的功能。 将原始模块按照功能拆分成若干个子模块,方便模块间的相互调用,接口共享 问题导入 一个完整的工程依据什么来进行模块的拆分? 2.1 创建Maven模块 2.2 书

    2024年02月09日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包