Maven实战(三)--Nexus私服

Maven实战(三)–Nexus私服

一、搭建Nexus私服

  为什么要搭建nexus私服,原因很简单,有些公司都不提供外网给项目组人员,因此就不能访问maven中央仓库,或者公司内部的jar包在外网无法找到,所以很有必要在局域网里使用一台有外网权限的机器,搭建nexus私服,然后开发人员连到这台私服上,这样的话就可以通过这台搭建了nexus私服的电脑访问maven的远程仓库,或者从上面下载内部jar包,使得开发人员可以下载仓库中的内容,而且对于下载过的文件,局域网内下载会更加快速。还有一点优势在于,我们需要的jar包可能在中央仓库中没有,需要去其他地方下载,有了中央仓库,只需要一人找到jar包其他人就不用再去上网搜索jar包,十分方便。

二、下载Nexus

官网地址:http://www.sonatype.org/nexus
下载地址:http://www.sonatype.org/nexus/archived/
官方文档:http://books.sonatype.com/nexus-book/3.0/reference/install.html

这里写图片描述

三、Zip安装配置

1、解压

将zip解压到合适目录,如我解压后的情况如下:
这里写图片描述
解压 nexus-2.12.0-01-bundle.zip,进入 bin 目录:
管理员cmd 进入 bin 目录,执行 nexus install 安装
nexus start 启动服务
nexus.bat uninstall   卸载 nexus
http://localhost:8081/nexus/ 访问路径

2.配置
这里写图片描述

# Jetty section
application-port=8081   # nexus 的访问端口配置 application-
application-host=0.0.0.0    # nexus 主机监听配置(不用修改) nexus-
nexus-webapp=${bundleBasedir}/nexus    # nexus 工程目录 nexus-webapp- 
nexus-webapp-context-path=/nexus   # nexus 的 web 访问路径 

# Nexus section
nexus-work=${bundleBasedir}/../sonatype-work/nexus
runtime=${bundleBasedir}/nexus/WEB-INF

使用 Nexus 内置账户 admin/admin123 登陆:

仓库类型
这里写图片描述
 central:代理仓库,代理中央仓库
 apache-snapshots:代理仓库 存储 snapshots 构件,代理地址 https://repository.apache.org/snapshots/
 central-m1:virtual 类型仓库,兼容 Maven1 版本的 jar 或者插件
 releases:本地仓库,存储 releases 构件。
 snapshots:本地仓库,存储 snapshots 构件。
 thirdparty:第三方仓库
 public:仓库组

四、 将项目发布到私服

第一步: 需要在客户端即部署 maven-dao 工程的电脑上配置 maven 环境,并修改
settings.xml 文件,配置连接私服的用户和密码 。
此用户名和密码用于私服校验,因为私服需要知道上传的账号和密码是否和私服中的账号和密码一致。

<server> 
      <id>releases</id> 
      <username>admin</username> 
      <password>admin123</password> 
    </server> 
    <server> 
      <id>snapshots</id> 
      <username>admin</username> 
      <password>admin123</password> 
    </server> 

releases 连接发布版本项目仓库 snapshots 连接测试版本项目仓库

第二步: 配置项目 pom.xml
配置私服仓库的地址,本公司的自己的 jar 包会上传到私服的宿主仓库,根据工程的版本号决定上传到哪个宿主仓库,如果版本为 release 则上传到私服的 release 仓库,如果版本为 snapshot 则上传到私服的 snapshot 仓库

<distributionManagement> 
   <repository> 
        <id>releases</id> 
    <url>http://localhost:8081/nexus/content/repositories/releases/</url> 
   </repository>  
   <snapshotRepository> 
        <id>snapshots</id> 
    <url>http://localhost:8081/nexus/content/repositories/snapshots/</url> 
   </snapshotRepository>  
 </distributionManagement> 

注意:pom.xml 这里<id> 和 settings.xml 配置 <id> 对应!
4.3 测试
将项目 dao 工程打成 jar 包发布到私服:
1、首先启动 nexus
2、对 maven-dao 工程执行 deploy 命令

五、从私服下载 jar 包

在 setting.xml 中配置仓库

maven配置文件
在客户端的 setting.xml 中配置私服的仓库,由于 setting.xml 中没有 repositories 的配置标签需要使用 profile 定义仓库。

<profile>    
    <!--profile 的 id--> 
   <id>dev</id>    
    <repositories>    
      <repository>   
        <!--仓库 id,repositories 可以配置多个仓库,保证 id 不重复--> 
        <id>nexus</id>    
        <!--仓库地址,即 nexus 仓库组的地址--> 
        <url>http://localhost:8081/nexus/content/groups/public/</url>    
        <!--是否下载 releases 构件--> 
        <releases>    
          <enabled>true</enabled>    
        </releases>    
        <!--是否下载 snapshots 构件--> 
        <snapshots>    
          <enabled>true</enabled>    
        </snapshots>    
      </repository>    
    </repositories>   
     <pluginRepositories>   
     <!-- 插件仓库,maven 的运行依赖插件,也需要从私服下载插件 --> 
        <pluginRepository>   
         <!-- 插件仓库的 id 不允许重复,如果重复后边配置会覆盖前边 --> 
            <id>public</id>   
            <name>Public Repositories</name>   
            <url>http://localhost:8081/nexus/content/groups/public/</url>   
        </pluginRepository>   
    </pluginRepositories>   
  </profile>   

使用 profile 定义仓库需要激活才可生效。

  <activeProfiles> 
    <activeProfile>dev</activeProfile> 
  </activeProfiles> 

六、 把第三方 jar 包放入本地仓库或私服

1. 导入本地库
随便找一个 jar 包测试,可以先 CMD进入到 jar包所在位置,运行

mvn install:install-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dfile= fastjson-1.1.37.jar -Dpackaging=jar 

2、导入私服
需要在 maven 软件的核心配置文件 settings.xml 中配置第三方仓库的 server 信息

 <server>    
    <id>thirdparty</id>    
    <username>admin</username> 
    <password>admin123</password>    
  </server> 

3同样要 CMD先进入到 jar包所在位置,运行 才能执行以下命令

mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 
-Dpackaging=jar -Dfile=fastjson-1.1.37.jar 
-Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ 
-DrepositoryId=thirdparty 

4. 参数说明
DgroupId 和 DartifactId 构成了该 jar 包在 pom.xml 的坐标,项目就是依靠这两个属性定位。自己起名字也行。

Dfile 表示需要上传的 jar 包的绝对路径

Durl 私服上仓库的位置,打开 nexus——>repositories 菜单,可以看到该路径。

DrepositoryId 服务器的表示 id,在 nexus 的 configuration 可以看到。 Dversion 表示版本信息,
关于 jar包准确的版本 包的名字上一般会带版本号,如果没有那可以解压该包,会发现一个叫 MANIFEST.MF 的文件,这个文件就有描述该包的版本信息。
比如 Specification-Version: 2.2 可以知道该包的版本了。 上传成功后,在 nexus 界面点击 3rd party 仓库可以看到这包。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值