Maven高级

[Maven 高级

一、Maven基础回顾

1.Maven基础回顾
  • Maven的标准结构是如何?

    创建 maven工程默认packaging为jar、创建web工程packaging 为war

  • Maven怎么添加依赖?

    dependencies中添加依赖坐标GAV=groupId+artifactId+version来找到本地仓库中jar包

    pom.xml文件中添加  
     <dependencies>
          <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.13.2</version>
                <!--依赖在生命周期中作用范围-->
                <scope>test</scope>
          </dependency>
     </dependencies>
    
  • Maven的的生命周期有哪些阶段?

    maven的3套生命周期 =clean清除生命周期+default默认的生命周期+site生成站点生命周期

    大体上:清除、编译、测试、打包、安装、部署

    ​	[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dCv5V88x-1645080006210)(img/image-20211217130931504.png)]

2.Maven高级用法介绍

既然POM(Project Object Model)是项目对象模型管理,到底还可以管理哪些呢?

  • 模块管理:把项目拆成多个模块分层开发
  • 依赖管理:依赖继承、依赖传递(封装)关系
  • 插件管理:揭秘maven整个生命周期干活的不是maven本身,而是身后这些插件。
  • 环境管理:maven在开发中对项目建立了标准化,那么实际开发环境(开发、测试、发布)由怎么管理呢?
  • 仓库管理:下载maven中央仓库比较慢,能不能换其他仓库?

二、Maven模块管理

1.大项目拆分成多个模块开发有什么好处?
  • 场景:在Spring Framework框架中为什么会有多个模块(jar),而不是一个模块(jar)呢?

在这里插入图片描述

​ 除了熟悉的Spring Framework框架之外,在实际开发中也会面临大型复杂的项目,例如如下项目功能架构:

​ https://pip.itcast.cn/lkd2?javaeezly%24jingjiaczpz-PC-1

  • 分层开发: 当项目规模变大,需要按业务,按功能进行拆分多个模块,这样的好处是

    • 增强代码的复用性

      一些通用的工具类、实体类可以抽取到独立的模块(项目),进行重用。例如:spring-context、spring-test很多spring项目中都有该模块。

    • 方便于分工开发

      按业务划分模块可以让开发人员编写代码更为独立,互不干扰。

2.Maven是如何实现项目分层开发?
  • Maven分层开发步骤

    • 步骤1 - 创建01_maven_parent模块

    • 步骤2 - 创建maven_child1模块,maven_child2,分别在parent中选择01_maven_parent作为父模块
      image-20211217215317442

      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jqlVJr9O-1645080006211)(img/image-20211217215547360.png)]

    • 步骤3-分别查看01_maven_parent、maven_child1、maven_child2中的pom文件

      • 01_maven_parent中的pom文件:打包为pom

        <?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.itheima</groupId>
            <artifactId>01_maven_parent</artifactId>
            
            <packaging>pom</packaging>
            <version>1.0-SNAPSHOT</version>
            <modules>
                <module>maven_child1</module>
                <module>maven_child2</module>
            </modules>
        
            <properties>
                <maven.compiler.source>8</maven.compiler.source>
                <maven.compiler.target>8</maven.compiler.target>
            </properties>
        
        </project>
        
      • maven_child1和maven_child2中的pom文件:

        <?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>01_maven_parent</artifactId>
                <groupId>com.itheima</groupId>
                <version>1.0-SNAPSHOT</version>
            </parent>
            <modelVersion>4.0.0</modelVersion>
            ..
        
        </project>
        

        说明:

        1. packaging为pom说明该工程为聚合工程,通过来聚合对应的2个子的模块。拆分出来的独立模块是无法完成项目要求的,只有整合在一起才能实现最终项目功能 ,模块与模块之间关系就是聚合

          image-20211217145944668 image-20211217150020958
        2. 通过继承父模块(pom文件声明的依赖、版本控制、插件等都可以继承)。 模块与模块之间对于有重复的,可以通过继承来解决,模块与模块之间关系就是继承

    • 步骤4-测试,感受聚合作用,对01_maven_parent执行如下操作,看控制台打印信息

      • 对父模块进行compile、test、package、installss操作,maven_child1和maven_child2都会执行

        image-20211217222126858

        [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-t1p618Oq-1645080006211)(img/image-20211217223117338.png)]

    • 步骤5-测试,创建maven_child3 maven web模块感受继承、聚合作用

      • 创建maven_chil3 maven web模块继承了01_maven_parent,并添加javax.servlet-api

        image-20211217232616474

        <?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>
                <artifactId>01_maven_parent</artifactId>
                <groupId>com.itheima</groupId>
                <version>1.0-SNAPSHOT</version>
            </parent>
        
            <artifactId>maven_child3</artifactId>
            <packaging>war</packaging>
        
            <name>maven_child3</name>
        
            <properties>
                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
            </properties>
        
            <dependencies>
                <dependency>
                    <groupId>javax.servlet</groupId>
                    <artifactId>javax.servlet-api</artifactId>
                    <version>3.0.1</version>
                    <scope>provided</scope>
                </dependency>
            </dependencies>
        </project>
        
      • 在01_maven_parent中的pom.xml中添加tomcat7-maven-plugin插件

        <!--01_maven_parent中pom.xml文件-->
        <build>
                <plugins>
                    <!--tomcat7-->
                    <plugin>
                        <groupId>org.apache.tomcat.maven</groupId>
                        <artifactId>tomcat7-maven-plugin</artifactId>
                        <configuration>
                            <uriEncoding>utf-8</uriEncoding>
                            <port>80</port>
                            <path>/</path>
                        </configuration>
                    </plugin>
                </plugins>
        </build>
        
      • 启动01_maven_parent,控制台显示的是作用于maven_child3的war包 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qeo3xXyi-1645080006212)(img/image-20211220083805247.png)]

  • Maven分层开发总结(项目模块之间的关系)

    • pom类型的父模块中通过合并了子模块,开发玩了所有模块,合并成完成项目(多个模块合并)之后 ,对项目操作意味着对所有模块一起操作,这样提高了开发效率。
    • 子模块通过标签继承了父模块 ,上例中子模块仅继承了版本号,还有依赖等资源都可以继承。

三 、Maven依赖管理

1.依赖传递
  • 什么是依赖的传递?

    • 场景:我们之前学习spring框架的时候,会向maven模块中添加spring依赖,例如:在maven_child1中加入spring-context依赖,观察maven工具窗口:添加的spring-context依赖结果也顺便也把spring-bean等显示出来了。这个在maven中叫“依赖的传递”。

          <dependencies>
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-context</artifactId>
                  <version>5.2.17.RELEASE</version>
              </dependency>
          </dependencies>
      

      image-20211219211345960

  • 为什么会产生依赖传递?

    • 将maven_child1打包成jar然后在maven_child2中添加maven_child1的依赖

      <!--在maven_child2中pom文件添加maven_child1依赖-->
      <dependencies>
          <dependency>
             <groupId>com.itheima</groupId>
             <artifactId>maven_child1</artifactId>
             <version>1.0-SNAPSHOT</version>
          </dependency>
      </dependencies>
      

    ​ 那这个时候会发现加入的maven_child1也顺便传递了spring-context依赖

    ​ [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qUHeIRhA-1645080006212)(img/image-20211219211605644.png)]

    • 总结
      • 所谓依赖其实是通过项目工程模块打包过来到仓库中,通过坐标引入进来的。只不过spring-context是打包上传到了maven中央仓库中了,供全球所有开发人员使用。

      • 在B模块中引入A依赖,A依赖所关联的C、D依赖也会随着A引入过来。这种现象就是依赖传递。

        可以理解为依赖的封装。

2.依赖冲突
  • 依赖可以传递,那传递过程中是否存在问题呢?

    • 场景:在maven_child3中添加dubbo2.5.4版本(该依赖是阿里提供的微服务框架,大家不需要知道该依赖是干什么 )之后,观察maven 窗口中该依赖有什么问题 ?

      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>dubbo</artifactId>
          <version>2.5.4</version>
      </dependency>
      
      image-20211220094924768
    • maven是怎么选中同一个版本多个相同的依赖呢?

      就近优先原则!以上例dubbo传递spring-bean来说明

      第1条路径:dubbo2.5.4-传递->spring-context4.3.10-传递->spring-beans4.3.10   (经过了3级才找到传递spring-bean4.3.10版本)
      
      第2条路径:dubbo2.5.4-传递->spring-beans4.3.10  (这里经过了2级就找到了spring-bean4.3.10)
      
      结果:很显然第2条路径更短能够找到spring-bean ,故而会选择dubbo-->spring-beans
      
    • maven是怎么确定不同版本多个相同的依赖呢?

      同一个版本相同依赖,maven可以自己确定,那不同的版本相同类型依赖,maven能够确定选择哪个依赖么?例如:在dubbo2.5.4 基础上添加 spring-context5.2.17.RELEASE

      <!--通过就近原则,越早传递越早确定版本-->
      <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>dubbo</artifactId>
         <version>2.5.4</version>
      </dependency>
      <dependency>
      	<groupId>org.springframework</groupId>
      	<artifactId>spring-context</artifactId>
          <version>5.2.17.RELEASE</version>
      </dependency>
      

      在maven窗口中能够看到maven并没有对版本冲突进行解决

      image-20211220101229563

      ​ [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lkl6grVC-1645080006212)(img/image-20211220101307381.png)]

      • 解决方式一:排除

        <!--在dubbo中排除低版本的spring-context-->
        <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>dubbo</artifactId>
             <version>2.5.4</version>
             <exclusions>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                         <artifactId>spring-context</artifactId>
                    </exclusion>
             </exclusions>
        </dependency>
        <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-context</artifactId>
             <version>5.2.17.RELEASE</version>
        </dependency>
        
        image-20211220101717110

        说明:排除具体依赖,需要我们先分析,然后一个个手动排除,这种方式开发效率比较低!还有一种一劳永逸的方式:锁定版本。

      • 解决方式二:锁定版本 在01_maven_parent中添加

        <!--锁定依赖的版本-->
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context</artifactId>
                    <version>5.2.17.RELEASE</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-beans</artifactId>
                    <version>5.2.17.RELEASE</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-web</artifactId>
                    <version>5.2.17.RELEASE</version>
                </dependency>
            </dependencies>
        </dependencyManagement>
        
        image-20211220102322205

        说明 :推荐使用标签锁定冲突版本。上例的版本5.2.17.RELEASE如果哪天想升级到5.3版本,也可以解耦优化以上写法:

        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
            <!--定义spring.version变量来控制版本值,这样可以解耦,变量名可以随便取-->
            <spring.version>5.2.17.RELEASE</spring.version>
        </properties>
        <!--锁定依赖的版本-->
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-beans</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-web</artifactId>
                    <version>${spring.version}</version>
                </dependency>
            </dependencies>
        </dependencyManagement>
        
3.依赖继承
  • 什么是依赖继承?

    依赖可以封装,那依赖能不能继承呢?有点像面向对象的特点,下面我们通过实验来感受一下依赖的继承关系。在01_maven_parent中添加junit依赖。

    <dependencies>
         <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.13.2</version>
              <scope>test</scope>
         </dependency>
    </dependencies>
    
  • 观察maven_child1、maven_child2、maven_child3依赖库,3个子模块都继承了junit

    image-20211220084509685

    总结:在01_maven_parent 父模块中声明的依赖、插件、版本锁定等在子模块可以直接继承。

四 、Maven插件管理

1.插件理解
  • Maven中插件怎么配置?

    <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                    <configuration>
                        <port>8080</port>
                        <path>/</path>
                        <uriEncoding>UTF-8</uriEncoding>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    

    ​ [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NugiN7EY-1645080006213)(img/image-20211217153804811.png)]

  • Maven中生命周期和插件的关系是什么?

    image-20211217154813907

image-20211217154932607

​ 故而,创建的每一个maven工程完整有效的pom.xml文件中包含了生命周期要使用到的插件:

image-20211217155845957

​ 总结: maven设计好了各种生命周期后,maven不提供具体执行,插件负责在各个生命周期具体执行目标 (goal);

  • Maven中常用的插件有什么作用?

    • tomcat7-maven-plugin: 构建tomcat服务器,用来部署web项目

    • maven-compile-plugin:编译Java代码成class文件

    • maven-resources-plugin: 默认会将src/main/resources 下的资源(*.xml, *.properties 等)拷贝到 classes 目录 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4deBLL8B-1645080006213)(img/image-20211217161327447.png)]

    • maven-jar-plugin:将指定的文件打包并命名jar

      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-C3vERz54-1645080006213)(img/image-20211217161248504.png)]

2.常用插件设置
  • maven-compiler-plugin插件设置:Java源码和编译版本
    • 场景: maven-compiler-plugin 默认的 JDK 版本为 1.5,此时 JDK 1.5 是不可能将带有 JDK 1.8 特性的代码编译通过的,故而尽可能设置成相同编译版本。

      image-20211220111353094

    • 解决:

      <?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">
      	
          ...
      
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-compiler-plugin</artifactId>
                      <version>3.1</version>
                      <configuration>
                          <source>8</source>
                          <target>8</target>
                      </configuration>
                  </plugin>
              </plugins>
          </build>
      </project>
      

      设置完之后,再查看模块编译版本变成了8:

      image-20211220111537854
      <!--上述插件的简化形式如下-->
      <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
      </properties
      

      参考文档:- https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html

  • maven-resources-plugin插件设置:设置拷贝资源的文件编码、拷贝文件动态替换文件变量
    • 场景1:在 maven 编译时,会提示如下警告错误,该如何解决?

      idea控制台警告: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1lWpOtkH-1645080006214)(img/image-20211220103821470.png )]

      Dos命令窗口警告:

      image-20211220104709271

      该警告意思是maven-resource-plugin在复制文件过程中,未对文件进行编码,故而会采用平台自己编码来处理文件 。例如:使用idea,采用idea的UTF-8编码来处理文件,使用命令窗口则使用GBK来编码文件。这样就会出现差异化,如果我们开发和复制文件编码不一致 就会出现文件乱码。故而需要统一编码 。

    • 解决1:在01_maven_parent中添加project.build.sourceEncoding,这样所有子模块就可以统一编码了

      <?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">
      	
      	...
      
          <properties>
              <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
          </properties>
      	
      	...
          
      </project>
      
    • 场景2:在拷贝资源文件时,希望将文件内 ${} 占位符(maven中el表达式)替换为 maven 中定义的【属性】,例如:
      maven_child1 模块的 src/main/resources/my.properties 内容如下

      host=${host}
      
    • 解决2:设置拷贝资源过滤使用值替换${host}变量

      <?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">
          
      	...
      
          <properties>
              <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
              <host>127.0.0.1</host>
          </properties>
      
          <build>
      		...
              <resources>
                  <resource>
                      <directory>src/main/resources</directory>
                      <!--maven-resources-plugin 的资源拷贝且 `<filtering>true</filtering>` 时,替换应用资源文件中的 ${}-->
                      <filtering>true</filtering>
                  </resource>
              </resources>
          </build>
      </project>
      
      mvn compile -Dhost=192.168.25.1
      

      参考:https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

  • maven-war-plugin插件设置:设置缺失的web.xml

    • 场景:在maven_child3,如果没有web.xml,例如使用了servlet3.0+就可以不需要web.xml,那么打成war的时候会Error,这个时候怎么办 ?

      [ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project maven_child3: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]
      
    • 解决:

      <build>  
          <plugins>  
              <plugin>  
                  <groupId>org.apache.maven.plugins</groupId>  
                  <artifactId>maven-war-plugin</artifactId>  
                  <version>2.3</version>  
                  <configuration>  
                      <!--没有web.xml依然可以打成war包-->
                      <failOnMissingWebXml>false</failOnMissingWebXml>  
                  </configuration>  
              </plugin>  
          </plugins>  
      </build>  
      

五、Maven开发环境管理

1.开发环境介绍
  • 开发工程师、测试团队、上线组长实际他们把代码部署在哪里呢?

    • 开发人员电脑本地环境local

    • 开发小组自测开发环境dev

    • 测试团队测试环境test

    • 预发布环境pre

    • 正式生产环境prod

      image-20211217164617179
2.搭建多套开发环境
  • Maven如何实现多套环境配置并动态切换呢?

    • 配置多套环境 : 为了实现不同环境的构建需求,maven 提供了 profile 的支持,下面是一个不同环境下拷贝资源时为 host 提供不同值的例子

      <?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">
          
      	...
      
          <build>
      		
              ...
      		
              <resources>
                  <resource>
                      <directory>src/main/resources</directory>
                      <filtering>true</filtering>
                  </resource>
              </resources>
          </build>
      
          <profiles>
              <profile>
                  <id>development</id>
                  <properties>
                      <host>localhost</host>
                  </properties>
              </profile>
      
              <profile>
                  <id>production</id>
                  <properties>
                      <host>www.itheima.com</host>
                  </properties>
              </profile>
          </profiles>
      </project>
      
    • 切换具体环境:

      方式1 - idea 中勾选相应的 profile:本质是在执行 mvn 命令时添加 -P production 参数
      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pblgiKoF-1645080006214)(img/26.png)]

      方式2 - 设置默认激活

      <?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">
          
      	...
      
          <build>
      		
              ...
      		
              <resources>
                  <resource>
                      <directory>src/main/resources</directory>
                      <filtering>true</filtering>
                  </resource>
              </resources>
          </build>
      
          <profiles>
              <profile>
                  <id>development</id>
                  <properties>
                      <host>localhost</host>
                  </properties>
      			<activation>
                      <activeByDefault>true</activeByDefault>
                  </activation>
              </profile>
      
              <profile>
                  <id>production</id>
                  <properties>
                      <host>www.itheima.com</host>
                  </properties>
              </profile>
          </profiles>
      </project>
      

​ 参考资profile 介绍 - https://maven.apache.org/guides/introduction/introduction-to-profiles.html

六、Maven仓库管理

1.国内中央仓库
  • setting.xml中配置全局mirror镜像拦截国外中央仓库

    • 场景1: Maven中央仓库在国外,下载速度比较慢。能不能不从国外下载呢?

      <!--中央仓库地址--> 
      <repository>
            <snapshots>
              <enabled>false</enabled>
            </snapshots>
            <id>central</id>
            <name>Central Repository</name>
            <url>https://repo.maven.apache.org/maven2</url>
      </repository>
      
    • 解决1:在setting.xml中配置国内阿里云仓库镜像(这个仓库速度很爆)

             	<mirrors>
                    <!-- mirror
                     | Specifies a repository mirror site to use instead of a given repository. The repository that
                     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
                     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
                     |
                    <mirror>
                      <id>mirrorId</id>
                      <mirrorOf>repositoryId</mirrorOf>
                      <name>Human Readable Name for this Mirror.</name>
                      <url>http://my.repository.com/repo/path</url>
                    </mirror>
                     -->
                    <mirror>
                        <id>aliyun</id>
                        <mirrorOf>central</mirrorOf>
                        <name>aliyun</name>
                        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
                    </mirror> 
                </mirrors>   
      

      注意:最好重启 idea 让 mirror 生效观察一下 jar 的下载地址

      阿里云仓库地址大全:https://developer.aliyun.com/mvn/guide [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mzU3qlhu-1645080006214)(img/image-20211217175717473.png)]

      什么是mirror?请求A仓库时拦截去找B仓库作用

      image-20211217174447346

  • 项目中pom.xml配置指定仓库互补下载依赖

    • 场景2:如果阿里云仓库中没有该依赖,该怎么办?

    • 解决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">
          
      	...
          <!--这里举例,阿里云镜像已经能够满足实际开发所需的依赖了-->
          <repositories>
              <repository>
                  <id>aliyun</id>
                  <url>https://maven.aliyun.com/repository/spring</url>
              </repository>
          </repositories>
      	
      	...
      	
      </project>
      
  • 练习🎯

    • 动手找到自己的maven安装目录,然后在setting.xml中设置一下镜像(一劳永逸的好处)
2.公司远程仓库
  • nexus私服

    • 场景:在公司开发时,自己开发出来的 jar 包不会放在大家都能访问的中央仓库上,那会放在哪里?

      image-20211217183159767

    • nexus搭建(学习期间不需要搭建,开发中开发人员不会搭建)

      私服采用了 nexus 公司提供的免费软件来搭建,下载地址为:https://www.sonatype.com/nexus/repository-oss-download 如果自己尝试搭建,建议选择 2.x 这个旧的版本,因为更小、更简单

      • 第一步,解压缩,会得到下面两个文件夹和如下重要目录结构

        nexus-2.14.20-02
        	|-bin
        		|-jsw
        			|-windows-x86-64
        				|-console-nexus.bat   以黑窗口方式运行
        				|-install-nexus.bat	  安装nexus为windows服务
        				|-start-nexus.bat     启动nexus的windows服务
        				|-stop-nexus.bat      停止nexus的windows服务
        				|-uninstall-nexus.bat 卸载nexus的windows服务
        			|-conf
        				|-wrapper.conf
        sonatype-work
        
      • 第二步(可选),如果 java 的环境变量配置的不是 1.8 版本,修改上图中 wrapper.conf,将

        wrapper.java.command=java
        

        修改为,其中 C:\Program Files\Java\jdk1.8.0_212\bin 根据你 jdk 安装的实际情况改动

        wrapper.java.command=C:\Program Files\Java\jdk1.8.0_212\bin\java
        
      • 第三步,如果 java 的环境变量配置的是 1.8 版本,直接运行 console-nexus.bat 即可,想停止服务,按 Ctrl+C 或者直接关闭黑窗口即可

      • 第四步,打开浏览器,点击右上角的 Log In 链接,输入用户名 admin 和密码 admin123

          http://localhost:8081/nexus
        

        先认识一下管理界面
        [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HTReYZ1f-1645080006215)(img/32.png)]

        我们要做的就是记录正式版(Releases)和快照版(Snapshots)的仓库地址,以后我们自己开发打包的 jar 就会向这两个地址去上传,同时也可以供其它公司同事下载

      • 第五步,配置仓库地址,用于 deploy 上传

        <?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">
            
        	...
        
            <distributionManagement>
                <repository>
                    <id>releases</id>
                    <url>http://localhost:8081/nexus/content/repositories/releases/</url>
                </repository>
                <snapshotRepository>
                    <id>snapshots</id>
                    <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
                </snapshotRepository>
            </distributionManagement>
        	
        	...
        	
        </project>
        

        说明 :releases 对应正式版的仓库,snapshots 对应快照版的仓库

      • 第六步,因为上传 jar 需要认证,还要在 settings.xml 中配置认证信息

        <?xml version="1.0" encoding="UTF-8"?>
        <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
        
          ...
        	
          <servers>
            <server>
              <id>releases</id>
              <username>admin</username>
              <password>admin123</password>
            </server>
            <server>
              <id>snapshots</id>
              <username>admin</username>
              <password>admin123</password>
            </server>
          </servers>
        
          ...
        	
        </settings>
        

        其中 <server> 的 id 标签值要与 <repository> 以及 <snapshotRepository> 的 id 标签值一致,用户名密码这里为了简单就采用了私服管理员的,以后公司会分配个人账号和密码

      • 第七步,运行 mvn deploy 命令,即可将对应模块的 jar 上传至私服,注意我们的 jar 的 version 是 snapshot 结尾的,最后会上传至 snapshots 对应的库

      • 第八步,其它开发人员如果要从私服下载 jar 包,按普通方式配置仓库即可

        <?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">
            
        	...
        
            <repositories>
                <!-- 其它第三方的仓库 -->
        		
        		<!-- 本公司的私服仓库地址 -->
                <repository>
                    <id>releases</id>
                    <url>http://localhost:8081/nexus/content/repositories/releases/</url>
                </repository>
                <repository>
                    <id>snapshots</id>
                    <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
                </repository>
            </repositories>
        	
        	...
        	
        </project>
        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Code攻城狮

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

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

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

打赏作者

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

抵扣说明:

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

余额充值