Maven专题(二) - 仓库与私服

一、本地仓库和远程仓库

maven仓库分为本地仓库和远程仓库,当maven根据坐标寻找构件时,首先会从本地仓库中查找,如果本地仓库中没有,则会去远程仓库中查找,然后下载到本地仓库中再使用。本地仓库的仓库目录默认为用户目录/.m2/repository,可以修改 .m2/setting.xml 文件(默认不存在,需要从maven安装目录下copy一份)的localRepository元素。

<settings>
    <localRepository>D:\maven\repository\</localRepository>
</settings>

很多情况下,默认的中央仓库无法满足项目的需求,可能需要的构件存在于另一个远程仓库,如JBoss Maven仓库,可以在POM中配置该仓库。

<repositories>
  	<repository>
  		<id>jboss</id>
  		<name>JBoss Repository</name>
  		<url>http://repository.jboss.org/maven2</url>
  		<releases>
  			<enabled>true</enabled>
  		</releases>
  		<snapshots>
  			<enabled>false</enabled>
  		</snapshots>
  	</repository>
</repositories>

releases和snapshots中的enabled表示开启JBoss仓库的的发布版本下载,关闭快照版本的下载。

如果一个内部的maven服务器需要认证,可以在settings.xml中配置用户名和密码

<servers>
    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
</servers>

repository信息可以直接在pom.xml中配置,但认证信息必须在settings.xml中配置。这样更为安全。

二、私服安装

2.1 系统版本

win :https://download.sonatype.com/nexus/3/latest-win64.zip
linux:https://download.sonatype.com/nexus/3/latest-unix.tar.gz

2.2 服务安装

nexus.exe /install <optional-service-name>
nexus.exe /uninstall <optional-service-name>

nexus.exe /start <optional-service-name>
nexus.exe /stop <optional-service-name>

2.3 代理配置

Proxy:http://maven.aliyun.com/nexus/content/groups/public

三、私服配置

私服是一种特殊的远程仓库,为了节省带宽和时间,可以在局域网内架设一个私有的仓库服务器,用其代理所有外部的远程仓库,而内部项目也可以部署到私服上供其他项目使用。

创建私服有许多好处

  • 节省外网带宽
  • 加速maven构建
  • 部署第三方构件
  • 提高稳定性,增强控制
  • 降低中央仓库的负荷

公司的内部项目不能公开发布,但需要被其他项目依赖,可以部署到私服上。 在pom.xml中配置发布地址。

<distributionManagement>
	<repository>
		<id>thirdparty</id>
		<name>project thirdparty repository</name>
		<url>
			http://mvn.company.com/nexus/content/repositories/thirdparty
		</url>
	</repository>
</distributionManagement>

当执行mvn发布命令时,maven会将项目构建输出到配置对应的远程仓库

mvn clean deploy

四、镜像

如果仓库X可以提供仓库Y存储的所有内容,那么就可以认为X是Y的一个镜像。由于地理位置的因素,镜像往往能够提供比中央库存更快的服务。因此可以在settings.xml中配置镜像代替中央仓库。

<!-- 配置阿里的镜像 -->
<mirrors>
    <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>*</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror> 
</mirrors>

配置*,代表对所有仓库的请求都转发至此镜像,如果配置central,则表示对中央仓库的请求都会转至该镜像。支持多种配置:

  • *:匹配所有远程仓库
  • external:*:匹配所有不在本机上的远程仓库
  • repo1,repo2:匹配仓库repo1和repo2,使用逗号分隔多个远程仓库
  • *,!repo1:匹配所有远程仓库,repo1除外

也可以将私服配置为镜像。由于私服可以代理任何外部的公共仓库,因此对于内部的maven用户来说,使用一个私服地址等于使用了所有需要的外部仓库。

<mirror>
  <id>internal repository</id>  
  <name>Internal Repository</name>
  <url>http://192.168.1.100/nexus/content/groups/public</url>
  <mirrorOf>*</mirrorOf>
</mirror>

五、仓库搜索

在使用maven进行日常开发的时候,一个常见的问题就是如何寻找需要的依赖的坐标地址。我常用的就是http://mvnrepository.com/,根据关键字进行查询,绝大部分的maven项目都能查询到。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值