美文网首页我爱编程Maven
使用私服——Nexus

使用私服——Nexus

作者: SonyaBaby | 来源:发表于2018-06-27 16:13 被阅读0次

Nexus的安装和启动

Nexus拥有全面的权限控制功能。默认的Nexus访问为匿名,仅包含一些最基本的权限。我们以管理员方式登录。

仓库与仓库组

内置的仓库

内置的仓库.png

仓库属性:
1.仓库类型(Type):

  • group 仓库组
  • hosted 宿主
  • proxy 代理
  • virtual 虚拟

2.仓库格式(Format):

  • maven2
  • maven1

3.策略(Policy):

  • Realease 发布版本
  • Snapshot 快照版本

最后两列为仓库的状态和路径。

仓库:

  • Central 该仓库代理Maven中央仓库,策略为Release,只会下载和缓存中央仓库中的发布版本构件。
  • Releases 策略为Release的宿主类型仓库,用来部署内部的发布版本构件。
  • Snapshots 策略为Snapshot的宿主类型仓库,用来部署组织内部的快照版本构件。
  • 3rd party 策略为Release的宿主类型仓库,用来部署无法从公共仓库获得的第三方发布版本构件。
  • Apache Snapshots 策略为Snapshot的代理仓库,用来代理Apache Maven仓库的快照版本构件。
  • Public Repositories 该仓库将所有策略为Release的仓库聚合,通过一致的地址提供服务。

Central仓库会被用来代理中央仓库的内容,并在私服上缓存下来。但是当Maven项目X依赖于某个Google Code的项目,其构件在中央仓库不存在,那我们需要添加Google Code代理仓库。如果X还依赖于Oracle的JDBC驱动,由于版权因素,无法从公共仓库获得,公司管理员将其部署到3rd party宿主仓库中,供X使用。X的快照版本成功后,X构件会被部署到Snapshots宿主仓库中,供其他项目使用。X的正式版本发布之后,其构件会被部署到Releases宿主仓库。由于X用到了很多仓库,为每个仓库声明Maven配置很麻烦,可以直接使用仓库组Public Repositories。可以根据需要进行配置,如下图:


Public Repositories配置.png

仓库分类

各种仓库类型.png

1.Maven可以直接从宿主仓库下载构件
2.Maven从代理仓库下载构件,代理仓库间接地从远程仓库下载和缓存构件。
3.Maven可以通过仓库组下载构件,但是仓库组没有实际内容(虚线表示),它会转向其他仓库获取实际构件的内容。

创建宿主仓库

点击Hosted Repo.png

可以看到配置界面:

创建Nexus宿主仓库.png
  • Provider :一般选择默认的Maven2 Repository
  • Repository Policy: 快照还是发布策略
  • Default Local Storage Location:表示该仓库的默认存储目录,若为空,则会基于sonatype-work,生成一个目录,sonatype-work/nexus/storage/repository-id/
sonatype-work.png
  • Override Local Storage Location:可以用来配置自定义的仓库目录位置
  • Deployment Policy:一般是选择允许部署,还有种是Read Only,只读。
  • Allow File Browsing:是否允许浏览仓库内容。可以以树形结构浏览仓库存储文件的内容。
Browse Storage.png
  • Include in Search:是否对仓库进行索引并提供搜索。
  • Publish URL:是否通过URL提供服务。如果为false,访问该仓库地址时,会得到HTTP 404 Not Found错误。
  • Not Found Cache TTL:当一个文件没有被找到,缓存这一不存在信息的时间,默认值为1440分钟,在之后的1440分钟之后再访问该文件,将直接返回不存在,而不会查找文件系统。

创建代理仓库

Proxy Repo.png
  • 最重要的是远程仓库的地址Remote Storage Location,必须是有效的值。
  • Download Remote Indexes 是否下载远程仓库的索引。下载索引,即使没有缓存远程仓库的构件,还是可以在本地搜索和浏览构件的基本信息。
  • Checksum Policy:配置校验和出错时的策略。
  • Authentication配置,为可选项。当远程仓库需要认证的时候,需要做配置。


    Authentication.png

可选HTTP Request Settings,配置Nexus访问远程仓库时HTTP请求的参数:

HTTP Request Settings.png

创建仓库组

group Repo.png

Ordered Group Repo包含的仓库的顺序决定了遍历仓库的数序,将常用的可以放在前面。


索引与构件搜索

为了可以搜索Maven中央仓库,将仓库Central的Download Remote Indexes值改为true


Central.png

修改后,我们可以从左侧导航栏Scheduled Tasks进入任务列表


导航栏.png Scheduled Tasks.png

下载index过程中,Status为RUNNING,等下载完毕后,该任务就会消失。我们就可以在Nexus中快速搜索构件了~

  • GAV搜索(GAV Search):通过设置GroupId、ArtifactId、Version信息进行针对性搜索。
  • 类名搜索(Classname Search):允许用户搜索包含某个Java类的构件
  • 校验和搜索(Checksum Search): 允许直接使用构件的校验和搜所该构件。


    search.png

有了中央仓库的索引,不仅能够搜索构件,还能够直接浏览中央仓库的内容。即索引浏览功能。


Browse Remote.png

当然我们也可以为宿主仓库和代理仓库创建索引


建立索引.png

配置Maven从私服Nexus上下载构件

为项目POM添加上Nexus私服上的Public仓库信息:

<!-- Maven仓库 -->
<repositories>
    <repository>
        <id>nexus</id>
        <name>Nexus</name>
        <url>http://localhost:8081/nexus/content/groups/public/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>nexus</id>
        <name>Nexus</name>
        <url>http://localhost:8081/nexus/content/groups/public/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

这样的配置只对当前Maven项目有效。希望通过一次配置就能让本机所有的Maven项目都使用自己的Maven私服。Maven提供了Profile机制,能让用户将仓库配置放到setting.xml的Profile中

<profiles>
   <profile>
     <id>nexus</id>
     <repositories>
       <repository>
         <id>nexus</id>
         <name>Nexus</name>
         <url>http://localhost:8081/nexus/content/groups/public/</url>
         <releases>
           <enabled>true</enabled>
         </releases>
         <snapshots>
           <enabled>true</enabled>
         </snapshots>
       </repository>
     </repositories>
     <pluginRepositories>
       <pluginRepository>
         <id>nexus</id>
         <name>Nexus</name>
         <url>http://localhost:8081/nexus/content/groups/public/</url>
         <releases>
           <enabled>true</enabled>
         </releases>
         <snapshots>
           <enabled>true</enabled>
         </snapshots>
       </pluginRepository>
     </pluginRepositories>
   </profile>
</profiles>

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

可以通过配置镜像,使所有的请求都只通过私服。

<mirror>
     <id>nexus</id>
     <url>http://localhost:8081/nexus/content/groups/public/</url>      
     <mirrorOf>*</mirrorOf>      
</mirror>  

<profiles>
   <profile>
     <id>nexus</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>
</profiles>

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

需要注意的是,这里仓库和插件仓库的id都为central,也就是说覆盖了超级POM中央仓库的配置,它们的url已无关紧要,因为所有请求都会通过镜像访问私服。但是依然配置仓库和插件仓库主要是为了开启对快照版本下载的支持。Maven需要下载发布版本/快照版本时,首先检查central,看该类型是否支持,得到正面回答后,根据镜像匹配规则转而访问私服仓库。


部署构件至Nexus

Nexus仓库对于匿名用户是只读的。为了能够部署构件,还需要在settings.xml中配置认证信息。

<servers>
<server>
  <id>releases</id>
   <username>admin</username>
   <password>xxxxxx</password>
</server>
<server>
  <id>snapshots</id>
   <username>admin</username>
   <password>xxxxxx</password>
</server>

当然我们也可以手动上传构件至Nexus,例如上传第三方构件:

Artifact Upload.png

Nexus权限管理

Nexus是基于权限做访问控制的。
参考《Maven实战》9.7

相关文章

  • maven私服

    使用私服 Maven 自动打包上传到私服 Nexus 自动打包上传私服(nexus3.X版本改了很多) Repos...

  • Maven搭建私服Nexus

    一、搭建Maven私服,使用Nexus搭建1、下载Nexus,点击nexus下载,下载nexus-2.13.0-0...

  • maven

    仓库地址 : 1、http://www.sonatype.org/nexus/ 私服nexus工具使用 2、htt...

  • MAVEN中央仓库地址大全

    Maven 中央仓库地址: 1、私服nexus工具使用 http://www.sonatype.org/nexus...

  • maven中央仓库大全

    Maven 中央仓库地址: 1、私服nexus工具使用 http://www.sonatype.org/nexus...

  • 使用 Nexus 搭建 Maven 私服

    使用 Nexus 搭建 Maven 私服 一、Nexus 服务的安装 Nexus 既可以使用传统的二进制包进行安装...

  • 使用Nexus2搭建私有库

    本文梳理了一些使用Nexus搭建Maven私服的方法。Maven私服Nexus的作用,主要是为了节省资源,在内部作...

  • Maven实战之nexus

    使用专门的Maven仓库管理软件Nexus构建Maven私服。 nexus下载地址 https://www.son...

  • java Nexus私服

    Nexus私服

  • 使用私服——Nexus

    Nexus的安装和启动 Nexus拥有全面的权限控制功能。默认的Nexus访问为匿名,仅包含一些最基本的权限。我们...

网友评论

    本文标题:使用私服——Nexus

    本文链接:https://www.haomeiwen.com/subject/azbpyftx.html