Skip to content

nexus 第二篇

yangyp8110 edited this page Jan 17, 2018 · 1 revision

nexus

安装完nexus后,默认存放repository地址是\sonatype-work\nexus\storage\central

登陆http://192.168.74.129:8081/nexus,选中Central,打开Configuration选项卡,可以看到默认存储位置:Default Local Storage Location为:/usr/local/install/nexus/sonatype-work/nexus/storage/central;如需修改存储位置改Override Local Storage Location

nexus默认用户(Users):

Nexus 预定义了三个用户,这三个用户对应了三个权限级别,可以从右侧的菜单栏查看权限信息:

  • admin:该用户拥有对Nexus服务的完全控制,默认密码为 admin123
  • deployment:该用户能够访问Nexus,浏览仓库、搜索,并且上传部署构件,但无法对Nexus进行任何配置,默认密码为 deployment123
  • anonymous:该用户对应了所有未登录的匿名用户,可以浏览仓库并搜索

nexus角色(Roles):

Nexus 预定义了一些常用且重要的角色:

  • UI:Basic UI Privileges:包含了访问Nexus界面必须的最基本权限
  • UI:Repository Browser:包含了浏览仓库页面所需要的权限
  • UI:Search:包含了访问快速搜索页面说需要的权限
  • Repo:All Repositoryies(Read):给予用户读取所有仓库内容的权限,没有仓库的读权限用户将无法在仓库页面上看到实际的仓库内容,也无法使用Maven从仓库下载构件
  • Repo:All Repositoryies(Full Control):给予用户完全控制所有仓库内容的权限。

nexus为项目分配独立的仓库

如果所有项目都部署快照及发布构件至同样的仓库,就会存在潜在的冲突及安全问题,因此我们可以为每个项目分配独立的仓库,并且只将仓库的部署、修改和删除权限赋予该项目的成员,其他用户只能读取、下载和搜索该仓库的内容。

  • 创建项目的独立仓库
  • 点击 Repositories -> Add Hosted Repositor 打开创建仓库的页面,输入仓库信息,点击保存:

  • 创建仓库:

  • 创建指定仓库的权限
  • 点击 Privileges -> Add Repository Target Privilege 打开权限页面,在页面中的 Repository 选择新创建的仓库,Repository Target 中选择 All Maven2

  • 创建角色并指定权限
  • 点击 Roles -> Add Nexus Role 打开角色页面,输入基本信息后,增加权限,将创建的权限根据需求增加

  • 创建用户并指定角色
  • 点击 Users -> Add Nexus User 打开创建用户页面,输入用户基本信息并增加角色

配置maven使用nexus(从nexus下载jar)

  • 1)在maven安装目录中找到maven的全局配置settings.xml文件
  • 2)将settings.xml修改为如下配置(可以从nexus下载依赖的包):
    <?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">  
      
      <localRepository>D:\Program Files\Java\Maven-Repo</localRepository>  
      
      <pluginGroups>  
          
      </pluginGroups>  
      
      <!-- proxies  
       | This is a list of proxies which can be used on this machine to connect to the network.  
       | Unless otherwise specified (by system property or command-line switch), the first proxy  
       | specification in this list marked as active will be used.  
       |-->  
      <proxies>  
        <!-- proxy  
         | Specification for one proxy, to be used in connecting to the network.  
         |  
        <proxy>  
          <id>optional</id>  
          <active>true</active>  
          <protocol>http</protocol>  
          <username>proxyuser</username>  
          <password>proxypass</password>  
          <host>proxy.host.net</host>  
          <port>80</port>  
          <nonProxyHosts>local.net|some.host.com</nonProxyHosts>  
        </proxy>  
        -->  
      
      </proxies>  
      
        
      <!-- servers  
       | This is a list of authentication profiles, keyed by the server-id used within the system.  
       | Authentication profiles can be used whenever maven must make a connection to a remote server.  
       |-->  
      <servers>  
          
      </servers>  
      
      <!-- 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>  
          <!--This is used to direct the public snapshots repo in the   
              profile below over to a different nexus group -->  
          <id>demorepository</id>  
          <mirrorOf>firstRepository</mirrorOf>  
          <url>http://192.168.74.129:8081/nexus/content/repositories/demoRepository</url>  
        </mirror>  
        <mirror>  
          <!--This sends everything else to /public -->  
          <id>nexus</id>  
          <mirrorOf>*</mirrorOf>  
          <url>http://192.168.74.129:8081/nexus/content/groups/public</url>  
        </mirror>  
      </mirrors>  
        
      <profiles>  
          
        <profile>  
          <id>development</id>  
          <repositories>  
            <repository>  
              <id>central</id>  
              <url>http://central</url>  
              <releases><enabled>true</enabled></releases>  
              <snapshots><enabled>true</enabled></snapshots>  
            </repository>  
          </repositories>  
         <pluginRepositories>  
            <pluginRepository>  
              <id>central</id>  
              <url>http://central</url>  
              <releases><enabled>true</enabled></releases>  
              <snapshots><enabled>true</enabled></snapshots>  
            </pluginRepository>  
          </pluginRepositories>  
        </profile>  
        <profile>  
          <!--this profile will allow snapshots to be searched when activated-->  
          <id>firstRepository</id>  
          <repositories>  
            <repository>  
              <id>demorepository</id>  
              <url>http://firstRepository</url>  
              <releases><enabled>false</enabled></releases>  
              <snapshots><enabled>true</enabled></snapshots>  
            </repository>  
          </repositories>  
         <pluginRepositories>  
            <pluginRepository>  
              <id>demorepository</id>  
              <url>http://firstRepository</url>  
              <releases><enabled>false</enabled></releases>  
              <snapshots><enabled>true</enabled></snapshots>  
            </pluginRepository>  
          </pluginRepositories>  
        </profile>  
      </profiles>  
      
      <!-- activeProfiles  
       | List of profiles that are active for all builds.  
       |  
      <activeProfiles>  
        <activeProfile>alwaysActiveProfile</activeProfile>  
        <activeProfile>anotherAlwaysActiveProfile</activeProfile>  
      </activeProfiles>  
      -->  
      <activeProfiles>  
        <activeProfile>development</activeProfile>  
      </activeProfiles>  
    </settings>  

deploy到demorepository的配置:

  • ideal使用的setting.xml里<servers>节点中加入认证信息:
    • 如果为配置server认证信息,会报错Return code is: 401, ReasonPhrase: Unauthorized.
      <server>
          <id>demoRepository</id>
          <username>admin</username>
          <password>admin123</password>
      </server>
  • 配置pom的distributionManagement,可以从nexus的仓库里获取,如下图:
  <distributionManagement>
    <snapshotRepository>
      <id>demoRepository</id>
      <url>http://192.168.74.129:8081/nexus/content/repositories/demoRepository</url>
    </snapshotRepository>
  </distributionManagement>

  • 运行项目里deploy,可以看到通过http协议发布到了服务器上:
[root@yyp sonatype-work]# ll /usr/local/install/nexus/storage/demoRepository/com/yangyp/common/yangyp.common/
total 16
drwxr-xr-x. 2 root root 4096 Apr 16 12:33 1.0-SNAPSHOT
-rw-r--r--. 1 root root  288 Apr 16 12:33 maven-metadata.xml
-rw-r--r--. 1 root root   32 Apr 16 12:33 maven-metadata.xml.md5
-rw-r--r--. 1 root root   40 Apr 16 12:33 maven-metadata.xml.sha1
[root@yyp sonatype-work]# ll nexus/storage/demoRepository/com/yangyp/common/yangyp.common/1.0-SNAPSHOT/
total 36
-rw-r--r--. 1 root root  774 Apr 16 12:33 maven-metadata.xml
-rw-r--r--. 1 root root   32 Apr 16 12:33 maven-metadata.xml.md5
-rw-r--r--. 1 root root   40 Apr 16 12:33 maven-metadata.xml.sha1
-rw-r--r--. 1 root root 3502 Apr 16 12:33 yangyp.common-1.0-20170416.162907-1.jar
-rw-r--r--. 1 root root   32 Apr 16 12:33 yangyp.common-1.0-20170416.162907-1.jar.md5
-rw-r--r--. 1 root root   40 Apr 16 12:33 yangyp.common-1.0-20170416.162907-1.jar.sha1
-rw-r--r--. 1 root root 1375 Apr 16 12:33 yangyp.common-1.0-20170416.162907-1.pom
-rw-r--r--. 1 root root   32 Apr 16 12:33 yangyp.common-1.0-20170416.162907-1.pom.md5
-rw-r--r--. 1 root root   40 Apr 16 12:33 yangyp.common-1.0-20170416.162907-1.pom.sha1
[root@yyp sonatype-work]# 

Clone this wiki locally