Maven的pom文件中可继承的元素包括:

  groupId:项目ID,项目坐标核心元素

  version:项目版本

  description:描述信息

  organization:组织信息

  inceptionYear:创始年份

  url:项目URL地址

  developers:开发者信息

  distributionManagement:项目部署配置

  issueManagement:项目缺陷跟踪系统信息

  ciManagement:项目持续集成系统信息

  scm:项目版本控制系统信息

  mailingLists:邮件列表

  properties:自定义属性

  dependencies:依赖配置

  dependencyManagement:依赖配置管理

  repositories:仓库配置

  build:构建信息,包括源码目录,输出目录,插件配置,插件管理配置

  reporting:项目的报告输出目录配置,报告插件配置。

大部分配置都是说明性质的信息配置,这里主要介绍依赖配置,依赖管理配置,插件配置,插件管理配置。示例如下:

  父项目配置文件:

<!-- 父项目本身除了一个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.cbm.stu</groupId>
<artifactId>maven-study-parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging> <!-- 父项目的packing必须为pom --> <name>maven-study-parent</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <!-- dependencies标签中的依赖会被子项目完全继承:即父项目中有的依赖都会出现在子项目中 -->
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
</dependencies> <!-- 依赖管理中声明的依赖并不会被子项目直接继承,在子项目中需要声明,但是只需要声明groupId和artifactId -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.0.0</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<!-- 插件管理,可供子项目继承 -->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

子项目配置:

<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标签声明继承的父项目信息 -->
<parent>
<groupId>com.cbm.stu</groupId>
<artifactId>maven-study-parent</artifactId>
<version>1.0.0</version>
<!-- 父项目的pom.xml相对于当前项目的位置 -->
<relativePath>../maven-study-parent/pom.xml</relativePath>
</parent> <!-- groupId可以继承,也可以覆盖,通常不需要覆盖 -->
<groupId>com.cbm.stud</groupId>
<!-- 不可继承,子项目坐标的关键属性 -->
<artifactId>maven-study-account</artifactId>
<version>1.0.2</version>
<packaging>jar</packaging> <name>maven-study-account</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <!-- 父项目中dependencies标签声明的依赖被直接继承,例如mybatis包 -->
<dependencies>
<!-- 继承自父项目的依赖,只需指明groupId和artifactId -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<!-- 继承自父项目的插件依赖,此处省略也可继承 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

使用import属性实现依赖的多继承:

父项目a:

<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.cbm.stu</groupId>
<artifactId>maven-parent-a</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging> <name>maven-parent-a</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>

父项目b:

<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.cbm.stu</groupId>
<artifactId>maven-parent-b</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging> <name>maven-parent-b</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

继承 a 和 b 的子项目:

<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.cbm.stu</groupId>
<artifactId>maven-study-mail</artifactId>
<version>2.1</version>
<packaging>jar</packaging> <name>maven-study-mail</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencyManagement>
<dependencies>
<!-- 此处继承了a 和 b 两个项目,type为pom,scope 为 import -->
<dependency>
<groupId>com.cbm.stu</groupId>
<artifactId>maven-parent-a</artifactId>
<version>1.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.cbm.stu</groupId>
<artifactId>maven-parent-b</artifactId>
<version>1.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
<!-- 从继承的父项目中继承依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

----------------------

The end!

Maven的继承以及import作用域的更多相关文章

  1. 【maven】之使用import scope解决maven继承(单)问题

    想必大家在做SpringBoot应用的时候,都会有如下代码: <parent> <groupId>org.springframework.boot</groupId> ...

  2. Maven的继承与聚合——多模块开发

    一:Maven多模块项目,适用于一些比较大的项目,通过合理的模块拆分,实现代码的复用,便于维护和管理.尤其是一些开源框架,也是采用多模块的方式,提供插件集成,用户可以根据需要配置指定的模块. 二:继承 ...

  3. Maven的继承和聚合

    Maven的继承和聚合子项目的pom文件里通过<parent>节点来继承父项目 <parent> <groupId>com.tykj</groupId> ...

  4. maven pom继承与聚合

    一.POM聚合模块: 在分布式架构,分模块化开发中,每个某块可能都是一个单独的maven项目,能够独立的进行项目构架,当模块比较多时,可以使用maven聚合聚合项目来简化maven构建,一次构建多个项 ...

  5. Maven可继承的POM 元素

    groupId :项目组 ID ,项目坐标的核心元素: version :项目版本,项目坐标的核心元素: description :项目的描述信息: organization :项目的组织信息: in ...

  6. SpringBoot2.0.2 不使用parent作为maven单继承方式操作 : org.springframework.boot : spring-boot-dependencies : 2.0.2.RELEASE

    1.pom配置方式 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...

  7. Maven项目继承与聚合

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6628534.html  一:继承 在Java编程中,如果多个类都使用到了相同的内容.方法时,我们可以用继承的方 ...

  8. maven(十)-继承

     继承 如果项目划分了多个模块,都需要依赖相似的jar包,只需要创建一个父模块,在它的pom.xml文件中配置依赖jar包.功能模块只需要继承父模块,就可以自动得到其依赖jar包,而不需要在每个模 ...

  9. this关键字、static关键字、block块、封装类以及继承、import、修饰符的初步了解

    this关键字 定义 在类的方法定义中使用this关键字代表使用该方法的引用. this即"自己",代表对象本身,谁调用代表谁.在成员方法中或构造器中隐式的传递. this的两种用 ...

随机推荐

  1. IDEA搭建Spring Boot项目

    所需工具 新建项目 创建一个login控制器 写入两个注释 import导入项会自动添加@RestController@RequestMapping(value = "/login" ...

  2. kubernetes的安装方法

    背景 自己学习k8s集群,无奈屌丝一枚,没钱配置vpn服务,安装k8s花费的时间太久了.为了小伙伴们可以快速安装k8s,我花了点时间整理了这篇博客,提供一个不用FQ就可以愉快安装k8s集群的方法. 主 ...

  3. UML简单介绍-如何看懂UML(一)

      在计算复杂的数学题时,我们必然会打草稿计算 在绘画课中,我们可以素描出来看到的事物 那么在程序设计中呢? 如何描绘传达你脑海中的关于这个程序 ,设计的蓝图草稿?   OOP的程序设计中,最多的自然 ...

  4. [四]基础数据概述之Byte详解

        Byte 基本数据类型byte  的包装类 Byte 类型的对象包含一个 byte类型的字段            属性简介   构造方法 Byte的构造方法也是有两种 可以通过基本类型byt ...

  5. 痞子衡嵌入式:第一本Git命令教程(1)- 准备(init/config/.gitignore)

    今天是Git系列课程第一课,痞子衡给大家要讲的是创建仓库的准备工作. 1.建仓库git init 第一步是创建一个空仓库,这是一切操作的前提. // 打开git bash命令行,切换到指定目录下 ja ...

  6. 【Angular专题】——(1)Angular,孤傲的变革者

    目录 一. 漫谈Angular 二. 如果你还在使用Angularjs 三. 我计划这样学习Angular技术栈 一. 漫谈Angular Angular,来自Google的前端SPA框架,与Reac ...

  7. 第一册:lesson 111.

    原文:The most expensive model. question:Can Mr.Frith buy the television on instalments? How does it wo ...

  8. win10 git bash 闪退

    使用ghost重装了win10 专业版后.安装git,尝试重装了n个版本的git,右键git bash here 直接闪退,直接进入安装目录打开git-bash.exe依旧闪退, git右键点击Git ...

  9. Oracle day03 连表查询

    为什么要表连接进行查询? 查询部门名称为SALES的员工信息 如何进行表的连接查询? 两种方式:Sql 1992 和sql1999 sql1992sql分类    1.笛卡尔积 (表乘表)    2. ...

  10. Flask 系列之 部署发布

    说明 操作系统:Windows 10 Python 版本:3.7x 虚拟环境管理器:virtualenv 代码编辑器:VS Code 实验目标 通过 Windows 的 WSL,将我们的项目网站部署到 ...