公司在自己的局域网内搭建自己的远程仓库服务器,称为私服(私有服务器),私服服务器即是公司内部的maven远程仓库,每个员工的电脑上安装maven软件并且连接私服服务器,员工将自己开发的项目打成jar并发布到私服服务器,其它项目组从私服服务器下载所依赖的构件(jar)。
Maven私服作用:
1、我们不能联网的情况下,它帮我们从互联网下载jar(私服中jar的来源1)
2、自己开发的jar上传到私服供(共享给)团队使用(私服中jar的来源2)
3、最终私服就是让我们下载jar的
私服还充当一个代理服务器,当私服上没有jar包会从互联网中央仓库自动下载。
Maven服务器:某一个机器上安装了可以提供Maven私服功能的软件,那么这个机器就叫做Maven私有服务器。
(一)搭建私服环境
nexus是Maven仓库管理器(maven私服软件),通过nexus可以搭建maven私服仓库,同时nexus还提供强大的仓库管理功能等。
l 安装nexus
n 解压nexus-2.12.0-01-bundle.zip(无中文和空格路径下),进入bin目录
n 管理员身份运行cmd,进入bin目录,执行nexus.bat install(若安装后卸载可执行nexus.bat uninstall)
n services.msc进入服务管理,查看nexus服务
n 若nexus服务已经安装,可以使用两种方式启动nexus
u bin目录下执行nexus.bat start
u 直接启动nexus服务
n 查看nexus的配置文件conf/nexus.properties
n 访问http://localhost:8081/nexus/ 内置登录名:admin/admin123
n 仓库类型
nexus的仓库有4种类型:
-
hosted,宿主仓库,部署自己的jar到这个类型的仓库,包括releases和snapshot两部分,Releases公司内部发布版本仓库、 Snapshots 公司内部测试版本仓库
-
proxy,代理仓库,用于代理远程的公共仓库,如maven中央仓库,用户连接私服,私服自动去中央仓库下载jar包或者插件。
-
group,仓库组,用来合并多个hosted/proxy仓库,通常我们配置自己的maven连接仓库组。
-
virtual(虚拟):兼容Maven1 版本的jar或者插件
接下来三件事:
1、 把我们的项目发布到私服;
2、 从私服上下载jar;
3、 如何向私服上上传第三方的jar
(二)项目发布到nexus私服
对远程的一个操作:url、用户名、密码都得有,后续上传就是围绕这三个信息进行;
第一步: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>
<!--配置用户名密码结束-->
第二步:配置项目的pom.xml
<!--添加到要上传的项目pom中确定上传路径开始-->
<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中确定上传路径结束-->
注意:pom.xml这里 和 settings.xml 配置 对应
第三步:执行mvn deploy命令
(三)从nexus私服下载jar
在maven的settings.xml文件中配置下载模板(放在profiles标签内)
<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>
激活下载模板配置
<!--激活模板开始-->
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
<!--激活模板结束-->
测试:删除本地的dao工程,终端进入service工程下执行mvn compile
(四)把第三方jar放入本地仓库或者私服
第三方jar放入本地仓库
mvn install:install-file -Dfile=ojdbc14-10.2.0.4.0.jar -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.4.0 -Dpackaging=jar
第三方jar放入私服
maven的settings配置文件中配置第三方仓库的server信息
<server>
<id>thirdparty</id>
<username>admin</username>
<password>admin123</password>
</server>
执行命令
mvn deploy:deploy-file -Dfile=ojdbc14-10.2.0.4.0.jar -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.4.0 -Dpackaging=jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty