Maven如何修改本地仓库与中央仓库

1 篇文章 0 订阅

一.本地仓库与远程仓库运行原理                           

运行Maven的时候,Maven所需要的任何构件都是直接从本地仓库获取的。如果本地仓库没有,它会首先尝试从远程仓库下载构件至本地仓库,然后再使用本地仓库的构件。
比如说,你的项目配置了junit-3.8的依赖,在你运行 mvn test  的 时候,Maven需要使用junit-3.8的jar文件,它首先根据坐标查找本地仓库,如果找到,就直接使用。如果没有,Maven会检查可用的远程仓 库配置,然后逐个尝试这些远程仓库去下载junit-3.8的jar文件,如果远程仓库存在该文件,Maven会将其下载到本地仓库中,继而使用。如果尝 试过所有远程仓库之后,Maven还是没能够下载到该文件,它就会报错。
二.修改本地仓库  

Maven缺省的本地仓库地址为${user.home}/.m2/repository 。也就是说,一个用户会对应的拥有一个本地仓库。

你也可以自定义本地仓库的位置,修改${user.home}/.m2/settings.xml :

<?xml version="1.0" encoding="UTF-8"?>

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
 
 <localRepository>E:\.m2\repository</localRepository> <!--这个位置可以定义自己的本地仓库路径 -->
  
</settings>


你还可以在运行时指定本地仓库位置:mvn clean install -Dmaven.repo.local=/home/juven/myrepo/

注意:

    

1)尽量不要放到C盘
2)maven install 和 maven package 区别

	package:把jar打到本项目的target下
	install:把target下的jar安装到本地仓库,供其他项目使用.

三.在POM.xml文件中配置中央仓库地址

<repositories><!-- maven pom 构建远程仓库地址 覆盖maven默认的center地址-->
	<repository>
		<!-- Maven 自带的中央仓库使用的Id为central-->
		<id>mvnrepository</id><!--仓库Id -->
		<name>mvnrepository</name>
		<url>http://www.mvnrepository.com/</url><!--仓库地址-->
		<layout>default</layout>
		<releases><!-- 正式版本  -->
			<enabled>true</enabled><!-- 支持稳定版本的下载 -->
		</releases>
		<snapshots><!-- 开发版本 -->
			<enabled>false</enabled><!-- 不支持开发版本的下载 -->
		</snapshots>
	</repository>
</repositories>
说明:中央仓库的id为central,远程url地址为http://repo1.maven.org/maven2,它关闭了snapshot版本构件下载的支持,一般我们配置中央仓库,这样做的原因有很多,比如你有一个局域网的远程仓 库,使用该仓库能大大提高下载速度,继而提高构建速度,也有可能你依赖的一个jar在central中找不到,它只存在于某个特定的公共仓库,这样你也不 得不添加那个远程仓库的配置。


这里我配置一个远程仓库指向中央仓库的中国镜像:

< project >  
...  
   < repositories >  
     < repository >  
       < id >maven-net-cn</ id >  
       < name >Maven China Mirror</ name >  
       < url >http://maven.net.cn/content/groups/public/</ url >  
       < releases >  
         < enabled >true</ enabled >  
       </ releases >  
       < snapshots >  
         < enabled >false</ enabled >  
       </ snapshots >  
     </ repository >  
   </ repositories >  
   < pluginRepositories >  
     < pluginRepository >  
       < id >maven-net-cn</ id >  
       < name >Maven China Mirror</ name >  
       < url >http://maven.net.cn/content/groups/public/</ url >  
       < releases >  
         < enabled >true</ enabled >  
       </ releases >  
       < snapshots >  
         < enabled >false</ enabled >  
       </ snapshots >      
     </ pluginRepository >  
   </ pluginRepositories >  
...  
</ project >
repositories和pluginRepositories含义区别:

根据构件的用途,构件可以分为两类,一类是被其他构件依赖的构件(dependencies),这也是Maven库中主要的构件。另一类是插件(plugins),这是一种特殊的构件。对于依赖的构件,其所在的库通过<repositories>设置。但是对于插件的构件,由于插件的特殊性,所以插件库独立于依赖库,使用<pluginRepositories>单独设置。但是由于依赖和插件都属于构件,所以<repositories>和<pluginRepositories>中的设置基本一致。



四.在setting.xml文件中配置中央仓库地址

我们知道了如何在POM中配置远程仓库,但考虑这样的情况:在一个公司内部,同时进行多个项目,而且以后随着这几个项目的结束,越来越多的项目会开始; 同时,公司内部建立一个Maven仓库。我们统一为所有这些项目配置该仓库,于是不得不为每个项目提供同样的配置。问题出现了,这是重复 !

其实我们可以做到只配置一次,在哪里配置呢?就是settings.xml。
不过事情没有那么简单,不是简单的将POM中的<repositories>及<pluginRepositories>元素复制到settings.xml中就可以,setting.xml 不直接支持  这两个元素。但我们还是有一个并不复杂的解决方案,就是利用profile,如下:
< settings >  
   ...  
   < profiles >  
     < profile >  
       < id >dev</ id >  
       <!-- repositories and pluginRepositories here-->  
     </ profile >  
   </ profiles >  
   < activeProfiles >  
     < activeProfile >dev</ activeProfile >  
   </ activeProfiles >  
   ...  
</ settings >


五.在setting.xml文件中配置镜像地址

如果你的地理位置附近有一个速度更快的central镜像,或者你想覆盖central仓库配置,或者你想为所有POM使用唯一的一个远程仓库(这个远程仓库代理的所有必要的其它仓库),你可以使用settings.xml中的mirror配置。

以下的mirror配置用maven.net.cn覆盖了Maven自带的central:

< settings >  
...  
   < mirrors >  
     < mirror >  
       < id >maven-net-cn</ id >  
       < name >Maven China Mirror</ name >  
       < url >http://maven.net.cn/content/groups/public/</ url >  
       < mirrorOf >central</ mirrorOf >  
     </ mirror >  
   </ mirrors >  
...  
</ settings >

以下是Maven阿里云中央仓库地址

< settings > 
	... <mirrors><mirror><id>alimaven</id><name>aliyun maven</name><url>http://maven.aliyun.com/nexus/content/groups/public/</url><mirrorOf>central</mirrorOf> </mirror></mirrors> ... </ settings >


国内OSChina提供的镜像 

<mirrors>
	<mirror>
		<id>nexus-osc</id>
		<mirrorOf>central</mirrorOf>
		<name>Nexus osc</name>
		<url>http://maven.oschina.net/content/groups/public/</url>
	</mirror>
	<mirror><!-- 有些项目还会依赖第三方仓库 -->
		<id>nexus-osc-thirdparty</id>
		<mirrorOf>thirdparty</mirrorOf>
		<name>Nexus osc thirdparty</name>
		<url>http://maven.oschina.net/content/repositories/thirdparty/</url>
	</mirror>
</mirrors>






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值