Maven私有仓库搭建以及使用
参考链接https://www.cnblogs.com/wuwei928/p/10338307.html
一、使用Docker安装Nexus
Docker search nexus
docker pull docker.io/sonatype/nexus3
mkdir -p /usr/local/nexus3/nexus-data``chown -R 200 /usr/local/nexus3/nexus-data` `docker run -tid -p 8081:8081 --name nexus -e NEXUS_CONTEXT=nexus -v /usr/local/nexus3/nexus-data:/nexus-data docker.io/sonatype/nexus3
现在已经安装完成了 nexus。
二、登录Nexus进行配置
1、登录nexus
这里的ip是你的服务器ip
访问:http://ip:8081/nexus 使用默认管理员身份登录,帐号:admin,密码:admin123 注意:nexus 3.17版本后初始密码不是admin/admin123。
3.17版本密码改成随机的了,而且登录时候提示密码在/nexus-data/admin.password里
大概在这个位置
查看密码
vi admin.password
注意:登录密码就是这么一大长传的字符串!!!,这不是加密的,当初以为这是加密的,还各种找资料要解密
复制粘贴登录就行了
首次登录后会让你重置密码,重置密码后系统就会自动删除admin.password文件
2、进入管理界面
- Browse可以查看当前有多少仓库,搭建好的Nexus,默认会带有一些maven仓库,一般使用这些仓库就足够了。
- 默认仓库说明
maven-central:maven中央库,默认从https://repo1.maven.org/maven2/拉取jar
maven-releases:私库发行版jar,初次安装请将Deployment policy设置为Allow redeploy
maven-snapshots:私库快照(调试版本)jar
maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地maven基础配置settings.xml或项目pom.xml中使用
- 仓库类型
Group:这是一个仓库聚合的概念,用户仓库地址选择Group的地址,即可访问Group中配置的,用于方便开发人员自己设定的仓库。maven-public就是一个Group类型的仓库,内部设置了多个仓库,访问顺序取决于配置顺序,3.x默认Releases,Snapshots, Central,当然你也可以自己设置。
Hosted:私有仓库,内部项目的发布仓库,专门用来存储我们自己生成的jar文件
3rd party:未发布到公网的第三方jar (3.x去除了)
Snapshots:本地项目的快照仓库
Releases: 本地项目发布的正式版本
Proxy:代理类型,从远程中央仓库中寻找数据的仓库(可以点击对应的仓库的Configuration页签下Remote Storage属性的值即被代理的远程仓库的路径),如可配置阿里云maven仓库
Central:中央仓库
Apache Snapshots:Apache专用快照仓库(3.x去除了)
3、增加新的代理源
第一步 按照步骤添加新的代理源
第二步 选择添加maven2的代理
第三步 添加代理(Cache统一设置为200天 288000)
第四步 逐个增加常用代理
[](javascript:void(0)😉
1. aliyun
http://maven.aliyun.com/nexus/content/groups/public
2. apache_snapshot
https://repository.apache.org/content/repositories/snapshots/
3. apache_release
https://repository.apache.org/content/repositories/releases/
4. atlassian
https://maven.atlassian.com/content/repositories/atlassian-public/
5. central.maven.org
http://central.maven.org/maven2/
6. datanucleus
http://www.datanucleus.org/downloads/maven2
7. maven-central (安装后自带,仅需设置Cache有效期即可)
https://repo1.maven.org/maven2/
8. nexus.axiomalaska.com
http://nexus.axiomalaska.com/nexus/content/repositories/public
9. oss.sonatype.org
https://oss.sonatype.org/content/repositories/snapshots
10.pentaho
https://public.nexus.pentaho.org/content/groups/omni/
[](javascript:void(0)😉
第五步 设置maven-public 将这些代理加入Group,最好将默认的maven库放到最底下
第六步 设置私用仓库可重复发布
Nexus安装后自带maven-releases,maven-snapshots两个仓库,用于将生成的jar包发布在这两个仓库中,在实际开发中需要将maven-releases设置为可以重复发布
4、Maven配置使用Nexus
修改.m2下面的setting.xml文件配置
4.1 在servers标签下
<!-- 下面的password是我自己设置的 -->
<server>
<id>releases</id>
<username>admin</username>
<password>123456</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>123456</password>
</server>
<server>
<id>nexus-public</id>
<username>admin</username>
<password>123456</password>
</server>
4.2 在profiles标签下
<profile>
<id>HolliPark</id>
<repositories>
<!--
<repository>
<id>nexus</id>
<name>Public Repositories</name>
<url>http://10.18.1.21:8081/nexus/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
<repository>
<id>central</id>
<name>Central Repositories</name>
<url>http://10.18.1.21:8081/nexus/repository/maven-central/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
-->
<repository>
<id>releases</id>
<name>Release Repositories</name>
<url>http://10.18.1.21:8081/nexus/repository/maven-releases/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>snapshots</id>
<name>Snapshot Repositories</name>
<url>http://10.18.1.21:8081/nexus/repository/maven-snapshots/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus-public</id>
<name>Plugin Repositories</name>
<url>http://10.18.1.21:8081/nexus/repository/maven-public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
4.3 激活——在activeProfiles标签下
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Y43vwNc9-1646375727617)(C:\Users\Yohann\AppData\Roaming\Typora\typora-user-images\image-20220120112514049.png)]
<!--这里的HolliPark要与上面profile下的id一致-->
<activeProfiles>
<activeProfile>HolliPark</activeProfile>
</activeProfiles>
三、创建私有公库
1、创建普通的maven项目
2、修改项目的pom.xml
在pom文件中加入distributionManagement节点,注意:pom.xml中repository里的id需要和.m2中setting.xml里的server id名称保持一致
<!-- nexus 注意:id必须一致!-->
<distributionManagement>
<repository>
<id>releases</id>
<name>Nexus Release Repository</name>
<url>http://10.18.1.21:8081/nexus/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://10.18.1.21:8081/nexus/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
3、发布私有公库
执行部署命令即可发布。
mvn deploy
登录Nexus,查看对应的仓库已经有相关的依赖包了