目录
私服简介
私服是一种特殊的远程仓库,它是架设在局域网内的仓库服务,私服代理广域网上的远程仓库,供局域网内的用户使用。当Maven需要下载构件的时候,它从私服请求,如果 私服上不存在该构件,则从外部远程仓库下载,缓存在私服上之后,再为Maven的下载请求提供服务。
私服的好处
a、节省自己的外网带宽
b、加速Maven构建
c、部署自己内部的第三方构件
d、提高稳定性,增强控制
e、降低中央仓库的负荷。
使用docker搭建私服
一 下载镜像
docker pull sonatype/nexus3
二 创建数据目录
mkdir -vp /opt/nexus/nexus-data && chown -R 200 /opt/nexus/nexus-data
三 启动容器
docker run -d -p 8081:8081 --name nexus --restart=always -v /opt/nexus/nexus-data:/nexus-data sonatype/nexus3
访问 http://ip:8081,可以看到如下页面,用户名admin,初始密码再容器 /nexus-data/admin.password 里面
可以使用docker exec -it nexus /bin/bash 进入容器,获取登录密码。
登录进去仓库管理页。我们可以看到maven2有三种仓库类型。
1 proxy。proxy代理中央仓库,它可以帮我们从远程仓库下载包到本地仓库(可额外添加阿里云maven私服作为代理)。
2 hosted。本地仓库,用来存储包。有测试版和正式版
3 group。自定义仓库组合,可以组合本地仓库,代理仓库。在配置文件中的镜像来源可以配置此仓库,这样就相对于配置了本地仓库和代理仓库了。
因为我们上传的包都是在hosted仓库,所以我们需要在hosted这两个仓库配置可以重新部署包的方式,这样重新打包就不会报400错误
添加阿里云私服代理
如果直接从maven中央仓库拉取,由于网络原因,会显得非常慢,而且一些jar包中央仓库也可能不全,所以我们可以添加国内的maven私服作为代理。这里,我们使用阿里云maven私服添加额外的代理下载。
点击 Create repository 添加仓库
Remote storage 填写阿里云的私服仓库地址,其它配置项跟中央仓库配置一样。
配置完成后,我们还要把此仓库拉入仓库组中(group),这样就可以同时使用中央仓库和阿里云私服仓库了。
配置settings.xml
将url改成自己私服的地址,密码设置你改好的密码
<?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">
<activeProfiles>
<activeProfile>xxxxProfile</activeProfile>
<activeProfile>jdk-1.8</activeProfile>
</activeProfiles>
<localRepository>D:\maven_repository</localRepository>
<servers>
<!--maven私服账号,用于上传jar包到私服认证使用
注意,项目的pom.xml文件的
<distributionManagement>
<repository>
<id>releases</id>
<name>releases Repository</name>
<url>http://nexus.xxxx.info/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://nexus.xxxx.info/repository/maven-snapshots/</url>
<uniqueVersion>true</uniqueVersion>
</snapshotRepository>
</distributionManagement>
id必须于此保存一致,否则无法通过id找到对应的认证账号信息
-->
<server>
<id>releases</id>
<username>admin</username>
<password>xxxx</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>xxxx</password>
</server>
</servers>
<mirrors>
<mirror>
<id>releases</id>
<mirrorOf>*</mirrorOf>
<name>Nexus private</name>
<!--配置仓库为group类型的私服仓库地址-->
<url>http://nexus.xxxx.info/repository/maven-public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>zhaodaoProfile</id>
<properties>
<encoding>UTF-8</encoding>
<project.build.sourceEncoding>${encoding}</project.build.sourceEncoding>
<project.reporting.outputEncoding>${encoding}</project.reporting.outputEncoding>
</properties>
<repositories>
<repository>
<id>nexus</id>
<name>private nexus</name>
<!--配置仓库为group类型的私服仓库地址-->
<url>http://nexus.xxxx.info/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>local private nexus</name>
<!--配置仓库为group类型的私服仓库地址-->
<url>http://nexus.xxxx.info/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
</settings>
配置pom.xml文件
在项目的pom.xml文件中,配置如下
<distributionManagement>
<repository>
<id>releases</id>
<name>releases Repository</name>
<url>http://nexus.xxxx.info/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://nexus.xxxx.info/repository/maven-snapshots/</url>
<name>snapshots Repository</name>
</snapshotRepository>
</distributionManagement>
我们可以通过idea测试一下,是否可以打包成功。
测试
通过idea自带的插件,依次点击 clean、deploy。就可把本地的包上传到我们的私服上去
可以看到,我们的包已经上传到私服了。