maven私服搭建

前言

本文在mac环境下搭建maven私服,将基础代码打包部署,供团队成员临时下载使用(长期的话可以向公司申请一台服务器安装部署),增加代码的复用性和团队成员的代码规范。
maven私服搭建所需的开源软件有Apache Archiva,Artifactory,Nexus等。Nexus日趋成为最流行的maven仓库管理器,这里选择Nexus(3.37.3-02)。

一、Nexus

  • Nexus是一个强大的Maven仓库管理器,它极大地简化了自己内部仓库的维护和外部仓库的访问。
  • 利用Nexus你可以只在一个地方就能够完全控制访问和部署在你所维护仓库中的每个Artifact。
  • Nexus是一套“开箱即用”的系统不需要数据库,它使用文件系统加Lucene来组织数据。
  • Nexus 使用ExtJS来开发界面,利用Restlet来提供完整的REST APIs,通过m2eclipse与Eclipse集成使用。
  • Nexus支持WebDAV与LDAP安全身份认证。

二、安装nexus

2.1在Mac终端输入(如果提示没有brew命令,请先安装brew)

brew install nexus

2.2启动nexus

brew services start nexus

2.3访问web管理系统

  • 访问127.0.0.1:8081
    nexus

  • 登录
    默认管理员帐号为admin
    nexus
    查看密码

cat /usr/local/var/nexus/admin.password

三、nexus的配置

3.1登录后开始设置操作

nexus

3.1.1设置新密码

nexus

3.1.2配置匿名访问

nexus

3.2设置 proxy 代理仓库

  • 默认仓库有哪些呢?

    • maven-central:maven中央库,默认从https://repo1.maven.org/maven2/拉取jar
    • maven-releases:私库发行版
    • maven-snapshots:私库快照(调试版本)
    • maven-public:仓库分组(把上面三个仓库组合在一起对外提供服务)
    • 自己也可以选择仓库类型进行创建
  • nexus仓库类型主要分为三种

    • proxy 远程代理仓库jar放置的目录
    • hosted 本地仓库jar放置的目录,我们一般部署自己的jar到这个类型的仓库
    • group远程代理仓库jar和本机仓库jar
  • 添加阿里云公共仓库
    nexus

  • 将阿里云公共仓库添加到maven-public这个group类型的仓库,并调整优先级nexus

  • 私有仓库(hosted)不用创建 , 使用原有的 maven-releases 和 maven-snapshots即可

3.3获取私服地址

http://0.0.0.0:8081/repository/maven-public/

nexus

四、maven使用私服jar

使用前注意:请自行下载maven客户端。

maven配置私服有两种方式

  • 全局模式,通过配置maven的配置文件 setting.xml
  • 项目独享模式,通过配置项目中的pom.xml

需要项目中使用jar 的配置过程: 先要引入jar包,然后需要在 setting.xml或者pom.xml文件中添加资源地址.

4.1全局模式

maven的配置文件 setting.xml

   <repositories>
       <repository>
           <id>local-center</id>
           <url>http://127.0.0.1:8081/repository/maven-public/</url>
           <releases>
               <enabled>true</enabled>
           </releases>
           <snapshots>
               <enabled>true</enabled>
           </snapshots>
       </repository>
   </repositories>

4.2项目独享模式

项目中的pom.xml

  <profiles>
  
		<profile>
            <id>local-center</id>
            <repositories>
                <repository>
                	<id>nexus-releases</id>
                    <name>nexus-releases</name>
                   <url>http://127.0.0.1:8081/repository/maven-public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>nexus-releases</id>
                    <name>nexus-releases</name>
                   <url>http://127.0.0.1:8081/repository/maven-public/</url>
                </pluginRepository>
            </pluginRepositories>
        </profile>
  </profiles>
  <activeProfiles>
	 <activeProfile>local-center</activeProfile>
  </activeProfiles>

或者换一种写法

    <repositories>
        <repository>
            <id>nexus</id>
            <url>http://127.0.0.1:8081/repository/maven-public/</url>
            <releases>
                <updatePolicy>never</updatePolicy>
            </releases>
            <snapshots>
                <updatePolicy>always</updatePolicy>
            </snapshots>
        </repository>
    </repositories>

五、打包本地jar上传私服

本地jar上传到私服,有两种方式,一种是maven客户端配置,deploy上传;一种是在管理后台手动上传。下面分别介绍这两种方法。

5.1maven客户端上传

5.1.1.需要配置maven的setting.xm

在servers 标签中添加配置

<servers>	
<server>
	 <!-- 与 pom文件中的 distributionManagement id对应 -->
		<id>nexus-releases</id>
		<username>admin</username>
		<password>admin123</password>
    </server>
	
	<server>
	  <id>nexus-snapshots</id>
	  <username>admin</username>
	  <password>admin123</password>
    </server>
 
	
  </servers>

5.1.2.在项目中配置

需要在 pom文件中配置加入

    <distributionManagement>
        <repository>
            <!-- 与 maven setting 文件中的 server id对应 -->
            <id>nexus-releases</id>
            <url>http://127.0.0.1:8081/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <url>http://127.0.0.1:8081/repository/maven-snapshots/</url>
            <uniqueVersion>true</uniqueVersion>
        </snapshotRepository>
    </distributionManagement>

5.1.3.发布打包

  • 执行 mvn clean package -Dmaven.test.skip=true deploy
  • 执行成功后私服中查看

5.2私服web地址上传

进入上传页面进行上传操作
nexus


总结

本文主要是用nexus来搭建maven私服,介绍安装配置及使用的方法。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

blackoon88

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值