Maven私服搭建
服务器配置
搭建Maven私服
关闭防火墙禁止开机自启
$ systemctl stop firewalld && systemctl disable firewalld
下载镜像
$ docker pull sonatype/nexus3
构建Docker镜像
$ mkdir /data/nexus-data
$ docker run -d -p 8081:8081 \
--name nexus \
-v /data/nexus-data:/var/nexus-data \
--restart=always \
sonatype/nexus3
访问http://192.168.80.128:8081,成功如下:
初始化账号密码 admin/admin123,如果初始化密码显示不正确,使用如下命令查看
$ docker exec -it 1b0ae9615457 /bin/bash
$ more /nexus-data/admin.password
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jZ1lL52t-1576132650317)(https://raw.githubusercontent.com/FocusProgram/PicGo/master/image/1576035748(1)].jpg)
初始化仓库,默认创建仓库类型如下
- proxy 远程仓库的代理,比如说nexus配置了一个central repository的proxy,当用户向这个proxy请求一个artifact的时候,会现在本地查找,如果找不到,则会从远程仓库下载,然后返回给用户。
- hosted 宿主仓库,用户可以把自己的一些仓库deploy到这个仓库中
- group 仓库组,是nexus特有的概念,目的是将多个仓库整合,对用户暴露统一的地址,这样就不需要配置多个仓库地址。
maven-public为group类型的仓库,里面包含了maven-releases/maven-snapshots/maven-central仓库
nuget-public为group类型的仓库,里面包含了nuget-hosted/nuget.org.proxy仓库
创建自己的项目Maven仓库
创建账户
本地仓库配置
修改本地.m2目录下的settings.xml(默认maven仓库位置)
<?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">
<servers>
<server>
<id>my-lib-repo</id>
<username>admin</username>
<password>admin</password>
</server>
</servers>
</settings>
编辑pom文件
<?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>Qianxun</groupId>
<artifactId>qywk_fastdfs_service</artifactId>
<!--注意限定版本一定为RELEASE,因为上传的对应仓库的存储类型为RELEASE-->
<version>0.0.1-RELEASE</version>
<description>FastDFS文件服务</description>
<packaging>jar</packaging>
<!--指定仓库地址-->
<distributionManagement>
<repository>
<!--此名称要和.m2/settings.xml中设置的ID一致-->
<id>my-lib-repo</id>
<url>http://www.your-repo.com/repository/my-lib-repo/</url>
</repository>
</distributionManagement>
<build>
<plugins>
<!--发布代码Jar插件-->
<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构建指令
mvn clean deploy
构建成功,查看仓库显示如下:
项目中引入Maven私服jar包,pom.xml配置如下:
<project>
...
<distributionManagement>
<repository>
<id>my-lib-repo</id>
<url>http://www.your-repo.com/repository/my-lib-repo/</url>
</repository>
</distributionManagement>
<repositories>
<repository>
<id>aliyun-repo</id>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</repository>
<repository>
<id>my-lib-repo</id>
<url>http://www.your-repo.com/repository/my-lib-repo/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</pluginRepository>
<pluginRepository>
<id>my-lib-repo</id>
<url>http://www.your-repo.com/repository/my-lib-repo/</url>
</pluginRepository>
</pluginRepositories>
...
</project>