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>
相关推荐
这里是maven继承与聚合代码。
maven本地缓批量存上传到Nexus Repository Manager的脚本,本地仓库从远程仓库拉取的缓存文件上传的本地私服,只要将本文件添加到缓存文件的一级目录然后运行该脚本文件即可,传入参数包含私服的url以及私服仓库的...
这是一个maven依赖+继承+聚合的小例子,用来参考和学习maven
一个简单的maven聚合,继承示例。parent为父目录,account-email,account-persist为子项目,继承了parent的pom.xml文件
maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装...
Maven就可以替我们自动的将当前jar包所依赖的其他所有jar包全部导入进来,无需人工参与,节约了我们大量的时间和精力。用实际例子来说明就是:通过Maven导入commons-fileupload-1.3.jar后,commons-io-2.0.1.jar会被...
主要介绍了IDEA 重新导入依赖maven 命令 reimport的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
maven安装以及eclipse配置maven
在javaweb高速发展的今天,我们软件设计人员往往会用很多种...Maven有很好的依赖管理系统(Dependency Management System)和项目生命周期的管理(Project Leftcycle),而其中的依赖管理是本文阐述和做出实例的重点。
一个 maven 项目可以继承另一个 maven 的依赖, 称为子项目 父项目 使用场景: 多个子项目都需要某些依赖, 就可以把子项目共同的依赖抽取到父项目中, 子项目通过继承得到这些依赖, 这样也更好的来管理(比如升级, 删除...
maven 依赖,聚合,继承,插件运行,已经说明的简单小案例
主要介绍了maven继承父工程统一版本号的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
该资源包含了一个maven聚合和继承的简单demo,可以参考博客http://blog.csdn.net/mockingbirds/article/details/47374127
maven配置过程以及简单的helloworldmaven配置过程以及简单的helloworldmaven配置过程以及简单的helloworld
Idea 导入Maven项目,在加载pom.xml文件内容时,弹窗提示错误信息 Unable to import maven project的解决方案。文档是包含在操作时遇到的问题以及问题的解决方案,搭配图片,图文并茂,方便读者准确定位到问题以及更...
Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令
用于对Maven聚合继承项目的版本号进行便捷变更的工具 在使用RPC框架的微服务架构中,为了让服务消费者实现像调用本地方法一样的透明的通过网络进行RPC,服务提供者一般会提供立面jar用于服务消费者集成并通过动态...
maven3.0maven3.0maven3.0maven3.0maven3.0maven3.0maven3.0maven3.0maven3.0maven3.0
Maven3实战笔记07——继承的介绍。
Maven3.9.4版本压缩包,仅供学习参考,更新版本请前往Maven官方下载;Maven3.9.4版本压缩包,仅供学习参考,更新版本请前往Maven官方下载;Maven3.9.4版本压缩包,仅供学习参考,更新版本请前往Maven官方下载;Maven...