Maven私服(远程仓库)搭建

1、下载maven私服工具

可以百度搜索 或 从百度网盘中下载

这里已经将工具上传到百度网盘,下载链接:

链接:https://pan.baidu.com/s/1eSh_TegWUFsEBlv92PWYiw 
提取码:cobz 

下载 windows系统安装包,即 nexus-2.12.0-01-bundle.zip 文件

2、安装私服

首先,先将  nexus-2.12.0-01-bundle.zip 文件解压到某个目录(目录不含空格)

解压后发现有两个目录,分别是:nexus-2.12.0-01 和 sonatype-work

nexus-2.12.0-01 目录:私服安装及运行时需要用到相关文件

私服仓库:在 sonatype-work\nexus 目录下面,由于还没有创建仓库,所以该目录下面没有任何目录或文件。

安装私服:将 私服安装到 windows 服务项中

使用 windows 命令行(以管理员身份运行),进入到 nexus-2.12.0-01\bin 目录,执行 nexus.bat install 安装私服

此时windows服务项中,可以找到服务名 为 nexus 的服务。说明私服安装成功。

3、启动私服服务器

注意私服服务器端口占用问题:服务器默认使用的是 8081 端口。(在 nexus-2.12.0-01\conf\nexus.properties 文件中可以看到相关信息 )

在 windows 服务项中,找到服务名 为 nexus 的服务,并启动该服务。

点击 “启动” 服务。

或者服务项中,右键启动。

私服服务器启动成功:

4、访问私服服务器(默认端口为 8081)

打开电脑中的浏览器:

输入地址 http://localhost:8081/nexus/  或者 http://192.168.1.106:8081/nexus/

浏览器自动跳转到  http://localhost:8081/nexus/#welcome 或者 http://192.168.1.106:8081/nexus/#welcome

这里以localhost地址为例:

登录服务器:默认账号及密码为   admin / admin123

登录成功:右上角会显示登录账号信息、左侧菜单可看到 Views/Repositories 选项。

5、查看仓库

左侧菜单可看到 Views/Repositories 选项, 点击 Repositories 查看仓库。

在私服安装目录 sonatype-work\nexus\storage 目录下,可以找到对应的仓库目录

6、上传 jar 包到私服

  • 配置maven环境,修改 settings 文件,配置连接私服的用户名和密码
<server>
 <id>releases</id>
 <username>admin</username>
 <password>admin123</password>
 </server>
<server>
 <id>snapshots</id>
 <username>admin</username>
 <password>admin123</password>
 </server>

releases 连接发布版本项目仓库
snapshots 连接测试版本项目仓库

  • 配置 (工程或模块的)pom.xml 文件

配置私服仓库的地址,本公司的自己的 jar 包会上传到私服的宿主仓库,如果工程的版本为 release 则上传到私服的 release 仓库,如果版本为 snapshot 则上传到私服的 snapshot 仓库,这里演示配置成localhost即可,如果是是公司中安装的私服地址应该是localhost替换成私服ip地址

在工程或模块的 pom.xml 文件中,添加如下代码:

<distributionManagement>
 <repository>
 <id>releases</id>
 <url>http://localhost:8081/nexus/content/repositories/releases/</url>
 </repository>
 <snapshotRepository>
 <id>snapshots</id>
 <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
 </snapshotRepository>
</distributionManagement>
注意:pom.xml 这里<id> 和 settings.xml 配置 <id> 对应!
  • 测试
以mall项目为例(包含了父工程 mall_parent、子模块有mall_dao、mall_service、mall_web), 将mall_dao 模块打成 jar 包发布到私服:
 
 
确定私服服务器已经启动,然后对子模块 mall_dao 执行 deploy 命令
 
执行 deploy 命令后,mall_dao将安装 jar 包到本地仓库,以及上传 jar  包到私服(远程仓库),查看 nexus 的 snapshot仓库,如果 version定义为 release则项目将发布到 nexus的 release 仓库,本项目将发布到 snapshot 仓库:
 
 
 
在网页中浏览器私服(远程仓库):
 
 
 
7、从私服(远程仓库)下载 jar 包
 
没有使用私服之前,如果本地仓库没有,去中央仓库下载,通常在企业中会在局域网
内部署一台私服服务器,有了私服本地项目首先去本地仓库找 jar,如果没有找到则连接私
服从私服下载 jar 包,如果私服没有 jar 包私服同时作为代理服务器从中央仓库下载 jar 包,
这样做的好处是一方面由私服对公司项目的依赖 jar 包统一管理,一方面提高下载速度,项
目连接私服下载 jar 包的速度要比项目连接中央仓库的速度快的多。
 
例子: 测试从私服下载 mall_dao 工程 jar 包。
  • 配置maven环境,修改 settings 文件,配置配置私服的仓库
在客户端的 setting.xml 中配置私服的仓库,由于 setting.xml 中没有 repositories 的配置
标签需要使用 profile 定义仓库。
<profile> 
 <!--profile 的 id-->
 <id>dev</id> 
 <repositories> 
     <repository> 
     <!--仓库 id,repositories 可以配置多个仓库,保证 id 不重复-->
     <id>nexus</id> 
     <!--仓库地址,即 nexus 仓库组的地址-->
     <url>http://localhost:8081/nexus/content/groups/public/</url> 
     <!--是否下载 releases 构件-->
     <releases> 
         <enabled>true</enabled> 
     </releases> 
     <!--是否下载 snapshots 构件-->
     <snapshots> 
         <enabled>true</enabled> 
     </snapshots> 
     </repository> 
 </repositories> 
 <pluginRepositories> 
     <!-- 插件仓库,maven 的运行依赖插件,也需要从私服下载插件 -->
     <pluginRepository> 
         <!-- 插件仓库的 id 不允许重复,如果重复后边配置会覆盖前边 -->
         <id>public</id> 
         <name>Public Repositories</name> 
         <url>http://localhost:8081/nexus/content/groups/public/</url> 
     </pluginRepository> 
 </pluginRepositories> 
 </profile>
使用 profile 定义仓库需要激活才可生效。
 
<activeProfiles>
    <activeProfile>dev</activeProfile>
</activeProfiles>

配置成功后通过 idea 查看有效 pom (effective pom),有效 pom 是 maven 软件最终使用的 pom 内容,程
序员不直接编辑有效 pom,打开有效 pom
 
有效 pom 内容如下:
下边的 pom 内容中有两个仓库地址,maven 会先从前边的仓库的找,如果找不到 jar 包再从
下边的找,从而就实现了从私服下载 jar 包。
 
 
<repositories>
 <repository>
 <releases>
 <enabled>true</enabled>
 </releases>
 <snapshots>
 <enabled>true</enabled>
 </snapshots>
 <id>public</id>
 <name>Public Repositories</name>
 <url>http://localhost:8081/nexus/content/groups/public/</url>
 </repository>
 <repository>
 <snapshots>
 <enabled>false</enabled>
 </snapshots>
 <id>central</id>
 <name>Central Repository</name>
 <url>https://repo.maven.apache.org/maven2</url>
 </repository>
 </repositories>
 <pluginRepositories>
 <pluginRepository>
 <id>public</id>
 <name>Public Repositories</name>
 <url>http://localhost:8081/nexus/content/groups/public/</url>
 </pluginRepository>
 <pluginRepository>
 <releases>
 <updatePolicy>never</updatePolicy>
 </releases>
 <snapshots>
 <enabled>false</enabled>
 </snapshots>
 <id>central</id>
 <name>Central Repository</name>
 <url>https://repo.maven.apache.org/maven2</url>
 </pluginRepository>
 </pluginRepositories>
  • 测试从私服下载 jar 包
测试 1:局域网环境或本地网络即可
在 mall_service 工程中添加 mall_dao 工程的依赖以及配置私服下载信息,删除本地仓库中 mall_dao jar包,同时在 idea 中关闭 mall_dao 工程。
 
 
 
 mall_service 模块重新编译,执行 compile 命令
 
 
项目先从本地仓库找 mall_dao,找不到从私服找,由于之前执行 deploy 将 mall_dao 部署到
私服中,所以成功从私服下载 mall_dao 并在本地仓库保存一份。
 
 
 
 
 
 
 
 
 
 
 
 
 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章