侧边栏壁纸
  • 累计撰写 1,975 篇文章
  • 累计创建 73 个标签
  • 累计收到 20 条评论

目 录CONTENT

文章目录

maven统一管理依赖&依赖继承&

猿哥
2021-11-28 / 0 评论 / 0 点赞 / 349 阅读 / 0 字

1、统一管理依赖

统一管理依赖jar包的版本,建议配置方式 (一般配置,在当前pom文件配置) 1 .使用properties标签,标签内使用自定义标签统一声明版本号.

<properties>
    <spring.version>4.3.13.RELEASE</spring.version>
</properties>
2 .在需要统一版本的位置,使用${自定义标签名}引用声明的版本号

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
</dependency>
3 .其实properties标签配合自定义标签声明数据的配置并不是只能用于声明依赖的版本号,凡是需要统一声明后再引用的场合都可以使用。

2、依赖继承

父工程统一管理依赖jar包的版本,各个子模块工程继承父模块(以junit为例)。

1.创建一个Maven工程做为父工程,注意:打包方式pom

2.父工程使用dependencyManagement标签声明jar包,此时并没有真正引用.

<!--配置依赖的管理-->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
3.在子工程中声明对父工程的引用

<!--子工程声明父工程-->
<parent>
    <groupId>com.shuai.maven</groupId>
    <artifactId>maventest</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <!--以当前文件为基准的父工程pom.xml文件的相对路径-->
    <relativePath>../pom.xml</relativePath>
</parent>
4.将子工程的坐标中与父工程坐标重复的内容删除:groupId、version等 5.在子工程中删除junit依赖的版本号部分,此时子工程是真正的引用junit


<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <scope>test</scope>
</dependency>
6.注意:配置继承后,要先安装父工程,再安装子工程

0
博主关闭了所有页面的评论