maven项目的继承

摘要:
多个maven项目之间难免存在重复的pom配置。重复的配置不需要重复写入。maven提供了父子继承关系。重复的依赖项直接放置在父项的pom中。因此,您不希望每个开发人员都随意定义maven版本依赖关系。您可以在父项目中解释它,然后在子项目中使用它--这一侧的依赖子项目将继承--˃commons-iocommon-io${common.version}spring-contextsupport˂artifactId=org。springframework${spring.version}需要说明的是,依赖项管理这一侧的依赖项不会被继承。如果子项目导入此依赖项,则无法写入版本号,但将以父项目为准,因为子项目不一定使用父项目中的所有依赖项。

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

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

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

maven项目的继承第1张

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

maven项目的继承第2张

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

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

maven项目的继承第3张

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

免责声明:文章转载自《maven项目的继承》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇卡顿问题PyQt5中文手册下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

pom配置文件的使用

1pom文件的作用 在maven项目中,通过maven来获取和管理第三方的资源。每个maven 项目默认是有一个pom文件的。该文件是用来告诉maven当前项目中需要 使用的资源的,该文件会被maven自动加载识别。我们需要在pom配置文 件中声明需要使用的资源的坐标即可。 注意: 当pom文件被修改的时候,maven会自动的重新加载pom文件,并 根据...

解决springboot2.x项目添加@EnableDiscoveryClient注解找不到的问题

环境: IDEA maven3.0 springboot2.2.0 特别注意springboot的版本,1.0和2.0有很大区别 解决办法: pom添加: <dependency>            <groupId>org.springframework.cloud</groupId>            <...

Maven创建父子工程(简单、完整)

Eclipse创建Maven父子工程(书中有的叫聚合工程,因为看了网上很多教程,实际操作总会有这样那样的小问题,有时候包结构还不对,创建成功后索性就自己写一篇帮助小白,有不对请指教) 1 file-new-other-maven Project 2 点击第一步next----先创建父工程(网上很多教程写的创建父工程不选的第一个,之后创建出来再删除掉,但是...

Spring boot(4)-应用打包部署

摘自:http://blog.csdn.net/hguisu/article/details/51072683 1、Spring Boot内置web springBoot 其默认是集成web容器的,启动方式由像普通Java程序一样,main函数入口启动。其内置Tomcat容器或Jetty容器,具体由配置来决定(默认Tomcat)。当然你也可以将项目打包成...

Maven之依赖关系

在maven的管理体系中,各个项目组成了一个复杂的关系网,但是每个项目都是平等的,是个没有贵贱高低,众生平等的世界,全球每个项目从理论上来说都可以相互依赖。就是说,你跟开发Spring的大牛们平起平坐,你的项目可以依赖Spring项目,Spring项目也可以依赖你的项目(虽然现实中不太会发生,你倒贴钱人家也不敢引用)。 项目的依赖关系主要分为三种:依赖,继...

Maven知识整理

一、概念: Maven是一个项目管理工具,它包含了一个项目对象模型(Project Object Model),一组标准集合,一个项目生命周期(Project Lifecycle),一个依赖管理系统(Dependency Management System),和用来运行定义在生命周期阶段(phase)中插件(plugin)目标(goal)的逻辑。 1、项目...