maven项目私服使用

1.maven私服安装
https://blog.youkuaiyun.com/u012385217/article/details/51352104
安装后,maven私服仓库和本地仓库默认是空的,只有当maven项目中的pom.xml或者setting.xml中配置私服地址,就会下载到私服仓库和本地仓库中。
maven私服的作用是:需要的架包从maven官方仓库下载 到私服仓库(具体是中央仓库的id为central)和本地仓库中。
2.maven仓库介绍
maven仓库默认登陆用户名和密码是  admin/admin123 
     登陆后可以上传第三方架包(详情请看maven私服安装)
介绍仓库三种类型:
hosted,本地代理仓库,通常我们会部署自己的构件到这一类型的仓库。 
proxy,代理的远程仓库,它们被用来代理远程的公共仓库,如maven中央仓库。 
group,仓库组,用来合并多个hosted/proxy仓库,通常我们配置maven依赖仓库组

3.maven项目配置私服地址
一般采用有两种配置方法:
1.项目pom.xml中配置私服地址仅对当前项目有效,但若需在其他项目中使用,为避免代码重复性,减少冗余,可在settings.xml文件中配置
(小案例:如果有多个项目,但是多个项目都在一个头部maven项目下,可以将这种私服配置到顶部pom.xml中,可以避免配置代码重复)
pom文件中配置私服仓库:

<repositories>
   <!--  配置nexus远程仓库 -->
   <repository>
      <id>nexus</id>
      <name>Nexus Snapshot Repository</name>
      <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>
      <releases>
         <enabled>true</enabled>
      </releases>
      <snapshots>
         <enabled>true</enabled>
      </snapshots>
   </repository>
</repositories>


<!--  配置从那个仓库下载jar架包 -->
<pluginRepositories>
   <pluginRepository>
      <id>nexus</id>
      <name>Nexus Snapshot Repository</name>
      <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>
      <releases>
         <enabled>true</enabled>
      </releases>
      <snapshots>
         <enabled>true</enabled>
      </snapshots>
   </pluginRepository>
</pluginRepositories>
  // 当上述配置中将releases和snapshots设置true那么需要配置以下内容maven中的仓库分为两种,snapshot快照仓库和release发布仓库。snapshot快照仓库用于保存开发过程中的不稳定版本,release正式仓库则是用来保存稳定的发行版本。定义一个组件/模块为快照版本,只需要在pom文件中在该模块的版本号后加上-SNAPSHOT即可(注意这里必须是大写)
     <distributionManagement>
<snapshotRepository>
<id>snapshots</id>
<url>http://127.0.0.1:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>releases</id>
<url>http://127.0.0.1:8081/nexus/content/repositories/releases</url>
</repository>
  </distributionManagement>

2.setting.xml中配置私服地址
第一步配置仓库:
<profiles>
  
  <profile>
      <id>development</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>
第二步:激活profile(其中上面id和activeProfile相同)
 <activeProfiles>
    <activeProfile>development</activeProfile>
  </activeProfiles>
第三步:配置镜像mirror
 <mirrors>
    <mirror>
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <name>Nexus Mirror</name>
      <url>http://127.0.0.1:8081/content/groups/public/</url>
    </mirror>
  </mirrors>
第四步:添加用户配置(用户名和密码)
 <servers>
    <server>
      <id>development</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
<server>
      <id>Snapshots</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
  </servers>

//具体可以查看该博客:https://blog.youkuaiyun.com/hh12211221/article/details/74217892

### 配置和使用Maven私服 #### 1. Maven查找依赖的流程 当B团队在`pom.xml`配置文件中引入了依赖坐标后,Maven会按照特定顺序来查找该依赖。这一过程首先会在本地仓库寻找所需构件;如果未能找到,则继续访问远程仓库列表中的每一个仓库直到定位到目标构件[^1]。 #### 2. 设置项目级别的POM文件 对于希望部署至私服上的JAR包,在项目的根级`pom.xml`里加入如下片段用于指定发布路径: ```xml <distributionManagement> <repository> <id>maven-releases</id> <url>http://192.168.2.xxx:xxxx/repository/maven-releases/</url> </repository> <snapshotRepository> <id>maven-snapshots</id> <url>http://192.168.2.xxx:xxxx/repository/maven-snapshots/</url> </snapshotRepository> </distributionManagement> ``` 上述代码定义了正式版本(`releases`)以及开发快照版(`snapshots`)所对应的存储位置[^4]。 #### 3. 修改全局Settings.XML以支持认证信息 为了让Maven能够成功连接并操作私服服务器,还需要编辑位于Maven安装目录下的`conf/settings.xml`文档,添加必要的身份验证凭证和其他设置项。这一步骤确保了即使面对受保护资源也能顺利完成下载或上传动作[^3]。 #### 4. 创建新的本地仓库指向私服 考虑到原有默认缓存可能已存在大量数据干扰测试效果,建议创建独立的新建空白文件夹作为临时工作区,并通过命令行参数告知Maven改为此处读写依赖库。这样做有助于更清晰地观察整个构建过程中涉及到了哪些外部资源请求[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值