maven关于配置仓库的标签详解

说到这个问题首先了解下,maven默认的中央仓库,

在maven3.xxx版本之后,在maven安装目录下的:/lib/maven-model-builder-${version}.jar中

,打开apache-maven-3.6.3\lib\maven-model-builder-3.6.3.jar\org\apache\maven\model\pom-4.0.0.xml,可以在该配置文件找那个默认的中央仓库配置,如下:

 <repositories>
    <repository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>

可以看到,默认的中央仓库id为:central,并且关闭了snapshot版本构建的下载。

POM文件中配置Repository 和Setting.xml配置Repository区别

首先在POM文件中,我们可以配置工程所用到的仓库。如下:配置了id为:central的仓库。

<repositories>
        <repository>
            <id>central</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <layout>default</layout>
            <!-- 是否开启发布版构件下载 -->
            <releases>
                <enabled>true</enabled>
            </releases>
            <!-- 是否开启快照版构件下载 -->
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

也即会覆盖掉maven默认的中央仓库配置。

那我们这POM文件配置repository,和再setting通过profile 配置repository有什么区别?

其实实际效果是没有任何区别的,但是在一个庞大的工程里,往往都是拆分为很多子工程,为了方便不用再每个POM文件配置仓库,只需要再Setting.xml 配置文件配置全局仓库即可。

那么再setting.xml如何配置仓库呢?

<profile>
	<id>dev</id>
    <repositories>
    		<repository>
					<id>central</id>
					<name>alibaba</name>
					<releases>
						<enabled>true</enabled>
                         <!--always代表经常性的去拉取最新的jar --> 
						<updatePolicy>always</updatePolicy>
						<checksumPolicy>warn</checksumPolicy>
					</releases>
					<snapshots>
						<enabled />
						<updatePolicy />
						<checksumPolicy />
					</snapshots>
					<url>https://CompanyIP/repository/public</url>
					<layout>default</layout>
				</repository>
    </repositories>
</profile>

注意到在profile中我们配置了id为:dev。所以激活这个加载即可

	<activeProfiles>
		<activeProfile>dev</activeProfile>
	</activeProfiles>

同样地,我们可以在setting.xml配置多个仓库,别忘了加载该仓库哦。

另外:在使用maven的时候,我们一般不仅需要配置repositories,在特殊情况下,也需要配置pluginRepository。因为只要当下载普通依赖的时候才去repositories找仓库地址,而maven命令需要的插件(比如clean、install都是maven的插件),走的还是默认的repository。

<pluginRepository>
        <id>central</id>
        <url>http://xxx/repository/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
        </snapshots>
</pluginRepository>

mirror和Repository的区别

在setting.xml 配置mirror

mirror相当于Repository的一个代理拦截层,什么意思呢?

就是说:若maven访问到我们配置好的central仓库,那么先看看有没有该仓库Id:central 有没有被代理。若被代理了,则使用mirror提供的URL下载jar包。

<mirrors>
    <mirror>
        <id>aliyunmaven</id>
        <!-- 代理id为:central的Repository -->
        <mirrorOf>central</mirrorOf>
        <name>aliyun maven</name>
        <url>https://maven.aliyun.com/repository/public</url>
    </mirror>
</mirrors>

配置完后,注意到mirrorOf相当于代理了我们前面配置的Repository,也就是说,当maven下载以来的时候,首先会去Repository提供的仓库地址去下载,若检测到id为:central的仓库被代理,那么就去mirror里面提供的地址去下载。

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值