Sonatype Nexus高级配置

Sonatype Nexus的安装配置参见:CentOS系统中安装Nexus并导入已有的构件库。Nexus内置了Jetty容器,${NEXUS_HOME}/bin/jsw目录下包含了各个操作系统的启动脚本。当然,如果你不喜欢内置的Jetty容器,也可以下载Nexus的war包,部署在tomcat等容器中。

安装部署后,比较重要的是能够对其进行深入的管理和配置,本文将介绍一些关于Sonatype Nexus的高级配置技巧,方便您进行配置管理。

1、使用Maven上传组件注意事项

  • host类型的仓库才允许上传组件
  • 仓库需要开启允许上传功能,配置选项中的Deployment Policy选择“Allow Redeploy”,见下图
  • Nexus用户必须具有“Artifact Upload”的权限,Nexus内置了一些角色,比如“Nexus Deployment Role”,就可以上传组件
  • Maven主配置文件中需要配置用户信息及上传的仓库信息,配置示例如下:
  • <server>
        <id>thirdparty</id>
        <username>deployment</username>
        <password>password</password>
    </server>

2、修改监听端口

默认情况下,Nexus监听端口8081。可以通过以下方法更改这个端口。

  • 停止Nexus
  • 编辑${NEXUS_HOME}/conf/plexus.properties文件,修改applicationPort的值
  • 重启Nexus。

3、使Nexus支持其它格式的包

Nexus默认支持jar、war、pom、ejb、ear、rar、par等类型的包,如果希望能添加一些其它类型的包,可以配置如下:

在${nexus-work}/conf/packaging2extension-mapping.properties配置文件中增加配置:

my-custom-packaging=myextension

如果该配置文件不存在则手动创建该文件。

4、更新仓库索引

1)强制更新索引

nexus索引经常会出现与远程仓库索引不一致的情况,导致某些包不能正常下载,可以强制更新仓库索引,保持索引同步:

  • 以管理员身份登录
  • 右键点击仓库,选择Expire Cache

    

  • 右键点击仓库,选择Update Index

    

2)定时更新索引

  • 以管理员身份登录
  • 左侧导航栏选择Administration->Scheduled Tasks

    

  • 点击add添加一个计划任务

    

  • 任务类型选择:Update Repositories Index,Repository/Group选择要更新的仓库,Recurrence选择计划任务的周期类型

5、调试组件下载过程

如果Nexus本地索引中包含组件,但是却不能下载,可能的原因如下:

  • 包含组件的组件库不在group类型的组件库中
  • 路由规则封锁了组件的下载

Nexus提供了可以调试组件下载过程的方法,在组件的下载全路径后面增加参数“?describe”,示例如下:

http://192.168.120.202:8081/nexus/content/groups/public/org/codehaus/mojo/mojo-parent/30/mojo-parent-30.pom?describe

http://localhost:8081/nexus/content/groups/public/foo/bar/1.0/bar-1.0.jar?describe

Nexus将会以json的形式返回调试的输出结果,结果主要包括:

  • 组件的地址
  • 组件是否可以找到以及原因
  • 如果能找到组件,统计查找组件的过程

示例返回结果如下:

    {
      "data":{
        "requestUrl":"http://localhost:8081/nexus/content/groups/public/test/project/1.0/project-1.0.jar?describe",
        "processingTimeMillis":471,
        "request":{
          "requestUrl":"http://localhost:8081/nexus/content
     /groups/public/test/project/1.0/project-1.0.jar?describe",
          "requestPath":"/test/project/1.0/project-1.0.jar",
          "requestContext":[
            "request.received.timestamp=1276286536595",
            "request.address=0:0:0:0:0:0:0:1%0",
            "request.remoteOnly=false",
            "request.url=http://localhost:8081/nexus/content/groups/public/test/project/1.0/project-
     1.0.jar?describe",
            "request.localOnly=false",
            "request.appRootUrl=http://localhost:8081/nexus"
          ]
        },
        "response":{
          "responseType":"NOT_FOUND",
          "processedRepositoriesList":[
            "public",
            "snapshots",
            "thirdparty",
            "central",
            "java.net-m2",
            "java.net-m1-m2",
            "google",
            "apache-snapshots",
            "codehaus-snapshots"
          ],
          "appliedMappings":[
            "public repository applied [11b647d6117038d8=
     [type=EXCLUSION, groupId=public, patterns=[.*/project/.*], mappedRepositories=[releases]]]"
          ]
        }
      }
    }

注意查看其中的response节点,如上例中的"responseType":"NOT_FOUND"表示未找到组件,通过该方法可以详细排查组件不能下载的原因。

再提供一个pom类型的包“mojo-parent-30.pom”的调试示例

    {
        "data": {
            "requestUrl": "http://192.168.120.202:8081/nexus/content/groups/public/org/codehaus/mojo/mojo-parent/30/mojo-parent-30.pom?describe", 
            "processingTimeMillis": 1322, 
            "request": {
                "requestUrl": "http://192.168.120.202:8081/nexus/content/groups/public/org/codehaus/mojo/mojo-parent/30/mojo-parent-30.pom?describe", 
                "requestPath": "/org/codehaus/mojo/mojo-parent/30/mojo-parent-30.pom", 
                "requestContext": [
                    "request.remoteOnly=false", 
                    "request.localOnly=true"
                ]
            }, 
            "response": {
                "responseType": "FILE", 
                "responseActualClass": "org.sonatype.nexus.proxy.item.DefaultStorageFileItem", 
                "responsePath": "/groups/public/org/codehaus/mojo/mojo-parent/30/mojo-parent-30.pom", 
                "responseUid": "central:/org/codehaus/mojo/mojo-parent/30/mojo-parent-30.pom", 
                "originatingRepositoryId": "central", 
                "originatingRepositoryName": "Central", 
                "originatingRepositoryMainFacet": "org.sonatype.nexus.proxy.maven.MavenProxyRepository", 
                "processedRepositoriesList": [ ], 
                "properties": [
                    "created=1333560570000", 
                    "modified=1333560570000", 
                    "lastRequested=1375255131976", 
                    "remoteChecked=1375255131556", 
                    "remoteUrl=http://repo1.maven.org/maven2/org/codehaus/mojo/mojo-parent/30/mojo-parent-30.pom", 
                    "storedLocally=1375255131556", 
                    "isExpired=false", 
                    "readable=true", 
                    "writable=true", 
                    "virtual=false"
                ], 
                "attributes": [
                    "digest.md5=61a1f7765d45a51a6a7bb03bf410588f", 
                    "digest.sha1=be742febdaa3cc7a971dacdf8cdc7ebd94b5765b", 
                    "remote.sha1=be742febdaa3cc7a971dacdf8cdc7ebd94b5765b", 
                    "request.address=192.168.120.121", 
                    "request.user=anonymous", 
                    "storageItem-checkedRemotely=1375255131556", 
                    "storageItem-created=1333560570000", 
                    "storageItem-expired=false", 
                    "storageItem-generation=2", 
                    "storageItem-lastRequested=1375255131976", 
                    "storageItem-length=26627", 
                    "storageItem-modified=1333560570000", 
                    "storageItem-path=/groups/public/org/codehaus/mojo/mojo-parent/30/mojo-parent-30.pom", 
                    "storageItem-readable=true", 
                    "storageItem-remoteUrl=http://repo1.maven.org/maven2/org/codehaus/mojo/mojo-parent/30/mojo-parent-30.pom", 
                    "storageItem-repositoryId=central", 
                    "storageItem-storedLocally=1375255131556", 
                    "storageItem-writable=true"
                ]
            }
        }
    }

 

posted @ 2013-08-01 16:48  没头脑的土豆  阅读(6911)  评论(5编辑  收藏  举报