目录
1.maven私服介绍
1.1 私服介绍
1.2 Nexus介绍
2. maven私服实战
2.1 nexus安装
2.2 nexus仓库类型
2.3 将项目发布到私服
2.4 从私服下载jar包
2.5 将第三方jar包发布到私服
1.maven私服介绍
1.1 私服介绍
正式开发时,不同的项目组开发不同的工程。maven-dao 工程开发完毕,发布到私服maven-service 从私服下载 dao。
公司在自己的局域网内搭建自己的远程仓库服务器,称为私服, 私服服务器即是公司内部的 maven 远程仓库, 每个员工的电脑上安装 maven 软件并且连接私服服务器,员工将自己开发的项目打成 jar 并发布到私服服务器,其它项目组从私服服务器下载所依赖的构件(jar)。
私服还充当一个代理服务器,当私服上没有 jar 包会从互联网中央仓库自动下载。
1.2 Nexus介绍
Nexus 是 Maven 仓库管理器, 通过 nexus 可以搭建 maven 仓库,同时 nexus 还提供强大的仓库管理功能,构件搜索功能等。
2. maven私服实战
2.1 nexus安装
- 将安装包存放到一个不含中文的目录下,查看conf文件下的nexus.properties配置文件,可以修改对应的配置。
2. nexus的安装命令:使用管理员运行cmd命令窗口,切换目录nexus\nexus-2.12.0-01\bin目录下,执行nexus.bat install进行安装。执行nexus.bat start 启动服务 执行nexus.bat stop停止服务。
3. nexus的卸载命令:使用管理员运行cmd命令窗口,切换目录nexus\nexus-2.12.0-01\bin目录下,执行nexus.bat uninstall进行卸载
4. 访问图形化界面:打开浏览器,输入http://localhost:端口号/nexus访问
5. 点击log in,进行登录。用户名:admin 密码:admin123
2.2 nexus仓库类型
nexus提供了不同的仓库类型:
- hosted:宿主仓库, 部署自己的 jar 到这个类型的仓库,包括 releases 和 snapshot 两部分, Releases 公司内部发布版本仓库、 Snapshots 公司内部测试版本仓库
- proxy:代理仓库, 用于代理远程的公共仓库,如 maven 中央仓库,用户连接私服,私服自动去中央仓库下载 jar 包或者插件。
- group:仓库组,用来合并多个 hosted/proxy 仓库,通常我们配置自己的 maven 连接仓库组。Group仓库组也是可以自己进行定制的。
-
virtual:兼容 Maven1 版本的 jar 或者插件
2.3 将项目发布到私服
-
配置 Maven 环境
首先,你需要在客户端机器上配置 Maven 环境,并修改 settings.xml
文件,以配置连接私服的用户名和密码。这些凭据将用于私服进行身份验证。
<servers><!-- 稳定版本仓库配置 --><server><id>releases</id><username>admin</username><password>admin123</password></server><!-- 开发版本仓库配置 --><server><id>snapshots</id><username>admin</username><password>admin123</password></server><!-- 第三方仓库配置 --><server><id>thirdparty</id><username>admin</username><password>admin123</password></server>
</servers>
2. 配置项目 pom.xml
接下来,你需要配置项目 pom.xml,配置私服仓库的地址,本公司的自己的 jar 包会上传到私服的宿主仓库,根据工程的版本号决定上传到哪个宿主仓库,如果版本为 release 则上传到私服的 release 仓库,如果版本为snapshot 则上传到私服的 snapshot 仓库。
<distributionManagement><repository><id>releases</id><url>http://localhost:端口号/nexus/content/repositories/releases/</url></repository><snapshotRepository><id>snapshots</id><url>http://localhost:端口号/nexus/content/repositories/snapshots/</url></snapshotRepository>
</distributionManagement>
注意:这里的 id 标签的值要和 settings.xml配置文件中的id值保持一致。
3. 发布项目到私服
在配置完成后,你可以在项目目录中执行 mvn deploy
命令,将项目发布到私服上。
4. 查看私服结果
2.4 从私服下载jar包
- 在settings.xml配置文件配置私服的镜像文件
<mirrors><mirror><id>nexusmaven</id><mirrorOf>*</mirrorOf><name>nexus maven</name><url>http://localhost:端口号/nexus/content/groups/public/</url></mirror>
</mirrors>
2. 测试依赖管理
为了测试依赖管理是否正常工作,你可以先发布一个项目到私服,然后删除本地仓库中的相应 JAR 包,再使用其他项目去依赖该 JAR 包,检查是否能够从私服中成功下载。
在其他项目中,你可以这样引入依赖:
<!--依赖Hello这个项目-->
<dependencies><dependency><groupId>cn.tx.maven</groupId><artifactId>Hello</artifactId><version>1.0-RELEASES</version></dependency>
</dependencies>
如图:依赖此Hello项目的HelloFriend项目去私服下载所需要的jar包。
还有一种方式:配置仓库。可以修改自己项目的pom配置文件,添加仓库的配置。
<repositories><repository><id>nexus</id><name>nexusmaven</name><url>http://localhost:端口号/nexus/content/groups/public/</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository>
</repositories>
<pluginRepositories><pluginRepository><id>public</id><url>http://localhost:端口号/nexus/content/groups/public/</url><name>pluginRepositories</name></pluginRepository>
</pluginRepositories>
上面的方式不是特别的理想,需要在每一个项目的pom文件中都添加相同的配置,比比较麻烦。可以在settings.xml配置文件中添加配置,完成统一的设置。
<!-- 下载jar包配置 --><profile> <!--profile的id --><id>dev</id><repositories><repository> <!--仓库id,repositories可以配置多个仓库,保证id不重复 --><id>nexus</id> <!--仓库地址,即nexus仓库组的地址 --><url>http://localhost:端口号/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:端口号/nexus/content/groups/public/</url></pluginRepository></pluginRepositories></profile>
激活配置
<activeProfiles><activeProfile>dev</activeProfile>
</activeProfiles>
2.5 将第三方jar包发布到私服
第三方包上传到私有maven仓库里有两种方式:
1. maven仓库界面上传。maven界面上有上传第三方包按钮,可以直接按照要求上传即可。
2. maven命令行模式。
步骤:
①在settings.xml配置文件中配置用户名密码
<server><id>thirdparty</id><username>admin</username><password>admin123</password>
</server>
②maven命令上传第三方包。
mvn deploy:deploy-file -DgroupId=sdk的groupId -DartifactId=包的名称 -Dversion=版本号(如:0.0.1) -Dpackaging=包的类型 -Dfile=第三方sdk存放在本地的文件位置 -Durl=要上传到maven仓库的仓库位置 -DrepositoryId=maven中配置的server id
-DgroupId=com.alibaba 第三方包的groupId
-DartifactId=fastjson 第三方包的名称
-Dversion=1.2.7 版本号(如:0.0.1),建议用三位版本号表示
-Dpackaging=jar 上传的类型是jar类型
-Dfile=D:\repository\.....\fastjson-1.2.71.jar 第三方sdk存放在本地的文件位置
-Durl=http://localhost:8081/repository/...../ 要上传到maven仓库的仓库位置
-DrepositoryId=thirdparty setting.xml文件中配置server的ID
命令行上传成功截图:
私服界面: