Ubuntu下 Maven 私服(Nexus) 安装配置

好了,新建一个 Maven 项目测试一下吧,看看jar包是否是从你指定的私服地址下载的。java

OK,重启你的 Ubuntu 试试吧!数据库

而后,在咱们要发布的项目所在 POM 文件中<project>标签下加入以下配置:apache

切记<server>标签 id 要和<distributionManagement>标签中的<repository>标签 id 要一致。maven

mvn deploy 命令就发布到仓库了!!!测试

OK,伺服搭建、配置,以及本地 Maven 如何使用私服,基本介绍完毕。如有疑问或不对之处,欢迎联系我指正!this

  • 安装环境url

    Ubuntu 14.04.3 LTS 64 位spa

  • Nexus 安装.net

    首先下载 Nexus 的 Linux 安装包,官方地址:http://www.sonatype.org/nexus/go/,若是不能打开或没法下载,能够经过个人网盘分享下载:http://pan.baidu.com/s/1sjmpxsp插件

    下载完成后,把你的 jdk 安装包上传到 Ubuntu 中 /usr/local/src 目录下,Nexus 是依赖 java jdk 的,若是已经安装好了 jdk,以下命令开始进行安装。(jdk 怎么安装?戳我!

    • cd /usr/local/src
      tar –zvxf nexus-latest-bundle.tar.gz
      mv sonatype-work ../
      mv nexus-2.11.2-06 ../
      cd ../nexus-2.11.2-06/bin
      ./nexus start

      nexus 启动失败,错误信息以下:

    • If you insist running as root, then set the environment variable RUN_AS_USER=root before running this script.

      意思是说若是你想运行这个脚本,必须先设置环境变量RUN_AS_USER=root,那么咱们来加入这个环境变量

    • vi /etc/profile
      # 末尾重起一行加入
      export RUN_AS_USER=root
      # 退出保存
      source /etc/profile # 让环境变量当即生效

      从新执行./nexus start。正常启动了,若是还报错,注意阅读异常信息,耐心 Google Baidu。

      咱们能够经过网页访问 http://server_ip:8081/nexus,来查看服务是否安装成功! 登录的用户名和密码是:admin/amdin123




         
      •  
  • 配置 Maven 使用私服下载 jar 包

    一般咱们本地配置好 Maven 之后,Maven 默认从http://repo.maven.apache.org/maven2下载jar包,咱们若是须要指定 Maven 从本身的伺服下载 jar 包,须要修改 MAVEN_HOME/conf/settings.xml文件(MAVEN_HOME是你 Maven的安装目录),找到 <mirros>标签,在其中加入以下内容:

    • <mirror>
        <id>myNexus</id>
        <mirrorOf>*</mirrorOf>
        <name>My Nexus Mirror</name>
        <url>http://192.168.0.147:8081/nexus/content/groups/public</url>
        <releases>  
          <enabled>true</enabled>
        </releases>  
        <snapshots>  
          <enabled>true</enabled>  
        </snapshots>
      </mirror>
  • mirrorOf
    * = everything
    external:* = everything not on the localhost and not file based.
    repo,repo1 = repo or repo1
    *,!repo1 = everything except repo1

  • 从私服下载 snapshots 版本的 jar 包

    进行了如上配置后,默认就从私服下载 jar 包了,可是没法从私服下载到 snapshots 版本的 jar 包和插件。若想下载 snapshots版本须要进行以下配置,找到修改 MAVEN_HOME/conf/settings.xml文件中<profiles>标签,在其中加入以下内容:

    • <!-- 经过上面的mirror配置,项目已经能从伺服下载jar包和插件,但不能下载snapshots版本的jar,经过如下配置开启 -->
      <profile>
          <!-- 从指定伺服下载jar包,此处开启了snapshots版本的jar包下载 -->
          <id>use-local-nexus</id>
          <repositories>
              <repository>
                  <id>nexus</id>
                  <name>local private nexus</name>
                  <url>http://192.168.0.147:8081/nexus/content/groups/public/</url>
                  <releases>  
                      <enabled>true</enabled>
                  </releases>  
                  <snapshots>  
                      <enabled>true</enabled>  
                  </snapshots>  
              </repository>
          </repositories>
          <!-- 指定插件也从伺服下载 -->
          <pluginRepositories>
              <pluginRepository>
                  <id>nexus</id>
                  <name>local private nexus</name>
                  <url>http://192.168.0.147:8081/nexus/content/groups/public/</url>
                  <releases>
                      <enabled>true</enabled>
                  </releases>
                  <snapshots>
                      <enabled>false</enabled>
                  </snapshots>
              </pluginRepository>
          </pluginRepositories>
      </profile>
  • Maven私服开机启动设置

    • vi /usr/local/nexus-2.11.2-06/bin/nexus
      # 修改一下两项
      NEXUS_HOME="/usr/local/nexus-2.11.2-06"
      RUN_AS_USER=root
      # 保存退出
    • vi /etc/rc.local
      # 在 exit0 以前加入下面一行
      /usr/local/nexus-2.11.2-06/bin/nexus start
  • 私服讲解

    Maven 安装完成之后,通常咱们会对其进行一些配置,使其更易于使用。

    登录后点击左侧导航的 Repositories ,看到以下图

    注意 Type 这一列,Maven 仓库一共有 4 中类型:group,hosted,proxy,virtual。

    • hosted 本地仓库,一般咱们会部署本身的构件到这一类型的仓库。如图,安装好私服后,会默认有三个 hosted 类型的仓库,名字分别是 3rd party,Releases,Snapshots。

      • 3rd party 是用来上传一些公共库中没有的jar包,如 Oracle数据库驱动,MySQL数据库驱动。咱们将驱动上传到 3rd party 这个仓库之后,项目就能直接经过私服下载数据库驱动了。

      • Releases 咱们将本身开发的jar包发布到仓库,若是jar包是 Releases 版本的,jar包会被发布到这个仓库

      • Snapshots,同 Releases 同样,若是咱们的 jar 包是 Snapshots版本的,jar包会被发布到这个仓库

         

    • proxy 代理仓库,安装好私服后,会默认有一个名为 Central 的仓库,它是一个代理仓库。也就是说,假如你请求私服下载 jar 包,可是私服中没有此 jar 包,那么私服会经过代理仓库来下载你请求的 jar 包。你能够配置多个代理仓库,配置完代理仓库后,记得将代理仓库加入Public Repositories组。另外记得将代理仓库的 Download Remote Indexes 选项设置为 true 。这里说几个经常使用的代理仓库地址:

      • https://repository.apache.org/snapshots/

      • http://repo1.maven.org/maven2/ (Central 库的地址就是这个)

      • https://nexus.codehaus.org/snapshots/

      • http://maven.oschina.net/content/groups/public/ (开源中国的Maven仓库)

      • http://repository.sonatype.org/content/groups/forge/

    • group,仓库组。仓库组中能够加入多个hosted/proxy类型的仓库。它至关于hosted/proxy类型的仓库集合,因此咱们配置 Maven 从私服下载 jar 包时,通常把 Maven 私服地址指向这个仓库组,而不指向具体某个 hosted/proxy类型的仓库。

    • 上面说的上传 jar 包到 hosted 类型仓库,添加一个 proxy 代理仓库,以及在group中加入多个 hosted/proxy类型的仓库,你能够参考这篇文章http://aijezdm915.iteye.com/blog/1335025


  • 发布本身的 jar 包到私服

    首先,MAVEN_HOME/conf/settings.xml文件中<servers>标签下加入以下配置:

    • <server>  
      	<id>nexus-releases</id>  
      	<username>admin</username>  
      	<password>admin123</password>  
      </server>  
      <server>  
      	<id>nexus-snapshots</id>  
      	<username>admin</username>  
      	<password>admin123</password>  
      </server>
    • <distributionManagement>
      	<repository>
      		<id>nexus-releases</id>
      		<name>Nexus Release Repository</name>
      		<url>http://192.168.0.147:8081/nexus/content/repositories/releases/</url>
      	</repository>
      	<snapshotRepository>
      		<id>nexus-snapshots</id>
      		<name>Nexus Snapshot Repository</name>
      		<url>http://192.168.0.147:8081/nexus/content/repositories/snapshots/</url>
      	</snapshotRepository>
      </distributionManagement>

其它须要注意的问题及推荐

  1. 尽可能不要配置mirrorOf为*, 会致使pom文件中的repository不生效
  2. 私服的配置推荐用profile配置而不是mirror(毕竟mirror是镜像,私服实际上是n个镜像及本身的开发库等的合集)
     
    <profile>
          <id>nexus</id>
          <repositories>
            <repository>
              <id>nexus</id>
              <url>http://192.168.163.xx:xx/nexus/content/groups/public/</url>
              <releases>
                <enabled>true</enabled>
              </releases>
              <snapshots>
                <enabled>true</enabled>
              </snapshots>
            </repository>
          </repositories>
          <pluginRepositories>
            <pluginRepository>
              <id>nexus</id>
              <url>http://192.168.163.xx:xx/nexus/content/groups/public/</url>
              <releases>
                <enabled>true</enabled>
              </releases>
              <snapshots>
                <enabled>true</enabled>
              </snapshots>
            </pluginRepository>
          </pluginRepositories>
        </profile>
      </profiles>
      <activeProfiles>
        <activeProfile>nexus</activeProfile>
      </activeProfiles>
相关文章
相关标签/搜索