Maven私服

私服

私服是架设在局域网的一种特殊的远程仓库。有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库;否则,私服请求外部的远程仓库,将构件下载到私服,再提供给本地仓库下载。

私服可以解决在企业做开发时每次需要的jar包都要在中心仓库下载,且每次下载完只能被自己使用,不能被其他开发人员使用的弱点。

私服就是一个服务器,不是本地层面的,是公司(局域网/内网)层面的,公司中所有的开发人员都在使用同一个私服。

Maven私服  Maven私服

我们可以使用专门的 Maven 仓库管理软件来搭建私服,比如:Apache ArchivaArtifactorySonatype Nexus等。

我使用的 Sonatype Nexus

下载nexus-2.x-bundle.zip,解压即可
(可以配置一下环境变量)
解压后在bin目录中执行:nexus install  在系统中安装nexus服务
​                    nexus uninstall 卸载nexus服务
​                    nexus start   启动服务
​                    nexus stop   停止服务
注:必须使用管理员身份打开cmd才可执行上述命令

访问私服:http://localhost:8081/nexus/
登录私服:admin/admin123
  • 导入第三方jar到私服:

有些jar在中心库中是没有的,比如oracle的数据库驱动jar:ojdb6.jar. 可以自己下载jar,然后上传到私服中。

此种构件,建议放在 "3rd party" 仓库中 ( 存放第三方构件 )

Maven私服

Maven私服

Maven私服

Maven私服

  •  仓库组

私服中有一个仓库组,组中包含多个仓库,可以指定仓库组的url,即可从多个仓库中获取构件

Maven私服

  • Maven关联私服

Maven 配置文件:E:\Maven\apache-maven-3.3.9\conf\settings.xml

<!--配置私服 username/password:-->
        <server>
		<id>nexus-public</id>
		<username>admin</username>
		<password>admin123</password>
	</server>
  </servers>
<!--配置私服为maven的远程仓库,
注意:【此处的repository的id要和 上图中server的id保持一致,才可以在私服认证通过】-->
         <profile>
	      <id>nexus</id>
	      <repositories>
	        <repository>
	          <id>nexus-public</id>
	          <url>http://localhost:8081/nexus/content/groups/public</url>
		  <releases><enabled>true</enabled></releases>
		  <snapshots><enabled>true</enabled></snapshots>
	        </repository>
	      </repositories>
	  <pluginRepositories>
		<repository>
	          <id>nexus-public</id>
	          <url>http://localhost:8081/nexus/content/groups/public</url>
		  <releases><enabled>true</enabled></releases>
		  <snapshots><enabled>true</enabled></snapshots>
	        </repository>
	  </pluginRepositories>
	  </profile>
    </profiles>
    <!-- 让增加的 profile生效 -->
	<activeProfiles>
		<activeProfile>nexus</activeProfile>
	</activeProfiles>

 

  • 项目加载到私服中:

在项目的pom中配置部署仓库位置:

  <!--将项目打包安装到私服上-->
  <distributionManagement>
      <repository>
          <id>nexus-public</id><!--和Maven配置中的server的id一致才能通过私服的认证-->
          <name>release</name><!--release版本部署位置-->
          <url>http://localhost:8081/nexus/content/repositories/releases</url>
      </repository>
      <snapshotRepository>
          <id>nexus-public</id>
          <name>snapshots</name><!--snapshots版本部署位置-->
          <url>http://localhost:8081/nexus/content/repositories/snapshots</url>
      </snapshotRepository>
  </distributionManagement>

执行 :mvn deploy

即可将项目部署到私服对应的仓库中,此时项目中的打包方式多为 jar