微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

maven多继承关系

在 Maven 中对于继承采用的也是单一继承,也就是说一个子项目只能有一 个父项目,但是有的时候我们项目可能需要从更多的项目中继承,那么我们可以 在子项目中通过添加<dependencyManagement>标记来实现多继承。在子项 目的<dependencyManagement>中每个<dependency>标记一个父工程 定义,同时还需要添加<type>标记,值为 pom。添加<scope>标记,值为 import。 1.多继承方法

image

点击查看代码
<?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>
    <!--    继承父类的坐标 父项目 A-->
    <parent>
        <groupId>com.bjsxt</groupId>
        <artifactId>parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.bisxt</groupId>
    <artifactId>child</artifactId>
    <version>1.0-SNAPSHOT</version>
<!--    通过dependencyManagement标签添加父类-->
    <dependencyManagement>
        <dependencies>
<!--            父项目B-->
            <dependency>
                <groupId>com.bisxt</groupId>
                <artifactId>parent_b</artifactId>
                <version>1.0-SNAPSHOT</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
    </dependencies>

</project>

2.多继承二

image

点击查看代码
<?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>
    <!--    继承父类的坐标 父项目 A-->
<!--    <parent>-->
<!--        <groupId>com.bjsxt</groupId>-->
<!--        <artifactId>parent</artifactId>-->
<!--        <version>1.0-SNAPSHOT</version>-->
<!--    </parent>-->
    <groupId>com.bisxt</groupId>
    <artifactId>child</artifactId>
    <version>1.0-SNAPSHOT</version>
<!--    通过dependencyManagement标签添加父类-->
    <dependencyManagement>
        <dependencies>
<!--            父项目B-->
            <dependency>
                <groupId>com.bisxt</groupId>
                <artifactId>parent_b</artifactId>
                <version>1.0-SNAPSHOT</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.bjsxt</groupId>
                <artifactId>parent</artifactId>
                <version>1.0-SNAPSHOT</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
    </dependencies>

</project>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐