nexus 搭建 maven 私服


本机环境

Win 8  JDK 7  Maven 3.2  Nexus 2.11

版本选择

http://www.sonatype.org/nexus/archived
nexus 2.6 及 2.6+ 版本要求 jdk 1.7 或 1.7+ 版本(jdk 1.6 请选择 nexus 2.5 及 以下版本)。根据你现有的环境来选择合适的版本。
这里选择  nexus-2.11.0 (如果无法打开下载, 可将新标签页地址栏中的地址复制到迅雷等下载工具中进行下载)。
下载得到 nexus-2.11.0-bundle.zip 压缩包。解压缩里面的 nexus-2.11.0-02 目录,如我的 E:\setup\dev\servers\nexus, 最终得到
的目录结构为 E:\setup\dev\servers\nexus\nexus-2.11.0-02。

配置 nexus 环境

NEXUS_HOME = E:\setup\dev\servers\nexus\nexus-2.11.0-02
Path = .;%NEXUS_HOME%\bin

安装 nexus 服务

如果你所使用的是 Win8 操作系统,以下操作需要管理员权限。在命令行中执行  nexus install(Win8 系统组合键为 Ctrl + X, A):
C:\WINDOWS\system32> nexus install
wrapper | nexus installed.

启动 nexus

在命令行中执行  nexus start
C:\WINDOWS\system32> nexus start
wrapper | Starting the nexus service...
wrapper | Waiting to start...
wrapper | Waiting to start...
wrapper | Waiting to start...
wrapper | nexus started.
页面访问的入口地址: http://localhost:8081/nexus

登录 nexus

默认的管理员账号是:admin, 密码是:admin123

停止 nexus

在命令行中执行  nexus stop
C:\Users\fanlychie> nexus stop
wrapper | Stopping the nexus service...
wrapper | Waiting to stop...
wrapper | nexus stopped.

修改 nexus 账号密码

先停止 nexus,打开 %NEXUS_HOME%\sonatype-work\nexus\conf\security.xml,修改即可。
nexus 的密码采用 SHA1 加密算法 (  在线加密工具 ),将加密后的 SHA1 串(小写)拷贝覆盖原来的。重启 nexus: nexus restart 。
< user >
   < id >admin </ id >
   < firstName >Administrator </ firstName >
   < lastName >User </ lastName >
   < password >2899441143e28aee7287e8da3d7211258f5edbd3 </ password >
   < status >active </ status >
   < email >fanlychie@yeah.net </ email >
</ user >

修改 nexus 端口

打开 %NEXUS_HOME%\nexus-2.11.0-02\conf\nexus.properties, 修改 application-port 的值。重启 nexus: nexus restart 。
application-port=8081
application-host=0.0.0.0
nexus-webapp=${bundleBasedir}/nexus
nexus-webapp-context-path=/nexus

仓库类型

打开 Views/Repositories --> Repositories 视图 

nexus 仓库分为 4 种,group(仓库组)、hosted(宿主仓库)、proxy(代理仓库)、virtual(虚拟仓库)。
我们自己开发的构件通常是发布到 hosted 仓库,proxy 用来代理远程的公共仓库,一个 group 可以包含多个 hosted/proxy 仓库。

仓库组

打开 Views/Repositories --> Repositories --> Public Repositories --> Configuration 视图 

Configuration 栏的左边是仓库组仓库列表,右边是当前可选的仓库,可以从右边挑选合适的仓库加入到左边的仓库组,点击 Save 保存即可。
一个仓库组通常包含了多个仓库,仓库组中的仓库列表的顺序决定了构件下载时遍历的仓库的先后次序,因此,建议将远程中央仓库(Central)放
到仓库组的最后一项。

maven settings.xml 配置

在 settings.xml 配置文件中添加如下配置:
< servers >
   < server >
     < id >releases </ id >
     < username >admin </ username >
     < password >admin123 </ password >
   </ server >
   < server >
     < id >snapshots </ id >
     < username >admin </ username >
     < password >admin123 </ password >
   </ server >
   < server >
     < id >thirdparty </ id >
     < username >admin </ username >
     < password >admin123 </ password >
   </ server >
</ servers >
< mirrors >
   < mirror >
     < id >nexus </ id >
     < mirrorOf >* </ mirrorOf >
     < name >nexus public repositories </ name >
     < url >http://127.0.0.1:8088/nexus/content/groups/public </ url >
   </ mirror >
</ mirrors >
<server> 节点配置服务的账户密码,用于发布构件时进行身份和权限的认证。<mirror> 节点用于镜像的配置,具体用法参考  maven 镜像配置

发布构件

在 nexus 中找到如下的视图页面:

复制以上的配置,粘贴到你的 pom.xml 配置文件中: 
< project  xmlns ="http://maven.apache.org/POM/4.0.0"  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation
="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
   < modelVersion >4.0.0 </ modelVersion >
   < groupId >org.fanlychie </ groupId >
   < artifactId >proj </ artifactId >
   < version >1.0.0 </ version >
   < packaging >jar </ packaging >
   < name >proj </ name >
   < url >http://maven.apache.org </ url >
   < distributionManagement >
     < repository >
       < id >releases </ id >
       < url >http://localhost:8088/nexus/content/repositories/releases </ url >
     </ repository >
   </ distributionManagement >
</ project >
这里需要注意的是,repository 节点的 id 需与 settings.xml 中配置的 server 节点的 id 相同,如果不相同,修改任意一方都可以,只
要让它们保持一致即可,否则,发布构件的时候会报 401,ReasonPhrase:Unauthorized 的错误,原因是无法认证用户的身份。
右键项目,Run As --> Maven build...,在 Goals 栏输入  deploy 或在命令行(cmd)执行  mvn deploy,在控制台若能看到
BUILD SUCCESS,表明构件发布成功。你可以在 nexus 的 Releases 仓库中找到这个构件:

发布构件的时候,如果想把源码也一起发布出去(执行  mvn dependency:sources 可获得源码),配置如下: 
< project  xmlns ="http://maven.apache.org/POM/4.0.0"  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation
="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
   < modelVersion >4.0.0 </ modelVersion >
   < groupId >org.fanlychie </ groupId >
   < artifactId >proj </ artifactId >
   < version >1.0.1 </ version >
   < packaging >jar </ packaging >
   < name >proj </ name >
   < url >http://maven.apache.org </ url >
   < distributionManagement >
     < repository >
       < id >releases </ id >
       < url >http://localhost:8088/nexus/content/repositories/releases </ url >
     </ repository >
   </ distributionManagement >
   < build >
     < plugins >
       < plugin >
         < groupId >org.apache.maven.plugins </ groupId >
         < artifactId >maven-source-plugin </ artifactId >
         < version >2.4 </ version >
         < executions >
           < execution >
             < id >attach-sources </ id >
             < phase >install </ phase >
             < goals >
               < goal >jar-no-fork </ goal >
             </ goals >
           </ execution >
         </ executions >
       </ plugin >
     </ plugins >
   </ build >
</ project >
同一个构件是不允许发布两次的,先修改一下构件的版本,然后  deploy,结果如图: 

发布快照

快照用于区分稳定与不稳定的构件(发布的版本就是稳定的,快照版本是不稳定的)。构件的升级通常会带来许多的不稳定性,需要不断的修复,
快照可以避免由于升级带来的不稳定性迫使的不停的升级版本号,最终造成版本号的泛滥的问题。快照允许重新发布,而不需要变更构件的版本号。
在 nexus 中找到如下的视图页面: 

复制以上的配置,粘贴到你的 pom.xml 配置文件中: 
< project  xmlns ="http://maven.apache.org/POM/4.0.0"  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation
="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
   < modelVersion >4.0.0 </ modelVersion >
   < groupId >org.fanlychie </ groupId >
   < artifactId >proj </ artifactId >
   < version >2.0.0-SNAPSHOT </ version >
   < packaging >jar </ packaging >
   < name >proj </ name >
   < url >http://maven.apache.org </ url >
   < distributionManagement >
     < repository >
       < id >releases </ id >
       < url >http://localhost:8088/nexus/content/repositories/releases </ url >
     </ repository >
     < snapshotRepository >
       < id >snapshots </ id >
       < url >http://localhost:8088/nexus/content/repositories/snapshots </ url >
     </ snapshotRepository >
   </ distributionManagement >
   < build >
     < plugins >
       < plugin >
         < groupId >org.apache.maven.plugins </ groupId >
         < artifactId >maven-source-plugin </ artifactId >
         < version >2.4 </ version >
         < executions >
           < execution >
             < id >attach-sources </ id >
             < phase >install </ phase >
             < goals >
               < goal >jar-no-fork </ goal >
             </ goals >
           </ execution >
         </ executions >
       </ plugin >
     </ plugins >
   </ build >
</ project >
版本号含有 SNAPSHOT 的构件将发布到 Snapshots 仓库,否则,将发布到 Releases 仓库。执行发布命令: mvn deploy,结果如图: 

发布第三方构件

步骤见下图的标注: 

命令行方式发布:
mvn deploy:deploy-file -DgroupId=自定义groupId -DartifactId=自定义artifactId -Dversion=版本号 -Dpackaging=jar 
-Dfile=JAR文件的路径 -Durl=http://127.0.0.1:8088/nexus/content/repositories/thirdparty -DrepositoryId=thirdparty

远程索引

在 nexus 中找到如下的视图页面: 

保存之后,在左侧面板中选择 Administration --> Scheduled Tasks,如下图: 

可以看到这里有一个 Repair Repositories Index 任务,任务完成之后会自动从面板中移除,点击左上角的 Refresh 按钮来查看。任务完成
之后回到 Repositories 面板,选择 Central 仓库右键,选择 Update Index,再回到 Scheduled Tasks 面板,可以看到有一个 Update 
Repositories Index 任务,这个任务花时较长,任务完成之后,回到 Repositories 面板,在 Browse Index 中可以看到从远程仓库下载回
来的索引文件,有了索引,即使 nexus 私服还没有你想要构件,你也可以搜索出你想要查找的构件。 



配置远程仓库的镜像

镜像通常可以提供比远程中央仓库更好的服务,提升构件下载的速度。
在 settings.xml(%MAVEN_HOME%\conf\settings.xml)配置文件中找到 <mirrors> 节点,添加如下的配置: 
< mirrors >
   < mirror >
     < id >ibiblio </ id >
     < mirrorOf >central </ mirrorOf >
     < name >mirrors repositories from central </ name >
     < url >http://mirrors.ibiblio.org/pub/mirrors/maven2 </ url >
   </ mirror >
</ mirrors >
id:随意,唯一即可; name:随意,只是对镜像的一个描述; url:镜像的地址。
mirrorOf:当 mirrorOf 与仓库的 id 匹配时(maven 默认的远程中央仓库的 id 是 central),所有远程仓库的请求都转成对该镜像配置的
url 的请求。当一个仓库的 id 与多个镜像的 mirrorOf 同时匹配时,则优先选取与仓库 id 完全一致的镜像,如果没有,则按照镜像配置的先
后顺序选取第一个镜像作为服务的提供者。下面几个是常见的配置 mirrorOf 的用法:
<mirrorOf>*</mirrorOf>           匹配所有的远程仓库
<mirrorOf>repo1,repo2</mirrorOf> 匹配 id 为 repo1 和 repo2 的远程仓库(仓库之间使用 "," 隔开)
<mirrorOf>external:*</mirrorOf>  匹配除本地仓库之外的所有其他仓库
<mirrorOf>*,!repo1</mirrorOf>    匹配除 repo1 之外的所有其他仓库(使用 "!" 可将仓库从匹配中排除)

附收藏的几个镜像地址列表

http://maven.antelink.com/content/repositories/central
http://maven.tmatesoft.com/content/groups/public
http://mirrors.ibiblio.org/pub/mirrors/maven2
http://uk.maven.org/maven2


转自:http://www.blogjava.net/fancydeepin/archive/2015/06/27/maven-nexus.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
搭建Maven私服可以使用Sonatype Nexus,它是一个功能强大的仓库管理器,可以帮助我们创建和管理Maven私服。下面是基于Nexus搭建Maven私服的详细步骤: 1. 准备环境:首先,确保你已经安装了Java环境,并且可以正常运行MavenNexus软件。 2. 下载和解压缩Nexus软件:从Sonatype官方网站下载最新版本的Nexus软件,并解压缩到合适的目录中。 3. 启动Nexus:使用命令行窗口进入Nexus软件的目录,然后执行 `./bin/nexus start` 命令来启动Nexus服务器。 4. 访问Nexus控制台:在浏览器中输入 `http://localhost:8081` 访问Nexus控制台,默认用户名和密码都是 `admin`,登录成功后,可以看到Nexus的管理界面。 5. 创建Maven仓库:在Nexus控制台中,点击左侧菜单的 `Repositories`,然后点击 `Create repository` 来创建新的Maven仓库。根据需要选择 `Hosted repository` 或者 `Proxy repository`,然后填写相应的配置信息,如仓库名称、URL、布局等。 6. 配置Maven项目:在你的Maven项目的pom.xml文件中添加Nexus私服的配置信息,包括ID、URL等,用来指定将Maven构建输出部署到Nexus仓库中。 7. 部署和使用:使用 `mvn deploy` 命令将项目打包并部署到Nexus私服中,当其他开发人员或者CI/CD服务器需要下载依赖时,只需要修改项目的settings.xml文件,指定Nexus私服的URL和凭据即可。 8. 其他设置:你还可以在Nexus控制台里进行其他设置,比如用户管理、访问控制、仓库代理等。 总结一下,使用Nexus搭建Maven私服非常简单,只需要几个基本的步骤即可完成。通过搭建Maven私服,我们可以有效地管理和共享项目的依赖包,提升团队的开发效率和项目的可维护性。这对于生产环境中的项目非常重要。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值