私服
1 概念
- 私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件。
- 有了私服之后,当 Maven 需要下载依赖时,直接请求私服,私服上存在则下载到本地仓库;否则,私服请求外部的远程仓库,将构件下载到私服,再提供给本地仓库下载。
- 私服可以解决在企业做开发时每次需要的jar包都要在中心仓库下载,且每次下载完只能被自己使用,不能被其他开发人员使用
- 所谓私服就是一个服务器,但是不是本地层面的,是公司层面的,公司中所有的开发人员都在使用同一个私服
2 Nexus安装【了解】
2.1 下载
- 官网:https://blog.sonatype.com/
- 下载地址:https://help.sonatype.com/repomanager2/download/download-archives---repository-manager-oss
2.2 安装
下载nexus-2.x-bundle.zip,解压即可
3 启动【了解】
- 解压后在bin目录中执行:
- nexus install 在系统中安装nexus服务
- nexus uninstall 卸载nexus服务
- nexus start 启动服务
- nexus stop 停止服务
4 Nexus登录【了解】
访问私服:http://localhost:8081/nexus/
4.1仓库列表【了解】
仓库类型 | 描述 |
---|---|
group | 包含多个仓库,通过group库的地址可以从包含的多个仓库中查找构件 |
hosted | 私服 服务器本地的仓库,其中存储诸多构件 |
proxy | 代理仓库,其会关联一个远程仓库, 比如中央仓库,aliyun仓库,向该仓库查找构件时,如果没有会从其关联的仓库中下载 |
仓库名 | 描述 |
---|---|
Releases | 存放项目的稳定发布版本,一个模块做完后如果需要共享给他人,可以上传到私服的该库 |
Snapshots | 对应不稳定的发布版本 |
3rd party | 存放中央仓库没有的 ,如ojdbc.jar,可以上传到私服的该库中 |
5 Maven配置私服 【重点
】
在maven中配置私服,使得maven可以从私服上获取构件
5.1 Maven关联私服
配置settings.xml,设置私服地址、认证等信息
<servers>
<server>
<id>nexus-public</id> <!-- nexus的认证id -->
<username>admin</username> <!--nexus中的用户名密码-->
<password>admin123</password>
</server>
</servers>
<profiles>
<!-- 在已有的profiles标签中添加profile标签 -->
<profile>
<id>myjdk</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>
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus-public</id> <!--nexus认证id 【此处的repository的id要和 <server>的id保持一致】-->
<!--name随便-->
<name>Nexus Release Snapshot Repository</name>
<!--地址是nexus中仓库组对应的地址-->
<url>http://localhost:8081/nexus/content/groups/public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories> <!--插件仓库地址,各节点的含义和上面是一样的-->
<pluginRepository>
<id>nexus-public</id> <!--nexus认证id 【此处的repository的id要和 <server>的id保持一致】-->
<!--地址是nexus中仓库组对应的地址-->
<url>http://localhost:8081/nexus/content/groups/public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>yourjdk</activeProfile>
<!-- 使私服配置生效 -->
<activeProfile>nexus</activeProfile>
</activeProfiles>
至此,Maven项目中需要依赖时,Maven会从私服中下载
5.2 Maven项目部署到私服
- 执行 :mvn deploy 即可将项目部署到私服对应的仓库中,此时项目中的打包方式多为jar
- 但需要提前在项目的pom.xml中配置部署私服仓库位置,如下:
...
<dependencies>
.....
</dependencies>
<!-- 在项目的pom.xml中 配置私服的仓库地址,可以将项目打jar包部署到私服 -->
<distributionManagement>
<repository>
<id>nexus-public</id> <!-- nexus认证id -->
<url>http://localhost:8081/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>nexus-public</id> <!-- nexus认证id -->
<url>http://localhost:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
</project>
注意:如上的 repository的 id 依然是要和settings.xml中配置的server中的id 一致,才能通过私服的认证