maven项目的继承

多个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属性。

posted @ 2020-04-28 22:29  护花使者  Views(472)  Comments(0Edit  收藏  举报