Docker 搭建 Maven 私服
# 安装
$ docker pull sonatype/nexus3
$ docker run -d -p 8081:8081 --name nexus -v /root/nexus-data:/var/nexus-data --restart=always sonatype/nexus3
$ docker logs -f nexus
Started Sonatype Nexus OSS 3.30.1-01
$ docker exec -it nexus /bin/bash
bash-4.4$ cat /nexus-data/admin.password
406ee8d2-ff62-4712-b627-c4cb57d56b6f
# 默认仓库
- maven-central:中央仓库,默认从 https://repo1.maven.org/maven2/ 拉取 jar
- maven-releases:私库发行版 jar(建议将 Deployment policy 设置为 Allow redeploy)
- maven-snapshots:私库调试版 jar
- maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务
# 仓库类型说明
- group:仓库组。内部设置了多个仓库,访问顺序取决于配置顺序(默认为 releases > snapshots > central)
- hosted:私有仓库。内部项目的发布仓库,存储自己生成的 jar
- proxy:代理仓库,从远程中央仓库中寻找数据
# 添加阿里云代理仓库
http://maven.aliyun.com/nexus/content/groups/public
加入 maven-public
# setting.xml
<?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">
<localRepository>C:\Develop\repository</localRepository>
<servers>
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://192.168.11.210:8081/repository/maven-public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>dev</id>
<repositories>
<repository>
<id>Nexus</id>
<url>http://192.168.11.210:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
<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>
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
</settings>
# pom.xml
<distributionManagement>
<repository>
<id>releases</id>
<name>Releases</name>
<url>http://192.168.11.210:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Snapshot</name>
<url>http://192.168.11.210:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
# 三方 jar 上传到 Nexus
mvn deploy:deploy-file \
-DgroupId=<group-id> \
-DartifactId=<artifact-id> \
-Dversion=<version> \
-Dpackaging=<type-of-packaging> \
-Dfile=<path-to-file> \
-DrepositoryId=<server-id-in-settings.xml> \
-Durl=<url-of-the-repository-to-deploy>