三.maven仓库管理

上一节,我们讲解了maven的依赖管理,各模块系统从仓库下载依赖包。在实际工作中,我们有大量开发人员协同开发,如果我们都同时用maven中央仓库或者阿里云镜像仓库,需要每一个人都去下载一遍,白白浪费时间和网络资源。此时我们需要一个企业级私服,局域网内使用,各开发组负责的模块也上传到私服,供其它开发组下载使用。

1在搭建私服之前,我们需要搞清楚几个概念;

1.maven中央仓库,是一个远程仓库,可供任何人使用
2.阿里云镜像仓库,代理了maven中央仓库,大陆使用更快速
3.其它远程仓库,例如JBOSS,GOOGlE等
4.本地仓库,开发者自己电脑上存储。~.m2\repository

2.私有仓库搭建用Nexus比较普遍,我们也将采用

1.GOOGLE搜索Nexus Repository Oss 下载
2.解压。解压后我们发现有两个文件,一个nexus-3.30XXX和一个sonatype-work
nexus-3.30XXX存放运行时需要的文件
bin目录下nexus.vmoptions和etc下nexus-default.properties(可配置端口号)
sonatype-work包含日志文件,配置文件和仓库文件
3.进入nexus-3.30XXX/bin目录下 运行nexus.exe/run 等待几分钟
打开http://localhost:8081/ 8081是默认端口号
新版本可以直接进入 然后设置密码
4.我们将看到有maven-public maven-central等各种仓库类型

11.png

maven-public:是个仓库组group nexus通过组的概念来管理多个仓库
maven-central:proxy代理仓库,我们可以代理maven中央仓库和阿里云镜像仓库
maven-releases:hosted宿主仓库 用于部署公司内部releases版本的包
maven-snapshots:hosted宿主仓库 用于部署公司内部snapshots版本的包

我们计划配置如下图一样的私服仓库效果:


nexus.png

首先配置maven-central, 双击进去把代理地址换为阿里云镜像地址保存,如下图


proxy.png

maven-releases已有 不需配置
maven-snapshots已有 不需配置

如上面nexus.png仓库流程图,我们计划配置个3rd-party来储存第三方非公开的商业包
点击“Create Repository"按钮,选择maven2(hosted)仓库 然后起名3rd-party
maven-public配置:双击进去,把上面maven-releases,maven-snapshots,maven-central和3rd-party都分配给maven-public组


maven-pulic.png

nexus已配置完毕,我们的maven项目直接使用maven-public仓库地址即可。

3.maven配置nexus私服

我们将maven项目配置为强制从nexus私服下载依赖
第一步:settings.xml在profile添加如下配置

    <profile>
        <id>nexus</id>
        <repositories>
            <repository>
                <id>nexus</id>
                <name>Nexus</name>
                <url>http://localhost:8081/repository/maven-public/</url>
                <releases><enabled>true</enabled></releases>
                <snapshots><enabled>true</enabled></snapshots>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>nexus</id>
                <name>Nexus Plugin Repository</name>
                <url>http://localhost:8081/repository/maven-public/</url>
                <releases><enabled>true</enabled></releases>
                <snapshots><enabled>true</enabled></snapshots>
            </pluginRepository>
        </pluginRepositories>
    </profile>

第二步,激活第一步的profile配置

<activeProfiles>
    <activeProfile>nexus</activeProfile>
</activeProfiles>

第三步,配置镜像机制

<mirrors>
    <mirror>
        <id>nexus</id>
        <mirrorOf>*</mirrorOf>
        <url>http://localhost:8081/nexus/content/groups/public</url>
    </mirror>
</mirros>

第四步,覆盖maven的远程仓库
打开安装包maven的lib包下maven-model-builder包下的pom.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>

  <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
  </pluginRepositories>

我们会看到maven默认加载使用的是一个id焦作central的仓库配置,我们需要覆盖它
更改第一步的profile配置,修改为central

    <profile>
        <id>nexus</id>
        <repositories>
            <repository>
                <id>central</id>
                <name>Nexus</name>
                <url>http://central/</url>
                <releases><enabled>true</enabled></releases>
                <snapshots><enabled>true</enabled></snapshots>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>central</id>
                <name>Nexus Plugin Repository</name>
                <url>http://central</url>
                <releases><enabled>true</enabled></releases>
                <snapshots><enabled>true</enabled></snapshots>
            </pluginRepository>
        </pluginRepositories>
    </profile>

第五步,配置专用开发者账号
我们在nexus后台配置一个角色nx-deployment分配给这个角色仓库读取修改添加删除等权限。


nx-deployment.png

然后创建一个deployment用户,角色分配为上面创建的nx-deployment角色
然后在settings.xml配置这个开发者账号

  <servers>
    <server>
        <id>nexus-releases</id>
        <username>deployment</username>
        <password>deployment</password>
    </server>
    <server>
        <id>nexus-snapshots</id>
        <username>deployment</username>
        <password>deployment</password>
    </server>
  </servers>

第六步,发布仓库的配置(本地项目上传到私服,供其它项目组引用这个模块使用)
在我们的项目pom.xml配置如下

    <distributionManagement>
        <repository>
            <id> nexus-releases</id>
            <name> Nexus Release Repository</name>
            <url>http://localhost:8081/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id> nexus-snapshots</id>
            <name> Nexus Snapshot Repository</name>
            <url>http://localhost:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

一个是releases版本上传到maven-releases仓库,另一个是snapshots上传maven-snapshots仓库
到此我们的maven使用nexus私服配置完毕。
如何测试?
1.我们可以测试把项目部署发布到远程仓库用mvn clean deploy 然后在nexus后台仓库里查看仓库里是否存在
2.项目添加引用包 看是否是从远程私服下载的引用包

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,367评论 6 512
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,959评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,750评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,226评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,252评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,975评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,592评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,497评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,027评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,147评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,274评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,953评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,623评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,143评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,260评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,607评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,271评论 2 358

推荐阅读更多精彩内容

  • 今天感恩节哎,感谢一直在我身边的亲朋好友。感恩相遇!感恩不离不弃。 中午开了第一次的党会,身份的转变要...
    迷月闪星情阅读 10,567评论 0 11
  • 彩排完,天已黑
    刘凯书法阅读 4,220评论 1 3
  • 表情是什么,我认为表情就是表现出来的情绪。表情可以传达很多信息。高兴了当然就笑了,难过就哭了。两者是相互影响密不可...
    Persistenc_6aea阅读 125,226评论 2 7