zoukankan      html  css  js  c++  java
  • Maven + 私服+中央仓库

    maven 简介

    Maven 是 Apache 下的一个纯 Java 开发的开源项目。基于项目对象模型(缩写:POM)概念,Maven利用一个中央信息片断能管理一个项目的构建、报告和文档等步骤。

    Maven 是一个项目管理工具,可以对 Java 项目进行构建、依赖管理。

    Maven 也可被用于构建和管理各种项目,例如 C#,Ruby,Scala 和其他语言编写的项目。Maven 曾是 Jakarta 项目的子项目,现为由 Apache 软件基金会主持的独立 Apache 项目。

    maven 优点

    1、jar 包管理

      从中央仓库下载标准的规范的jar 包以及相关依赖的jar包

      本地仓库统一管理jar包,使jar 包与项目分离,减轻项目体积。maven 通过坐标的方式从本地仓库获取jar 包,坐标由公司/组织+项目名/子项目名+版本号组成。

    2、多个maven 项目可以共用一个仓库,maven 可以通过建立本地仓库索引,可以快速找到符合要求的jar 包,从而解决效率问题。

    3、java 开发的,所以支持跨平台,可以在windows,linux ,mac 平台使用。

    4、清晰的项目结构,源码位于../src/main/java ,配置文件位于../src/main/resource目录。

    5、将大型项目可以按照功能模块拆分为多个工程,每个人负责维护各自的工程。例如一一个微服务架构的java项目:用户中心系统,可以拆分为多个子工程(登录验证、后台管理、验证码等等),这几个子工程每个工程一个目录上级是一个公共目录,整个目录可以存放在gitlab上,每个子项目都是一个独立的maven工程,可以单独编译,因为都有自己的pom.xml 文件。

    maven 仓库

    maven仓库分为本地仓库与远程仓库。远程仓库又分为私服、中央仓库、第三方仓库。

    maven 默认配置的是官方的中央仓库,如果本地有私服,一般需要配置两个地址,一个是私服的group (group把私服上面的一些仓库统一管理访问)地址,另一个是中央仓库的代理地址,例如阿里云的中央仓库配置代理在私服上面maven 配置私服的此代理地址就会通过此地址简介访问中央仓库。

    本地仓库就是一个缓存目录,maven 需要jar 包的时候优先从本地仓库获取,获取不到就会从私服、中央仓库等逐级获取最后缓存到本地仓库一份以供下次使用,提高效率。

    私服就是本地服务器搭建的私有仓库比如nexus,一般位于内网,开发人员把依赖的公共jar 包放在本地私服以供使用。

    中央仓库是第三方维护的maven仓库,具有权威性,位于国外,国内可以使用阿里云。

    第三方仓库就是第三方搭建的私服允许其他人访问,在访问方来看就是第三方仓库,在搭建者来看就是私服。

    配置中央仓库地址为阿里云仓库地址

    如果不配置阿里云地址,默认就会去官方地址下载,在不配置代理的情况下会很慢。

    vi ../conf/settings.xml 
    
    #此区域的配置说明
    <!-- mirrors
    | This is a list of mirrors to be used in downloading artifacts from remote repositories.
    |
    | It works like this: a POM may declare a repository to use in resolving certain artifacts.
    | However, this repository may have problems with heavy traffic at times, so people have mirrored
    | it to several places.
    |
    | That repository definition will have a unique id, so we can create a mirror reference for that
    | repository, to be used as an alternate download site. The mirror site will be the preferred
    | server for that repository.
    |-->
    
    配置范例
    <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>
    -->      #注释结束
    </mirrors>
    
    实际配置的阿里云仓库
    <mirror>
    <id>nexus-aliyun</id>
    <mirrorOf>*</mirrorOf>
    <name>Nexus aliyun</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror>
    View Code

    配置远程仓库私服

     <profile>
      <id>nexusProfile</id>                               #此处的id 用于后面激活配置才能正式使用私服
          <repositories>
            <repository>
              <id>nexus</id>
              <url>http://1.1.1.1:8081/nexus/content/groups/public/</url>    #此处为私服里面的group仓库地址
          <releases>
            <enabled>true<enabled/>
          </releases>
          <snapshots>
            <enabled>true<enabled/>
          <snapshots/>
    </repository>
    </repositories>
    </profile>

    设置本地仓库路径

    默认情况下仓库位于安装用户的家目录的下例如linux 系统 data用户,就在/home/data/.m2/repository 下面,如果要自定义配置,就在maven 根目录/conf/settings.xml 将localRespository标签中的路径设置成自己的目录路径即可。

    默认配置:  
    <!-- localRepository
       | The path to the local repository maven will use to store artifacts.
       |
       | Default: ${user.home}/.m2/repository
      <localRepository>/path/to/local/repo</localRepository>
    -->
    
    添加如下配置
    <localRepository>/path/to/local/repo</localRepository>      #/path/to/local/repo 替换为自己的目录即可
    View Code

    各个仓库之间的关系

    如果本地仓库是空的,在配置了中央仓库的前提下会去中央仓库下载依赖的jar 包到本地仓库缓存一份来供项目使用。

    其他配置

    server 配置

    <server>
          <id>deploymentRepo</id>
          <username>repouser</username>
          <password>repopwd</password>
    </server>
    用户名和密码就是nexus 里面配置账号密码,id 自定义,在pom 里面会引用此处的id 进而通过此处用户名和密码连接远程仓库

    一般都是通过maven 上传包的时候需要认证信息,也就是通过id 来传递用户名和密码去认证。nexus 里面配置了账号密码以及对应的权限。如果是下载包的话一般匿名用户就可以下载。

    激活配置

    <profile>
    <id>nexusProfile</id>
    <repositories>

    ...

    </repositories>
    </profile>

    #下面激活profile id  才能正式使用配置

    <activeProfiles>

    <activeProfile>nexusProfile</activeProfile>

    </activeProfiles>

     

    maven 常用命令

    mvn  常见命令参数如下

    clean        清理编译后的目录也就是target 目录
    compile       只编译main 不编译test目录
    test-compile   编译test 目录中的代码
    test        运行test里面的代码
    package       打包(jar/war)
    install      发布项目到本地仓库
    tomcat:run    此命令首先编译的项目(在src同级目录会生成一个target目录生成的jar包存放此处),然后启动
    View Code

    maven 生命周期

    clean 生命周期,包含命令为clean
    
    Default 生命周期,包含命令为compile、test-compile、test、package、install
    
    Site   生命周期,包含命令为site
    View Code

    不同生命周期命令可以一起执行,例如mvn  clean compile

    相同生命周期命令有执行顺序,Default生命周期执行顺序:compile -> test-compile -> test -> package -> install,执行后面的命令前面的命令也会默认顺序执行,例如执行install命令默认就会顺序前面compile等所有命令。

    所以生产环境中经常用的命令为 clean package -Dmaven.test.skip=true  -Ppre  ,先清理之前的编译目录target再执行compile -> test-compile -> test -> package,由于-Dmaven.test.skip=true 参数存在就会跳过 test-compile -> test  操作。

  • 相关阅读:
    CodeForces 408E Curious Array(组合数学+差分)
    CodeForces 519E A and B and Lecture Rooms(倍增)
    洛谷 4051 [JSOI2007]字符加密(后缀数组)
    哇,两门学考都是A(〃'▽'〃)
    BZOJ 1977 严格次小生成树
    XJOI 3606 最大子矩形面积/LightOJ 1083 Histogram(单调栈/笛卡尔树)
    XJOI 3629 非严格次小生成树(pqq的礼物)
    XJOI 3363 树4/ Codeforces 739B Alyona and a tree(树上差分+路径倍增)
    [转载]别让用户发呆—设计中的防呆策略
    Linux下的链接文件
  • 原文地址:https://www.cnblogs.com/fanggege/p/12346265.html
Copyright © 2011-2022 走看看