1.私服简介
私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件。有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库;否则,私服请求外部的远程仓库,将构件下载到私服,再提供给本地仓库下载。
我们可以使用专门的 Maven 仓库管理软件来搭建私服,比如:Apache Archiva,Artifactory,Sonatype Nexus。这里我们使用 Sonatype Nexus。
2.windows上安装Sonatype Nexus
Nexus Pro专业版是需要付费的,这里我们下载开源版 Nexus OSS。Nexus 提供两种安装包,一种是包含 Jetty 容器的 bundle 包,另一种是不包含容器的 war 包。https://help.sonatype.com/repomanager2/download
2.1 我们下载Nexus Repository Manager 2 OSS,解压zip文件到一个你喜欢的目录,
我选择解压到D:\Program Files\develop\nexus-2.14.10-01-bundle,结构目录如下
2.2 安装
cmd进入bin目录 D:\Program Files\develop\nexus-2.14.10-01-bundle\nexus-2.14.10-01\bin(为方便启动和退出Nexus,可将bin目录添加到环境变量),
在bin目录执行
nexus install
将Nexus安装为Windows服务。可以通过services.msc命令查看管理window的nexus服务。可将服务启动方式设为手动,以后通过 nexus start 即可启动Nexus ,通过 nexus stop 退出Nexus。为了方便可将服务启动方式设为自动。
修改nexus的配置文件
# Jetty section
application-port=18081 #修改了端口号
application-host=0.0.0.0
nexus-webapp=${bundleBasedir}/nexus
nexus-webapp-context-path=/nexus
# Nexus section
nexus-work=${bundleBasedir}/../sonatype-work/nexus #保持默认,为nexus存放包的地方
runtime=${bundleBasedir}/nexus/WEB-INF
# orientdb buffer size in megabytes
storage.diskCache.bufferSize=4096
2.3 启动nexus
nexus start
打开浏览器,访问:http://localhost:18081/nexus/:
3.管理Sonatype Nexus
3.1 点击右上角 Log In,使用用户名:admin ,密码:admin123 登录,可使用更多功能:
点击左侧 Repositories 链接,查看 Nexus 内置的仓库:
Nexus 的仓库分为这么几类:
- hosted 宿主仓库:主要用于部署无法从公共仓库获取的构件(如 oracle 的 JDBC 驱动)以及自己或第三方的项目构件;
- proxy 代理仓库:代理公共的远程仓库;
- virtual 虚拟仓库:用于适配 Maven 1;
- group 仓库组:Nexus 通过仓库组的概念统一管理多个仓库,这样我们在项目中直接请求仓库组即可请求到仓库组管理的多个仓库。
3.2 增加一个仓库,这里我们添加一个代理仓库
以 Sonatype 为例,添加一个代理仓库,用于代理 Sonatype 的公共远程仓库。点击菜单 Add - Proxy Repository :
填写Repository ID - sonatype;Repository Name - Sonatype Repository;
Remote Storage Location - http://repository.sonatype.org/content/groups/public/ ,save 保存:
将添加的 Sonatype 代理仓库加入 Public Repositories 仓库组。选中 Public Repositories,在 Configuration 选项卡中,将 Sonatype Repository 从右侧 Available Repositories 移到左侧 Ordered Group Repositories,save 保存:
3.3 为了更好的使用 Nexus 的搜索,我们可以设置所有 proxy 仓库的 Download Remote Indexes 为 true,即允许下载远程仓库索引。
索引下载成功之后,在 Browse Index 选项卡下,可以浏览到所有已被索引的构件信息,包括坐标、格式、Maven 依赖的 xml 代码:
有了索引,我们就可以搜索了:
3.4 为了让maven重新覆盖部署之前的版本,把host仓库的"Deployment Policy"要选择"Allow Redeploy"
4.安装maven
4.1 下载maven http://maven.apache.org/download.cgi,把下载的zip包解压
4.2 配置M2_HOME的环境变量,然后将该变量添加到Path中
5. 在idea上的file--> settings 设置maven目录
6. 配置maven
maven配置文件D:\Program Files\develop\apache-maven\apache-maven-3.5.0\conf\settings.xml在servers标签上增加下面内容,以至于部署自己的包到host仓库能自动取到用户名和密码
<server>
<id>snapshots1</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>releases1</id>
<username>admin</username>
<password>admin123</password>
</server>
在mirrors标签增加以下内容,这样就能下载到项目里 <dependencies>标签下的依赖包了
<mirror>
<id>central1</id>
<mirrorOf>*</mirrorOf> <!-- * 表示让所有仓库使用该镜像-->
<!-- <name>central-mirror</name> -->
<url>http://192.168.1.4:18081/nexus/content/groups/public/</url>
</mirror>
7.idea上新建一个maven工程,pom.xml重要的内容如下
<groupId>com.cmb.com</groupId>
<artifactId>spring-coret</artifactId>
<packaging>jar</packaging>
<version>6.6</version>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-streams-scala_2.11</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.4.2</version>
</dependency>
</dependencies>
<distributionManagement>
<snapshotRepository>
<id>snapshots1</id>
<url>http://192.168.1.4:18081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>releases1</id>
<url>http://192.168.1.4:18081/nexus/content/repositories/releases</url>
</repository>
</distributionManagement>
说明:
7.1. <groupId>com.cmb.com</groupId> //包名,一般是公司的名字
<artifactId>spring-coret</artifactId>//项目名
<packaging>jar</packaging>//打包方式是jar还是war或者其他的
<version>6.6</version>
|
7.2.java版本1.8和编码为UTF-8
<properties>j
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
7.3 你项目要依赖的其他包
<dependencies>
</dependencies>
7.4
<distributionManagement>
<snapshotRepository>
<id>snapshots1</id>
<url>http://192.168.1.4:18081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>releases1</id>
<url>http://192.168.1.4:18081/nexus/content/repositories/releases</url>
</repository>
</distributionManagement>
<id></id>一定要maven的settings.xml一致,以至于才能上传部署自己的包时能使用密码,不一定要和nexus的目标库一致的id,不过最好一致,一遍好理解
<url></url>一定要和nexus库目录地址一致
8.mvn deploy就能根据项目pom.xml配置的<version>和<distributionManagement>及maven设置的<servers>
deploy到nenux下的releases库.
过程:
1.根据pom.xml的设置生成spring-coret-6.6.jar包
<groupId>com.cmb.com</groupId>
<artifactId>spring-coret</artifactId>
<packaging>jar</packaging>
<version>6.6</version>
2.因为<version>不带-SNAPSHOT名字,上传到pom.xml的设置<repository>而不是<snapshotRepository>
<snapshotRepository>
<id>snapshots1</id>
<url>http://192.168.1.4:18081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>releases1</id>
<url>http://192.168.1.4:18081/nexus/content/repositories/releases</url>
</repository>
3.根据 <repository> 的<id>releases1</id>找到maven的servers标签的设置的密码和用户名,用该账户上传打包的jar包到<id>releases1</id>的地址<url>http://192.168.1.4:18081/nexus/content/repositories/releases</url>
<server>
<id>releases1</id>
<username>admin</username>
<password>admin123</password>
</server>
在deploy之前会下载第三方依赖包并编译成jar包
以下面项目pom.xml的一个依赖为例子
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.4.2</version>
</dependency>
maven发现本地D:\Program Files\develop\apache-maven\repository(该目录在idea里的maven设置里配置了)有这个包就不会向nexus要,没有这个包会向根据配置
<mirror>
<id>central1</id>
<mirrorOf>*</mirrorOf> <!-- * 表示让所有仓库使用该镜像-->
<!-- <name>central-mirror</name> -->
<url>http://192.168.1.4:18081/nexus/content/groups/public/</url>
</mirror>
向nexus要,nexus根据配置看本地D:\Program Files\develop\nexus-2.14.10-01-bundle\sonatype-work\nexus有没有,有的话直接给maven,没有的话向public repositories配置的所有仓库要,远程仓库就会去下载并且把下载的包放在D:\Program Files\develop\nexus-2.14.10-01-bundle\sonatype-work\nexus下,然后返回给maven。
参考:
https://www.cnblogs.com/yucy/p/7509561.html
https://www.cnblogs.com/ctxsdhy/p/6105805.html
http://www.cnblogs.com/luotaoyeah/p/3791966.html