maven 私服搭建

一、仓库:本地仓库、第三方仓库(内部中心仓库\私服)、中央仓库

1、本地仓库:1)、maven将工程中依赖的jar包(构件)从远程下载到本地某目录下管理,通常默认存储地址为${user.home}/.m2/responsitory/(Windows下可以通过%USERPROFILE%直接定位到当前用户文件夹路径下);2)、jar包存储方式为在responsitory下groupId/artifactId/version/*.jar;3)修改本地仓库方式,找到$MAVEN_HOME/conf/setting.xml文件,修改配置<localRepository>即可;3)、使用jar包时,先从本地仓库下载,如果没有就去第三方仓库,如果还没有就去中央仓库下载

2、私服:1)、公司自己设立,只为公司内部共享使用,同时减少外部访问和下载频率;2)、私服一般是第三方提供的,常见私服服务器为:Nexus和Artifactory;3)、setting.xml(全局的)或pom.xml(当前项目的)中可以同时配置多个私服地址;3)、私服要单独配置,如果没有配置,默认不使用

3、中央仓库:1)、地址为https://mvnrepository.com/;2)、如果本地没有jar包并且没有配置私服,要去中央仓库下载,需要联外网

 二、使用Nexus搭建私服

1、下载对应版本,下载地址:https://www.sonatype.com/download-oss-sonatype;或https://support.sonatype.com/hc/en-us/articles/213464298-Sonatype-Nexus-Professional-Download-Archives;或http://www.sonatype.org/nexus/archived/

2、两种搭建方式(Windows下):1)、war包方式,直接把war包放到服务器包内,如tomcat下webapps文件夹下,启动tomcat,访问部署的war包即可

                                   2)、非war包方式主要介绍两个版本部署方式:如果下载的是nexus-2.14.4-03-bundle.zip版本,解压后...\nexus-2.14.4-03\bin\jsw\windows-x86-64路径下直接双击console-nexus.bat即可,其他bat文件自行百度;如果是nexus-3.4.0-02-win64.zip版本,在...\nexus-3.4.0-02\bin路径下shift+右键,选择打开命令窗口,执行nexus.exe /run命令即可

  (Linux环境下解压nexus-2.14.4-03-bundle.tar.gz或nexus-2.14.4-03-bundle.zip后,在...\\nexus-2.14.4-03\bin下执行./nexus start命令即可)

  (war包文件是没有内置服务器的,bundle式文件绑定了jetty,所以直接启动就可以访问)

  (war包部署的nexus,其仓库物理地址配置文件是\WEB-INF\plexus.properties,属性是nexus-work=${user.home}/sonatype-work/nexus;nexus-2.14.4-03-bundle部署的nexus仓库物理地址配置文件是\nexus-2.14.4-03\conf\nexus.properties;nexus-3.4.0-02部署的nexus仓库物理地址配置文件是\nexus-3.4.0-02\bin\nexus.vmoptions;这些配置中的sonatype-work文件夹是可以删除的,然后在启动服务时候自动创建)

访问地址http://localhost:8081/nexus;如果tomcat就是指定端口,登录用户名密码默认admin\admin123

3、仓库目录\sonatype-work\nexus\下的indexer文件夹下存放jar包坐标索引;\sonatype-work\nexus\storage\中central存储私服从中央仓库下载下的jar包,releases存储项目发布版(deploy),snapshots存放项目测试版,public是用于整合central、releases、snapshots,后期用于私服上jar包下载

4、私服仓库的每一个物理地址对应私服网站上的路径,以及public可以配置包含哪几个仓库路径整合

三、项目发布到私服(上传,需要账号和密码)

1、配置账号密码,这样maven就可以访问nexus服务器,传输或下载文件了

在maven安装目录下的settings.xml文件中配置(如D:\maven\apache-maven-3.3.9\conf\settings.xml)

 servers节点下配置nexus网站的用户名密码

复制代码

    <!--发布版-->
    <server>
        <id>releases</id>
        <username>admin</username>
        <password>admin123</password>
    </server>
    <!--测试版-->
      <server>
        <id>snapshots</id>
         <username>admin</username>
        <password>admin123</password>
    </server>

复制代码

2、配置POM项目中pom.xml文件,使项目发布到私服

project节点下配置如下,其中仓库对应的id要和上面server中配置的id一致,url就是nexus网站中Repositories下releases和snapshots

复制代码

<!--发布到私服配置-->
    <distributionManagement>
        <repository>
            <id>releases</id>
            <name>Internal Release</name>
            <url>http://localhost:8081/nexus/content/repositories/releases/</url>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <name>Internal Snapshots</name>
            <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

复制代码

要注意当前项目version的配置,如果想发布到releases仓库,就要配置成“版本号-release”;如果发布到snapshots仓库,就配置成“版本号-snapshot”,不区分大小写

执行maven的发布命令后就会发布到指定仓库

我用的是IDEA,所有直接双击执行maven deploy命令即可 ,注意的是IDEA如果不设置会自己下载一套maven,这需要修改maven路径

       

四、从私服获取jar包

1、配置镜像,对指定路径进行拦截,默认maven访问中央,拦截后改为访问私服

在maven安装目录下的settings.xml文件中配置(如D:\maven\apache-maven-3.3.9\conf\settings.xml)

mirrors节点下配置

    <mirror>
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://localhost:8081/nexus/content/groups/public/</url>
    </mirror>

url配置成http://localhost:8081/nexus/content/groups/public/,这样获取jar包可以根据配置顺序优先级选择下载

2、settings.xml中配置profile,maven自定义配置,配置后如果使用需要激活

profiles节点下配置,注意profile下的id要和上面镜像的id一致,respositories和pluginRepositories要使用镜像中配置的

复制代码

    <profile>
        <id>nexus</id>
        <repositories>
            <repository>
                 <id>central</id>
                <url>http://repo1.maven.org/maven2/</url>
                <releases><enabled>true</enabled></releases>
                <snapshots><enabled>true</enabled></snapshots>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>          
                <id>central</id>
                 <url>http://repo1.maven.org/maven2/</url>
                 <releases><enabled>true</enabled></releases>
                <snapshots><enabled>true</enabled></snapshots>
            </pluginRepository>
        </pluginRepositories>
    </profile>

复制代码

 3、settings.xml激活profile,settings节点下配置

<activeProfiles>
    <activeProfile>nexus</activeProfile>
</activeProfiles>