Maven的继承以及import作用域
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作用域的更多相关文章
- 【maven】之使用import scope解决maven继承(单)问题
想必大家在做SpringBoot应用的时候,都会有如下代码: <parent> <groupId>org.springframework.boot</groupId> ...
- Maven的继承与聚合——多模块开发
一:Maven多模块项目,适用于一些比较大的项目,通过合理的模块拆分,实现代码的复用,便于维护和管理.尤其是一些开源框架,也是采用多模块的方式,提供插件集成,用户可以根据需要配置指定的模块. 二:继承 ...
- Maven的继承和聚合
Maven的继承和聚合子项目的pom文件里通过<parent>节点来继承父项目 <parent> <groupId>com.tykj</groupId> ...
- maven pom继承与聚合
一.POM聚合模块: 在分布式架构,分模块化开发中,每个某块可能都是一个单独的maven项目,能够独立的进行项目构架,当模块比较多时,可以使用maven聚合聚合项目来简化maven构建,一次构建多个项 ...
- Maven可继承的POM 元素
groupId :项目组 ID ,项目坐标的核心元素: version :项目版本,项目坐标的核心元素: description :项目的描述信息: organization :项目的组织信息: in ...
- 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 ...
- Maven项目继承与聚合
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6628534.html 一:继承 在Java编程中,如果多个类都使用到了相同的内容.方法时,我们可以用继承的方 ...
- maven(十)-继承
继承 如果项目划分了多个模块,都需要依赖相似的jar包,只需要创建一个父模块,在它的pom.xml文件中配置依赖jar包.功能模块只需要继承父模块,就可以自动得到其依赖jar包,而不需要在每个模 ...
- this关键字、static关键字、block块、封装类以及继承、import、修饰符的初步了解
this关键字 定义 在类的方法定义中使用this关键字代表使用该方法的引用. this即"自己",代表对象本身,谁调用代表谁.在成员方法中或构造器中隐式的传递. this的两种用 ...
随机推荐
- [MXNet逐梦之旅]练习一·使用MXNet拟合直线手动实现
[MXNet逐梦之旅]练习一·使用MXNet拟合直线手动实现 code #%% from matplotlib import pyplot as plt from mxnet import autog ...
- Java设计模式总结
什么是设计模式 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.通过对这些设计模式的合理使用能够是我们的系统更加的健壮. 六大设计原则 ...
- Spring中使用的设计模式
目录 Spring使用的设计模式 1.单例模式 2.原型模式 3.模板模式 4.观察者模式 5.工厂模式 简单工厂模式 工厂方法模式 6.适配器模式 7.装饰者模式 8.代理模式 9.策略模式 S ...
- 第5章 支持和咨询选项 - Identity Server 4 中文文档(v1.0.0)
我们为IdentityServer提供了多种免费和商业支持和咨询选项. 5.1 免费支持 免费支持是基于社区的,并使用公共论坛 5.1.1 StackOverflow StackOverflow 社区 ...
- C#单例模式的几种实现方式
一.多线程不安全方式实现 public sealed class SingleInstance { private static SingleInstance instance; private S ...
- c++中求数组长度
#include <iostream> using namespace std; template <class T> int getArrSize(T& arr){ ...
- JavaScript(三)
本文转载自:https://blog.csdn.net/xiaogeldx/article/details/85455011 JavaScript的math对象 math方法 sqrt:开方 abs: ...
- Dynamics 365 CE中AsyncOperationBase表记录太多,影响系统性能怎么办?
微软动态CRM专家罗勇 ,回复311或者20190311可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . 本文主要是根据微软官 ...
- Git:五、操作远程仓库
0.一般流程 1)自己新写:GitHub创建有README的库 -> clone到本地 2)修改已有:GitHub上fork别人的仓库 -> clone自己账号下的库到本地 1.创建库 右 ...
- ffmpeg错误码
以下ffmpeg错误代码及翻译是本人遇到或发现后整理出来的,不保证包含全部错误代码 EPERM(不允许操作,无相应权限) = -1 ENOENT(文件或目录不存在) = -2 ESRCH(线程不存在) ...