|
| 1 | +--- |
| 2 | +title: 部署并使用 Nexus 作为 Maven 私服 |
| 3 | +date: 2018/09/29 |
| 4 | +categories: |
| 5 | +- javatool |
| 6 | +tags: |
| 7 | +- java |
| 8 | +- javatool |
| 9 | +- build |
| 10 | +- maven |
| 11 | +--- |
| 12 | + |
| 13 | +# 部署并使用 Nexus 作为 Maven 私服 |
| 14 | + |
| 15 | +> 关键词:maven, nexus |
| 16 | +> |
| 17 | +> 部署环境 |
| 18 | +> |
| 19 | +> - Nexus 3.13.0 |
| 20 | +> - JDK 1.8 |
| 21 | +> - Maven 3.5.4 |
| 22 | +> |
| 23 | +
|
| 24 | +## 下载安装 Nexus |
| 25 | + |
| 26 | +进入[官方下载地址](https://www.sonatype.com/download-oss-sonatype),选择合适版本下载。 |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +本人希望将 Nexus 部署在 Linux 机器,所以选用的是 Unix 版本。 |
| 31 | + |
| 32 | +这里,如果想通过命令方式直接下载(比如用脚本安装),可以在[官方历史发布版本页面](https://help.sonatype.com/repomanager3/download/download-archives---repository-manager-3)中找到合适版本,然后执行以下命令: |
| 33 | + |
| 34 | +```sh |
| 35 | +wget -O /opt/maven/nexus-unix.tar.gz http://download.sonatype.com/nexus/3/nexus-3.13.0-01-unix.tar.gz |
| 36 | +tar -zxf nexus-unix.tar.gz |
| 37 | +``` |
| 38 | + |
| 39 | +解压后,有两个目录: |
| 40 | + |
| 41 | +- nexus-3.13.0-01 - 包含了 Nexus 运行所需要的文件。是 Nexus 运行必须的。 |
| 42 | +- sonatype-work - 包含了 Nexus 生成的配置文件、日志文件、仓库文件等。当我们需要备份 Nexus 的时候默认备份此目录即可。 |
| 43 | + |
| 44 | +## 启动停止 Nexus |
| 45 | + |
| 46 | +进入 nexus-3.13.0-01/bin 目录,有一个可执行脚本 nexus。 |
| 47 | + |
| 48 | +执行 `./nexus`,可以查看允许执行的参数,如下所示,含义可谓一目了然: |
| 49 | + |
| 50 | +```sh |
| 51 | +$ ./nexus |
| 52 | +Usage: ./nexus {start|stop|run|run-redirect|status|restart|force-reload} |
| 53 | +``` |
| 54 | + |
| 55 | +- 启动 nexus - `./nexus start` |
| 56 | +- 停止 nexus - |
| 57 | + |
| 58 | +启动成功后,在浏览器中访问 `http://<ip>:8081`,欢迎页面如下图所示: |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +点击右上角 Sign in 登录,默认用户名/密码为:admin/admin123。 |
| 63 | + |
| 64 | +有必要提一下的是,在 Nexus 的 Repositories 管理页面,展示了可用的 maven 仓库,如下图所示: |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +> 说明: |
| 69 | +> |
| 70 | +> - maven-central - maven 中央库(如果没有配置 mirror,默认就从这里下载 jar 包),从 https://repo1.maven.org/maven2/ 获取资源 |
| 71 | +> - maven-releases - 存储私有仓库的发行版 jar 包 |
| 72 | +> - maven-snapshots - 存储私有仓库的快照版(调试版本) jar 包 |
| 73 | +> - maven-public - 私有仓库的公共空间,把上面三个仓库组合在一起对外提供服务,在本地 maven 基础配置 settings.xml 中使用。 |
| 74 | +> |
| 75 | +
|
| 76 | +## 使用 Nexus |
| 77 | + |
| 78 | +如果要使用 Nexus,还必须在 settings.xml 和 pom.xml 中配置认证信息。 |
| 79 | + |
| 80 | +### 配置 settings.xml |
| 81 | + |
| 82 | +一份完整的 `settings.xml`: |
| 83 | + |
| 84 | +```xml |
| 85 | +<?xml version="1.0" encoding="UTF-8"?> |
| 86 | + |
| 87 | +<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" |
| 88 | + 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"> |
| 89 | + <pluginGroups> |
| 90 | + <pluginGroup>org.sonatype.plugins</pluginGroup> |
| 91 | + </pluginGroups> |
| 92 | + |
| 93 | + <!-- Maven 私服账号信息 --> |
| 94 | + <servers> |
| 95 | + <server> |
| 96 | + <id>releases</id> |
| 97 | + <username>admin</username> |
| 98 | + <password>admin123</password> |
| 99 | + </server> |
| 100 | + <server> |
| 101 | + <id>snapshots</id> |
| 102 | + <username>admin</username> |
| 103 | + <password>admin123</password> |
| 104 | + </server> |
| 105 | + </servers> |
| 106 | + |
| 107 | + <!-- jar 包下载地址 --> |
| 108 | + <mirrors> |
| 109 | + <mirror> |
| 110 | + <id>public</id> |
| 111 | + <mirrorOf>*</mirrorOf> |
| 112 | + <url>http://10.255.255.224:8081/repository/maven-public/</url> |
| 113 | + </mirror> |
| 114 | + <!-- 国内建议使用 aliyun 作为中央仓库以加速下载 --> |
| 115 | + <mirror> |
| 116 | + <id>aliyun</id> |
| 117 | + <mirrorOf>central</mirrorOf> |
| 118 | + <name>Aliyun Mirror</name> |
| 119 | + <url>http://maven.aliyun.com/nexus/content/groups/public/ </url> |
| 120 | + </mirror> |
| 121 | + </mirrors> |
| 122 | + |
| 123 | + <profiles> |
| 124 | + <profile> |
| 125 | + <id>zp</id> |
| 126 | + <repositories> |
| 127 | + <repository> |
| 128 | + <id>central</id> |
| 129 | + <url>http://central</url> |
| 130 | + <releases> |
| 131 | + <enabled>true</enabled> |
| 132 | + </releases> |
| 133 | + <snapshots> |
| 134 | + <enabled>true</enabled> |
| 135 | + </snapshots> |
| 136 | + </repository> |
| 137 | + </repositories> |
| 138 | + <pluginRepositories> |
| 139 | + <pluginRepository> |
| 140 | + <id>central</id> |
| 141 | + <url>http://central</url> |
| 142 | + <releases> |
| 143 | + <enabled>true</enabled> |
| 144 | + </releases> |
| 145 | + <snapshots> |
| 146 | + <enabled>true</enabled> |
| 147 | + <updatePolicy>always</updatePolicy> |
| 148 | + </snapshots> |
| 149 | + </pluginRepository> |
| 150 | + </pluginRepositories> |
| 151 | + </profile> |
| 152 | + </profiles> |
| 153 | + |
| 154 | + <activeProfiles> |
| 155 | + <activeProfile>zp</activeProfile> |
| 156 | + </activeProfiles> |
| 157 | +</settings> |
| 158 | +``` |
| 159 | + |
| 160 | +### 配置 pom.xml |
| 161 | + |
| 162 | +在 pom.xml 中添加如下配置: |
| 163 | + |
| 164 | +```xml |
| 165 | + <distributionManagement> |
| 166 | + <repository> |
| 167 | + <id>releases</id> |
| 168 | + <name>Releases</name> |
| 169 | + <url>http://10.255.255.224:8081/repository/maven-releases</url> |
| 170 | + </repository> |
| 171 | + <snapshotRepository> |
| 172 | + <id>snapshots</id> |
| 173 | + <name>Snapshot</name> |
| 174 | + <url>http://10.255.255.224:8081/repository/maven-snapshots</url> |
| 175 | + </snapshotRepository> |
| 176 | + </distributionManagement> |
| 177 | +``` |
| 178 | + |
| 179 | +> 注意: |
| 180 | +> |
| 181 | +> - `<repository>` 和 `<snapshotRepository>` 的 id 必须和 `settings.xml` 配置文件中的 `<server>` 标签中的 id 匹配。 |
| 182 | +> - `<url>` 标签的地址需要和 maven 私服的地址匹配。 |
| 183 | +> |
| 184 | +
|
| 185 | +### 执行 maven 构建 |
| 186 | + |
| 187 | +如果要使用 settings.xml 中的私服配置,必须通过指定 `-P zp` 来激活 profile。 |
| 188 | + |
| 189 | +示例: |
| 190 | + |
| 191 | +```sh |
| 192 | +# 编译并打包 maven 项目 |
| 193 | +$ mvn clean package -Dmaven.skip.test=true -P zp |
| 194 | + |
| 195 | +# 编译并上传 maven 交付件(jar 包) |
| 196 | +$ mvn clean deploy -Dmaven.skip.test=true -P zp |
| 197 | +``` |
| 198 | + |
| 199 | +## 参考资料 |
| 200 | + |
| 201 | +- https://www.cnblogs.com/hoobey/p/6102382.html |
| 202 | +- https://blog.csdn.net/wzygis/article/details/49276779 |
| 203 | +- https://blog.csdn.net/clj198606061111/article/details/52200928 |
0 commit comments