0
点赞
收藏
分享

微信扫一扫

maven私服搭建

Resin_Wu 2022-04-25 阅读 94
maven
本文在macbook pro 环境下搭建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

在这里插入图片描述

  • 登录
    默认管理员帐号为admin
    在这里插入图片描述
    查看密码
cat /usr/local/var/nexus/admin.password

三、nexus的配置

3.1登录后开始设置操作

在这里插入图片描述

3.1.1设置新密码

在这里插入图片描述

3.1.2配置匿名访问

在这里插入图片描述

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

阿里云公共仓库添加
在这里插入图片描述
将阿里云公共仓库添加到maven-public这个group类型的仓库,并调整优先级在这里插入图片描述

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

3.3获取私服地址

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

在这里插入图片描述

四、maven使用私服jar

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

maven配置私服有两种方式

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

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

4.1全局模式

   <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项目独享模式

  <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>

五、打包本地jar上传私服

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来搭建maven私服,介绍安装,配置,使用的方法。

举报

相关推荐

0 条评论