刚接触maven就是在公司里配置好的,所以一直以来使用都没毛病,所以一直没有去动这些固有的东西。

  但是,后来把公司的电脑拿回家之后,发现有的东西就搞不起来了。原因也看一下就明白了,因为在公司的时候用的是公司的maven私服,所以回家后,用不了也是正常。

  但是,真的脱离了公司,自己就不能工作了吗?不可能吧。 难道一下开源工具都必须要依赖于公司的网络? 这明显是不合理的。

  那么,就扯出本次文章的意义了,在家里,自然是要公有的maven仓库了,那么,怎样配置maven仓库才能让自己用起来顺心呢?

1. 改掉原有的maven仓库地址,让maven从公网上摘取jar包下载,方便、快捷。

  原私有配置示例如下:

<?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
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository -->
<localRepository>${user.home}/.m2/repository</localRepository>
<!--pluginGroups></pluginGroups-->
<!--proxies></proxies-->
<servers>
<server>
<id>releases</id>
<username>admin</username>
<password>123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>123</password>
</server>
</servers>
<pluginRepositories>
<pluginRepository>
<id>mypublic</id>
<name>Public</name>
<url>http://test.nexus.com/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
<mirrors>
<mirror>
<id>central</id>
<name>internal</name>
<url>http://test.nexus.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<!--Enable snapshots for the built in central repo to direct -->
<!--all requests to nexus via the mirror -->
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</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>
<activeProfiles>
<!--make the profile active all the time -->
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>

  如果想直接把私有的地方干掉,那么,这是最快的,直接把mirror的url改掉就行了,如:

 <mirrors>
<mirror>
<id>central</id>
<name>internal</name>
     <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
     <!-- <url>http://test.nexus.com/nexus/content/groups/public/</url> -->
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>

  当然了,到需要的地方,再把这个地址改回来就可以了,这可能是改动最小的方法了。但是也很恼火的一种配置方式,因为你要不时地记得切换(谁有那闲心)!!!

2. 添加一个类似结构的仓库配置,这样的话就不切来切去的了,一劳永逸。

  相当于添加了多仓库,如下:

<mirrors>
<!-- 再添加一个mirror, 注意mirrorsOf 为 * -->
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors> <repositories>
<!-- 添加一个 repository -->
<repository>
<id>alimaven</id>
<url>http://alimaven</url>
<releases><enabled>true</enabled></releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>

  这样的话,就不用再切换了。但是,这会导致一种情况,在某环境不可用时,maven下载jar将会很慢,严重影响心情,所以,其实是不建议这么干的。

3. 按照最简单的方式,新增一个仓库地址,随时切换。

  不用去添加mirror了,直接以url的形式,配置到reponsitory里即可,如下:

<repository>
<!-- 直接添加一个 repository,在 activeProfiles 里加上此id 即可 -->
<id>repo1</id>
<name>org.maven.repo1</name>
<layout>default</layout>
<url>https://repo1.maven.org/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
  <activeProfiles>
<activeProfile>nexus</activeProfile>
 <!-- 添加此属性,以便激活repo1的配置 -->
<activeProfile>repo1</activeProfile>
</activeProfiles>

  这样,既不影响原来的结构,也不影响现在使用,在家的时候,可以将私有仓库注释掉,以提高访问速度。

  注意: 最后一个 activeProfiles 的属性是必须的,否则你可能发现你改了配置,然而并没有什么卵用!

<activeProfiles>
<!-- 放心,此处的 nexus 是多个仓库的配置 -->
<activeProfile>nexus</activeProfile>
</activeProfiles>

4. 无法拉取包的困惑?你可能发现,你的maven无法拉取 SNAPSHOT 包,然而包明明就在那里!

  是的,出于安全的考虑,maven 默认是不会去使用 snapshot 包的,所以,如果你有需要使用 snapshot 包(很多公司可能大量使用),那么你就需要配置 SNAPSHOT 为允许了!

    <repository>
<id>central</id>
<url>http://central</url>
<releases>
<enabled>true</enabled>
</releases>
<!-- 如下开启 snapshots 功能 -->
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>

5. 另附一个idea maven配置的方法:

  maven作为基础辅助工具,虽不需去深入的理解,也没有高深的技巧,但是作为没有处理过几个相关问题的同学,还是很有必要了解的。

  在处理问题的过程,一步步成长了。

  毕竟,资深工程师,没有解决过很多bug,是不能成为资深的。

maven配置多仓库的方法的更多相关文章

  1. myeclipse2014 安装maven3.3.9和maven配置本地仓库 及错误修改

    结合网上的知识梳理以及自己安装的经验 myeclipse2014 安装maven3.3.9和maven配置本地仓库  及犯的错误修改  成功搞定maven 1,安装 Maven 之前要求先确定你的 J ...

  2. maven配置多仓库镜像(转)

    原文地址:maven配置多仓库镜像 问题场景: 1.国内访问maven默认远程中央镜像特别慢 2.用阿里的镜像替代远程中央镜像 3.大部分jar包都可以在阿里镜像中找到,部分jar包在阿里镜像中没有, ...

  3. maven配置本地仓库、maven配置阿里中央仓库、eclipse配置maven

    一.maven配置本地仓库路径 1.打开下载好的maven目录 (若没安装,可以看我写的安装步骤https://www.cnblogs.com/xjd-6/p/11344719.html) 2.进入c ...

  4. Maven 配置远程仓库

    最近要用到一个包:spark-1.6.0-cdh5.8.2,在https://mvnrepository.com网站上搜到该包的pom.xml语句.但是看到下面有这样一句话: 该包在图中所述repos ...

  5. Maven配置本地仓库

    当我们在myeclipse中update maven时可能会报错User setting file does not exist C:\Users\lenevo\.m2\setting.xml,以致于 ...

  6. maven配置远程仓库

    1,当中央仓库无法满足要求时,可能项目需要的构件存在另一个远程仓库中.可以在POM文件中配置该仓库.代码如下: <project> ...... <repositories> ...

  7. Windows下 maven3.3.1的安装步骤+maven配置本地仓库

    简单讲下maven的安装步骤: 1.在安装maven之前,先确保已经安装JDK1.6及以上版本,并且配置好环境变量. 2.下载maven3,最新版本是Maven3.3.1 ,下载地址:http://m ...

  8. Windows下 maven3.0.4的安装步骤+maven配置本地仓库

    简单讲下maven的安装步骤: 1.在安装maven之前,先确保已经安装JDK1.6及以上版本,并且配置好环境变量. 2.下载maven3,最新版本是Maven3.0.4 ,下载地址:http://m ...

  9. Maven - 配置镜像仓库

    默认仓库的配置(全球中央仓库): 可以打开maven的安装目录/conf/settings.xml文件,配置镜像,找到如下一段配置,这里默认没有配置任何镜像,但是有一个被注释的配置范例: id: 镜像 ...

随机推荐

  1. Spring系列之Spring常用注解总结 转载

    Spring系列之Spring常用注解总结   传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.x ...

  2. Echarts绘制横向柱状图

    效果图: 关键配置: 将xAxis的type设置为value, 将yAxis的type设置为category即可实现横向显示

  3. ImportError: No module named pycocotools.coco,pycocotools/_mask.so: undefined symbol: _Py_ZeroStruct

    准确的说是没有安装 pycocotools 可以借鉴下面链接: https://blog.csdn.net/ab0902cd/article/details/79085797 因为我通常用Python ...

  4. jq 切换功能toggle

    ---恢复内容开始--- $(document).ready(function () { $(".jianjie").click(function () { $(this).tog ...

  5. 第一章 C++语言入门

            标准数据类型         C++语言提供了丰富的数据类型,如整数类型.实数类型(浮点数).字符类型等.每种数据类型均有均值范围,Dev-C++(4.9.9.2)是Windows平台 ...

  6. CF 317 A. Lengthening Sticks(容斥+组合数学)

    传送门:点我 A. Lengthening Sticks  time limit per test        1 second You are given three sticks with po ...

  7. js+正则+单双引号问题

    在处理用js动态添加表格时,表格中有正则表达式,其单双引号问题导致的添加表格不成功,和正则失效问题 <script type="text/javascript"> va ...

  8. jsonarray根据id排序

    List<JSONObject> jsonValue=new ArrayList<JSONObject>(); JSONArray sortJsonarr=new JSONAr ...

  9. 152. Maximum Product Subarray最大乘积子数组/是否连续

    [抄题]: Given an integer array nums, find the contiguous subarray within an array (containing at least ...

  10. Head First Servlets & JSP 学习笔记 第九章 —— 使用JSTL

    JSTL1.1 不是JSP2.0规范的一部分!你能访问Servlet和JSP API 不意味着你能访问JSTL! 使用JSTL之前,需要将两个文件("jstl.jar" 和 &qu ...