详解maven配置多仓库的方法示例

网友投稿 899 2023-04-25

详解maven配置多仓库的方法示例

详解maven配置多仓库的方法示例

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

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

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

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

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

原私有配置示例如下:

xmlns:xsi="http://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">

${user.home}/.m2/repository

releases

admin

123

snapshots

admin

123

mypublic

Public

http://test.nexus.com/nexus/content/groups/public/

central

internal

http://test.nexus.com/nexus/content/groups/public/

central

nexus

central

http://central

true

true

always

central

http://central

true

true

nexus

xmlns:xsi="http://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">

${user.home}/.m2/repository

releases

admin

123

snapshots

admin

123

mypublic

Public

http://test.nexus.com/nexus/content/groups/public/

central

internal

http://test.nexus.com/nexus/content/groups/public/

central

nexus

central

http://central

true

true

always

central

http://central

true

true

nexus

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

central

internal

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

    

central

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

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

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

alimaven

aliyun maven

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

*

alimaven

http://alimaven

true

true

always

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

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

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

repo1

org.maven.repo1

default

https://repo1.maven.org/

false

nexus

 

repo1

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

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

nexus

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

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

central

http://central

true

true

always

5.一个完整的配置样例参考

前面说的基本是从解决问题的思路去讲解,可能还是不够形象,或者每个人的理解总是有些差异,下面来一个完整的配置参考。你可以在这上面继续任意发挥:

xmlns:xsi="http://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">

${user.home}/.m2/repository

sec@public-snapshots

snapshot_user

snapshot123

mirror-sec

snapshot_user

snapshot123

sec

repo1

org.maven.repo1

default

https://repo1.maven.org/

false

sec@public-snapshots

sec-snapshots

http://maven.sec-ins.com-/repository/maven-public

true

always

warn

true

my-profile

alirepo1

org.maven.repo1

default

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

false

spring-milestones

Spring Milestones

https://repo.spring.io/milestone

false

repository.springframework.maven.release

Spring Framework Maven Release Repository

http://maven.springframework.org/milestone/

org.springframework

http://maven.springframework.org/snapshot

spring-milestone

Spring Maven MILESTONE Repository

http://repo.spring.io/libs-milestone

spring-release

Spring Maven RELEASE Repository

http://repo.spring.io/libs-release

sec

my-profile

xmlns:xsi="http://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">

${user.home}/.m2/repository

sec@public-snapshots

snapshot_user

snapshot123

mirror-sec

snapshot_user

snapshot123

sec

repo1

org.maven.repo1

default

https://repo1.maven.org/

false

sec@public-snapshots

sec-snapshots

http://maven.sec-ins.com-/repository/maven-public

true

always

warn

true

my-profile

alirepo1

org.maven.repo1

default

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

false

spring-milestones

Spring Milestones

https://repo.spring.io/milestone

false

repository.springframework.maven.release

Spring Framework Maven Release Repository

http://maven.springframework.org/milestone/

org.springframework

http://maven.springframework.org/snapshot

spring-milestone

Spring Maven MILESTONE Repository

http://repo.spring.io/libs-milestone

spring-release

Spring Maven RELEASE Repository

http://repo.spring.io/libs-release

sec

my-profile

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

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:电脑版钉钉怎么打开小程序(钉钉电脑端在哪打开)
下一篇:MybatisPlus,无XML分分钟实现CRUD操作
相关文章

 发表评论

暂时没有评论,来抢沙发吧~