为何需要 Maven 私服?
我们从项目实际开发来看:
一些无法从外部仓库下载的构件,例如内部的项目还能部署到私服上,以便供其他依赖项目使用。
为了节省带宽和时间,在局域网内架设一个私有的仓库服务器,用其代理所有外部的远程仓库。当本地Maven项目需要下载构件时,先去私服请求,如果私服没有,则再去远程仓库请求,从远程仓库下载构件后,把构件缓存在私服上。这样,及时暂时没有Internet链接,由于私服已经缓存了大量构件,整个项目还是可以正常使用的。同时,也降低了中央仓库的符合。
Nexus
安装
Nexus 是用来搭建 Maven 私服的可以说是唯一的工具,它的官网上是这样说的:“世界上第一个也是唯一的免费使用的仓库解决方案”。目前的最新版本是 OSS 3.x。提供了针对 Windows、Unix、OS X 三种系统的版本。
这里只简单说明 Docker 下的安装方式
拉取 Nexus3 镜像
1
| docker pull sonatype/nexus3
|
启动镜像并将容器内部/nexus-data
挂载到主机/root/nexus-data
目录
1 2 3 4 5 6
| docker run \ -p 8081:8081 \ --name nexus \ -v /root/nexus-data:/nexus-data \ --restart=always \ -d sonatype/nexus3
|
通过 docker ps -a
查看容器启动情况
访问 ip:8081

至此,私服搭建成功。
登录
默认登陆账号 admin admin123
注意,这个时候你可能会遇到如下这个错误:
1
| Incorrect username or password, or no permission to use the application.
|

maven 私服登录的时候默认是不能使用明文密码,要使用密文密码,一般在你创建容器的目录
查看密文
宿住机中执行
1 2
| cat /root/nexus-data/admin.password d62fa667-a22b-41db-a14a-6aa6f793f4fb
|
- 重新登陆后,会提示你重设密码

创建 Maven 仓库

选择maven2(hosted)
proxy:这是代理方式,它是用来代理中央仓库的,例如我们依赖的包在本地仓库没有,就会到私服获取,私服没有的话,会到中央仓库先把包下载到这里,然后再下载到本地仓库;
hosted:指的是我们公司或团队内部的包,并且 hosted 类型的仓库会分为 releases 和 snapshots 两个,前者是正式版,后者一般是开发测试版;
group:它的目的就是把多个仓库组合起来,然后我们项目中只需要配置上这个类型的仓库地址,就可以把它里面组合的几个仓库都关联上。

填写仓库名称(其他配置按需配置)

创建用户

创建好账户后就可以在右上角切换账户了。
配置 Maven
接着就是配置本地 maven > conf 了,找到自己本机的 maven conf 下的 setting.xml 文件,添加如下信息:
注意是 services 节点下:
1 2 3 4 5 6 7
| <servers> <server> <id>likfees</id> <username>likfees</username> <password>123456</password> </server> <servers>
|
发布 Maven 插件到仓库
创建一个普通的 maven 项目,配置 pom.xml 如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>cn.likfees</groupId> <artifactId>likfees-test</artifactId> <version>1.0-RELEASE</version>
<properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties>
<distributionManagement> <repository> <id>likfees</id> <url>http://fungs.cn081/repository/maven-repo/</url> </repository> </distributionManagement>
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>2.7</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.2.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
|
发布插件

查看发布的 Maven

项目引入私服中依赖
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <dependencies> <dependency> <groupId>cn.likfees</groupId> <artifactId>likfees-test</artifactId> <version>1.0-RELEASE</version> </dependency> </dependencies>
<repositories> <repository> <id>likfees</id> <url>http://fungs.cn:8081/repository/maven-repo/</url> </repository> </repositories>
|
