Maven知识点汇总

一、Maven的简介

       Maven 工具是基于 POM(Project Object Model,项目对象模型)模式实现的。Maven 将每个项目都看作一个对象,对象(项目)和对象(项目)之间是有关系的。关系包含 了:依赖、继承、聚合, 实现Maven 项目可以更加方便的实现导 jar 包、拆分项目等功能。

二、Maven仓库的介绍

仓库:顾名思义就是存储东西的地方,而maven仓库里面存储的就是一个一个的构件(可以理解为jar包),仓库中的每一个构件都有一个唯一的坐标,我们可以通过这个坐标来找到对应的构件。对于Maven来说,仓库分为两种:本地仓库和中央仓库。

1. 两种仓库介绍
  • 中央仓库:互联网上的服务器,是 Maven 提供的最大的仓库,里面拥有最全的 jar包资源。默认是 Maven 自己的网络服务器,但是由于访问速度较慢,我们一般都配置成国内的镜像中央仓库如阿里镜像或者是华为镜像。
  • 本地仓库:指用户电脑中的文件夹,该文件夹中用来存储自己从中央仓库下载的构件(jar 包)。
2. 仓库的访问优先级

       首先,在我们要使用某一个资源的时候,maven会先从我们的本地仓库中寻找,如果本地仓库中没有这个资源,那么它会将资源从中央仓库下载到我们的本地仓库,然后使用;如果本地仓库已经有了,就直接使用。
在这里插入图片描述

3.仓库的配置
  • 配置本地仓库: 在settings.xml中的< settings >的标签体中加上如下配置:
<localRepository>本地仓库的路径</localRepository>

比如我的就是这样的

<localRepository>D:\apache-maven 3.8.1\mvn_repository</localRepository>
  • 配置镜像仓库: 在国内直接连接中央仓库下载依赖,由于一些特殊原因下载速度非常慢。这时,我们可以使用阿里云提供的镜像http://maven.aliyun.com/nexus/content/groups/public/来 替换中央仓库 http://repol.maven.org/maven2/。修改 maven的setting.xml 文件,具体内容如下:
这些代码加在< settings >标签下的< mirrors >中
<mirror>
    <!-- 指定镜像 ID -->
    <id>nexus-aliyun</id> 
    <!-- 匹配中央仓库。-->
    <mirrorOf>central</mirrorOf> 
    <!-- 指定镜像名称 -->
    <name>Nexus aliyun</name>
    <!-- 指定镜像路径 -->
    <url>http://maven.aliyun.com/nexus/content/groups/public</url> 
</mirror>

三、Maven工程类型

  • POM工程: POM工程是逻辑工程。用在聚合工程中,或者用在父级工程用来做jar包的版本控制。
  • JAR工程: 创建一个 Java Project,在打包时会将项目打成 jar 包。
  • WAR工程: 创建一个 Web Project,在打包时会将项目打成 war 包。

四、POM模型

1. 依赖关系

通俗理解:依赖谁就是将谁的 jar 包添加到本项目中。可以依赖中央仓库的 jar,也可以依赖当前开发中其他项目打包后的 jar 包。

在 pom.xml 文件根元素 project 下的 dependencies 标签中,配置依赖信息,可以包含多个 dependence 元素,以声明多个依赖。每个依赖 dependence 标签都应该包含以下元素:groupId, artifactId, version(依赖的坐标),Maven 根据坐标才能找到需要的依赖。

例如:

<dependencies> 
  <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId>         
      <version>5.2.4.RELEASE</version> 
  </dependency> 
</dependencies>
  • 依赖的传递性: 如果 A 依赖了B,那么 C 依赖 A 时会自动把 A 和 B都导入进来。
  • 依赖相同资源的依赖原则: 如果A同时依赖了B和C,而B和C又都依赖了另一个相同的构件,这可能会导致版本冲突的问题,此时maven会通过自身的算法帮助我们选择其中的一个来使用。
  • 排除依赖: exclusions:用来排除传递性依赖其中可配置多个 exclusion标签,每个exclusion标签里面对应的有groupId, artifactId 两项基本元素。
    例如:
<dependency> 
   <groupId>org.springframework</groupId> 
   <artifactId>spring-context</artifactId>    
   <version>5.2.4.RELEASE</version> 
   <exclusions> 
     <exclusion> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-aop</artifactId> 
     </exclusion> 
    </exclusions>
</dependency>
  • 依赖范围:
  1. compile :这是默认范围。如果没有指定,就会使用该依赖范围。表示该依赖在编译和运行时生效。在项目打包时会将该依赖包含进去。
  2. provided:可以参与编译,测试,运行等周期,但是不会被打包到最终的 artifact 中。
  3. runtime:runtime 范围表明编译时不需要生效,而只在运行时生效。
  4. test:test 范围表明使用此依赖范围的依赖,只在编译测试代码和运行测试的时候需要,应用 的正常运行不需要此类依赖。
  • 依赖管理: Maven 提 供 了 一 个 机 制 来 集 中 管 理 依 赖 信 息 ,叫做依赖管理元素< dependencyManagement >。假设你有许多项目继承自同一个公有的父项目,那可以把所有依赖信息放在一个公共的 POM 文件中并且在子 POM 中简单的引用该构件即可。(注:在< dependencyManagement >标签中的依赖只是相当于声明,没有真正的引入该依赖)

例如下面的案例:

这里是在父项目中的< dependencyManagement >中声明所有要用的构件,他有三个子模块

<?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.dubboTest</groupId>
    <artifactId>DubboTest_parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>user_api</module>
        <module>user_consumer</module>
        <module>user_provider</module>
    </modules>
    <packaging>pom</packaging>

    <!--声明版本信息-->
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <dubbo.spring.starter.version>2.7.6</dubbo.spring.starter.version>
        <dubbo.registry.zookeeper.version>2.7.6</dubbo.registry.zookeeper.version>
        <mybatis.spring.starter.version>2.2.0</mybatis.spring.starter.version>
        <mysql.connector.version>5.1.37</mysql.connector.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!--Dubbo Starter Dependency Version:2.7.6-->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>${dubbo.spring.starter.version}</version>
            </dependency>
            <!--Zookeeper Registry Dependency Version:2.7.6-->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-registry-zookeeper</artifactId>
                <version>${dubbo.registry.zookeeper.version}</version>
            </dependency>
            <!--Mybatis Starter Dependency Version:2.1.2-->
            <dependency> <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis.spring.starter.version}</version>
            </dependency>
            <!--MySQL Driver Dependency Version:5.1.38-->
            <dependency> <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.connector.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

这里是其中的一个子模块,只有用到的时候才在相应位置进行添加,而且添加的构件不需要添加版本,因为已经在父项目中进行了统一的声明

<?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">
    <parent>
        <artifactId>DubboTest_parent</artifactId>
        <groupId>com.dubboTest</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>user_api</artifactId>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <!--Zookeeper Registry Dependency-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-zookeeper</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--Dubbo Starter Dependency-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>com.dubboTest</groupId>
            <artifactId>pojo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

综上所述,父项目的< dependencyManagement > 中只是对构件进行声明,如果子项目想要使用,还是需要去引入依赖,
< dependencyManagement >只是为了方便管理以及控制相应的版本

2.继承关系
  • 简介: Maven 中的继承跟 Java 中的继承概念一样,需要有父项目以及子项目。我们可以将项 目中的依赖和插件配置提取出来在父项目中集中定义,从而更方便的管理项目的依赖以及插 件。注意父项目类型一定为 POM 类型。
  • 继承的优点:
  1. 依赖或插件的统一管理(在 parent 中定义,需要变更 dependency 版本时,只需要修改一处)。
  2. 代码简洁(子 model 只需要指定 groupId,artifactId 即可)
  3. dependencyManagement 是 “ 按 需 引 入 ” , 即 子 model 不 会 继 承 parent 中 dependencyManagement 所有预定义的 dependency。
  • Maven中的多继承: 在 Maven 中对于继承采用的也是单一继承,也就是说一个子项目只能有一 个父项目,但是有的时候我们项目可能需要从更多的项目中继承,那么我们可以 在子项目中通过添加标记来实现多继承。在子项目的中每个标记就一个父工程定义,同时还需要添加标记,值为 pom。添加标记,值为 import。
<dependencyManagement> 
   <dependencies> 
     <!--父项目 a--> 
     <dependency> 
        <groupId>com.aaa</groupId>      
        <artifactId>parentA</artifactId> 
        <version>1.0</version>
        <type>pom</type> 
        <scope>import</scope>
     </dependency> 
     <!--父项目 b--> 
     <dependency> 
        <groupId>com.bbb</groupId>      
        <artifactId>parentB</artifactId> 
        <version>1.0</version> 
        <type>pom</type> 
        <scope>import</scope>
     </dependency> 
    </dependencies> 
</dependencyManagement>
3.聚合关系

       Maven 的聚合特性可以帮助我们把多个项目基于多个模块聚合在一起,这样能够更加 方便项目的管理。
       前提:继承。
       聚合包含了继承的特性。
       聚合时多个项目的本质还是一个项目。这些项目被一个大的父项目包含。且这时父项目类型为 pom 类型。同时在父项目的 pom.xml 中出现< modules >表示包含的所有子模块。
       即使在 idea 中,也可以使用聚合在一个窗口创建多个项目。

五、Maven常用命令

  • clean: 清除已编译信息。删除工程中的 target 目录。
  • compile: 只编译。相当于javac 命令。
  • package: 打包。 包含编译,打包两个功能。
  • install: 本地安装。包含编译,打包,安装到本地仓库。
  • deploy: 远程部署命令。

六、Maven 项目名规范

  • groupId: 一般是公司域名反转。
    例如:baidu.com—>com.baidu
  • artifactId: 一般为 项目名-模块名
    例如:spirng-mvn、spring-core 等。
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

影佑佑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值