maven搭建私服

1 .搭建私服原因 

         搭建mavn私服的原因在于 可能公司内部开发人员无法上外网 这时 需要搭建一个中介平台(私服) 该平台(需要联网)可以从外部的网络中获取需要的依赖库 缓存在私服中

开发环境中需要配置私服的地址 开发环境发起依赖包的请求到私服时  如果私服中存在该依赖库则返回 没有则联网同步

2. 搭建私服

     1 >私服的搭建需要借助软件Nexus  下载地址:   https://www.sonatype.com/download-oss-sonatype

     2 >进入nexus目录bin目录下  安装并启动服务  nexus.bat install       

           nexus-2.1.1\bin>nexus install
                wrapper  | nexus installed.
            nexus-2.1.1\bin>nexus start
               wrapper  | Starting the nexus service...
              wrapper  | Waiting to start...
              wrapper  | nexus started.

      找到 conf目录下nexus.properties 文件查看应用端口   application-port=8081  通过浏览器访问  http://localhost:8081/nexus  [该地址为搜索依赖库地址]

      右上角 Log in点击登录就可以管理私服仓库  用户名 admin密码 admin123   点击左侧Repositories 可以看到Central中央仓库的地址

        

  在中央仓库上点击右键  updateindex  会自动到外网更新依赖库的索引 速度很慢  这里可以先下载索引文件后 更新nexus缓存中

   步骤如下:

        apache官网下载index文件  地址: http://repo.maven.apache.org/maven2/.index/      下载如下两个文件

      nexus-maven-repository-index.gz                   2017-04-30 14:09 355151742      
      nexus-maven-repository-index.properties       

  下载解压工具包(search.maven.org搜索indexer-cli下载indexer-cli-5.1.1.jar)

      http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22org.apache.maven.indexer%22       

   将下载的indexer的jar和下载的index文件和properties文件放在同一个目录下  

   执行该jar会自动解压 该索引文件到indexer目录下(解压时间需要几分钟 根据机器速度而定)

    java -jar indexer-cli-5.1.1.jar -u nexus-maven-repository-index.gz -d indexer

    

然后将indexer目录下的文件全部拷贝到 %NEXUS_HOME%\sonatype-work\nexus\indexer\central-ctx  覆盖

以上这种方式更新索引一直没有成功  现在推荐另外一种

使用tomcat模拟 http://repo1.maven.org/maven2/.index/的路径

安装一个tomcat 在tomcat的webapps目录下 新建目录maven2 maven2下新建.index文件夹 将下载的index文件和properties文件拷贝到这个.index目录下

假设tomcat端口为8080 可以通过路径 http://localhost:8080/maven2/.index/nexus-maven-repository-index.properties 访问测试是否部署成功


点击central点击下面的配置选项  修改nexus上更新索引的位置  会自动更新索引


点击save后 点击左侧菜单的Administrators下的Scheduled Tasks可以看到正在更新索引 因为是从本地tomcat更新索引所以一般几分钟就更新完成了 在更新的过程中可以

查看  sonatype-work\nexus\tmp目录下的文件nexus-maven-repository-index.gz开头的目录 一直增大到几个G 更新完成这个目录会自动被拷贝到indexer目录下 

索引更新完成就可以  browse index查看是否更新成功


在中心库中使用本地索引 方便我们快速搜索依赖库的配置

这里我们需要将开始设置的Remote Storage Location 从刚开始修改后的 http://localhost:8080/maven2修改为原始的镜像地址,并且将download remote indexs修改为false 否则无法找不到jar包 会报搜索出错

   这里 以前的值是 http://repo.maven.apache.org/maven2/ 由于到国外服务器下载所以速度非常慢  这里可以替换为 aliyun的maven地址 毕竟在国内

如果直接修改后save 会重新更新索引前面工作等于白做  这里考虑 修改配置文件 不要点击save

找到sonatype-work\nexus\conf\nexus.xml 该文件

  找到central的库配置

   repositories>
    <repository>
      <id>central</id>
      <name>Central</name>
      <providerRole>org.sonatype.nexus.proxy.repository.Repository</providerRole>
      <providerHint>maven2</providerHint>
      <localStatus>IN_SERVICE</localStatus>
      <notFoundCacheActive>true</notFoundCacheActive>
      <notFoundCacheTTL>1440</notFoundCacheTTL>
      <userManaged>true</userManaged>
      <exposed>true</exposed>
      <browseable>true</browseable>
      <writePolicy>READ_ONLY</writePolicy>
      <indexable>true</indexable>
      <searchable>true</searchable>
      <localStorage>
        <provider>file</provider>
      </localStorage>
      <remoteStorage>
        <provider>apacheHttpClient3x</provider>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>   <!--这里修改为aliyun的地址即可-->
      </remoteStorage>

重启nexus  通过maven客户端配置到public的镜像地址后 可以新建eclipse maven项目测试是否能够正常添加jar包

maven客户端找到 conf/settting.xml 

   <mirrors>
<mirror>  
           <id>central</id>  
           <name>central</name>                                                                                                                         
           <url>http://localhost:8081/nexus/content/groups/public/</url> 
       <mirrorOf>central</mirrorOf>  
    </mirror>
  </mirrors>





posted @ 2017-05-02 11:22  饺子吃遍天  阅读(198)  评论(0编辑  收藏  举报