欢迎来到代码驿站!

当前位置:首页 >

Maven项目继承实现过程图解

时间:2020-08-05 13:00:02|栏目:|点击:

多个maven项目之间难免有重复的pom配置,重复的配置没必要重复写,maven提供了父子继承的关系,重复的依赖直接放在父项目的pom中。

所以不希望每个开发者随意定义maven版本依赖,可以在父项目中进行说明,然后子项目沿用即可。

idea创建父项目(这是一个父项目,也是一个空项目,只需要pom.xml,编写相关的依赖, 父项目必须用pom打包的方式):

编辑父项目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.linewell</groupId>
  <artifactId>maven-parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <!--父项目必须是pom-->
  <packaging>pom</packaging>

  <!--定义参数-->
  <properties>
    <common.version>2.6</common.version>
    <spring.version>4.3.6.RELEASE</spring.version>
  </properties>

  <!--这边的依赖子项目会继承-->
  <dependencies>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>${common.version}</version>
    </dependency>
  </dependencies>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>spring-context-support</groupId>
        <artifactId>org.springframework</artifactId>
        <version>${spring.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
  
</project>

这边需要说明下,dependencyManagement,这边的依赖不会被继承,如果子项目导入了这个依赖,可以不用写版本号,会以父项目的为主,因为有的子项目不一定会用父项目中的所有依赖。个别子项目依赖到的包可以放在这里,然后不需要写版本号,会自动引用父项目。

创建一个子项目,编辑子项目的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.linewell</groupId>
  <artifactId>maven-children</artifactId>
  <version>1.0-SNAPSHOT</version>

  <parent>
    <groupId>com.linewell</groupId>
    <artifactId>maven-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../mavenparent/pom.xml</relativePath>
  </parent>

</project>

可以看到commons-io进来了,spring-context-support没进来。

我现在不添加spring-context-support的版本,然后看下结果,是会以父项目的版本为主。可以看到如下引入的也是父项目中的4.3.6

<?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>

  <parent>
    <groupId>com.linewell</groupId>
    <artifactId>maven-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../mavenparent/pom.xml</relativePath>
  </parent>

  <groupId>com.linewell</groupId>
  <artifactId>maven-children</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
    </dependency>
  </dependencies>
</project>

那么问题来了,如果子项目指定了版本会怎么样?

编辑子项目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>

  <parent>
    <groupId>com.linewell</groupId>
    <artifactId>maven-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../mavenparent/pom.xml</relativePath>
  </parent>

  <groupId>com.linewell</groupId>
  <artifactId>maven-children</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.5</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>5.2.5.RELEASE</version>
    </dependency>
  </dependencies>

</project>

ps:如果父项目中执行了mvn install安装到了本地仓库,然后子项目中引入父GAV的时候可以不用写路径relativePath属性。

上一篇:vue data变量相互赋值后被实时同步的解决步骤

栏    目:

下一篇:R语言ggplot2边框背景去除的实现

本文标题:Maven项目继承实现过程图解

本文地址:http://www.codeinn.net/misctech/3107.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有