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>