maven私服注意问题-nexus搭建maven私服及私服jar包上传和下载

http://blog.csdn.net/zwc0910/article/details/17349111html

 

1、nexus搭建maven私服及相关介绍

一、下载nexus-2.12.0-01-bundle.zip(版本随意)

二、以管理员身份运行cmd,cd进入解压文件的bin目录,执行nexus.bat installapache



若未以管理员身份运行则安装不了,由于权限不够


三、开启nexus服务,访问nexus服务器地址:http://localhost:8081/nexus/,默认登陆帐户为admin,默认密码为admin123,登陆成功后点击Repositories可看到私服有如下类型仓库:



1)hosted,宿主仓库,部署本身的jar到这个类型的仓库,包括releases和snapshot两部分,Releases公司内部发布版本仓库、 Snapshots 公司内部测试版本仓库
2)proxy,代理仓库,用于代理远程的公共仓库,如maven中央仓库,用户链接私服,私服自动去中央仓库下载jar包或者插件。 
3)group,仓库组,用来合并多个hosted/proxy仓库,一般咱们配置本身的maven链接仓库组。
4)virtual(虚拟):兼容Maven1 版本的jar或者插件 

四、nexus仓库默认在解压文件的sonatype-work\nexus\storage目录中:服务器

apache-snapshots:代理仓库,存储snapshots构件,代理地址https://repository.apache.org/snapshots/app

central-m1:virtual类型仓库,兼容Maven1 版本的jar或者插件eclipse

releases:本地仓库,存储releases构件。maven

snapshots:本地仓库,存储snapshots构件。测试

thirdparty:第三方仓库url

public:仓库组spa

 

2、向maven私服上传写好的jar
 .net

一、需求:将项目子模块ssm_dao这个工程打包成jar并上传到私服

二、第一步:须要在客户端即部署dao工程的电脑上配置 maven环境,并修改 settings.xml 文件,配置链接私服的用户

和密码。此用户名和密码用于私服校验,由于私服须要知道上传都 的帐号和密码 是否和私服中的帐号和密码 一致。

在maven文件夹下apache-maven-3.5.0\conf\settings.xml文件添加一下代码:(<servers>节点内)

 

[html] view plain copy

  1. <server>  
  2.      <!--releases 链接发布版本项目仓库-->  
  3.       <id>releases</id>  
  4.       <!--访问releases这个私服上的仓库所用的帐户和密码-->  
  5.       <username>admin</username>  
  6.       <password>admin123</password>  
  7.     </server>  
  8.     <server>  
  9.     <!--snapshots 链接测试版本项目仓库-->  
  10.       <id>snapshots</id>  
  11.       <!--访问releases这个私服上的仓库所用的帐户和密码-->  
  12.       <username>admin</username>  
  13.       <password>admin123</password>  
  14.     </server>  

 

三、在ssm_dao的pom.xml文件中添加一下代码:

[html] view plain copy

  1. <!--将ssm_dao上传私服  -->  
  2.  <distributionManagement>   
  3.  <!--pom.xml这里<id> 和 settings.xml 配置 <id> 对应  -->  
  4.     <repository>  
  5.         <id>releases</id>  
  6. <url>http://localhost:8081/nexus/content/repositories/releases/</url>  
  7.     </repository>   
  8.     <snapshotRepository>  
  9.         <id>snapshots</id>  
  10. <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>  
  11.     </snapshotRepository>   
  12.  </distributionManagement>  

根据工程的版本号决定上传到哪一个宿主仓库,若是版本为release则上传到私服的release仓库,若是版本为snapshot则上传到私服的snapshot仓库。

如:ssm_dao的工程的版本号为0.0.1-SNAPSHOT,则ssm_dao打包好的jar在本地仓库snapshots可见

[html] view plain copy

  1. <modelVersion>4.0.0</modelVersion>  
  2.  <parent>  
  3.    <groupId>com.nocol</groupId>  
  4.    <artifactId>ssm_parent</artifactId>  
  5.    <version><span style="color:#FF0000;">0.0.1-SNAPSHOT</span></version>  
  6.  </parent>  
  7.  <artifactId>ssm_dao</artifactId>  

四、正式上传:首先启动nexus服务,对ssm_dao工程执行deploy命令,看到BUILD SUCCESS则表示上传成功了


此时在\nexus-2.12.0-01-bundle\sonatype-work\nexus\storage\snapshots下能找到,可是在本地仓库并无,由于jar包上传在maven私服,接下来介绍如何能让本身上传的jar出如今本地仓库

如图:(本地snapshots)

如图:(私服)

五、此时将ssm_dao工程关闭,能够看到依赖ssm_dao的ssm_service工程出现感叹号(缺乏了ssm_dao.jar)

3、maven私服自动下载jar包
一、在没有配置nexus以前,若是本地仓库没有,去中央仓库下载。有了私服以后,本地项目首先去本地仓库找jar,若是没有找到则链接私服从私服下载jar包,若是私服没有jar包私服同时做为代理服务器从中央仓库下载jar包,这样提升了下载速度,项目链接私服下载jar包的速度要比项目链接中央仓库的速度快的多。

二、nexus中包括不少仓库,如上面介绍的hosted中存放的是本身发布的jar包及第三方公司的jar包,proxy中存放的是中央仓库的jar,为了方便从私服下载jar包能够将多个仓库组成一个仓库组,每一个工程须要链接私服的仓库组下载jar包,这样在项目中配置下载路径只须要给仓库组路径便可,即:

http://localhost:8081/nexus/content/groups/public/

三、第一步:在客户端的setting.xml中配置私服的仓库,因为setting.xml中没有repositories的配置标签须要使用profile定义仓库(profile节点内)

[html] view plain copy

  1. <profile>     
  2.     <!--profile的id-->  
  3.    <id>dev</id>     
  4.     <repositories>     
  5.       <repository>    
  6.         <!--仓库id,repositories能够配置多个仓库,保证id不重复-->  
  7.         <id>nexus</id>     
  8.         <!--仓库地址,即nexus仓库组的地址-->  
  9.         <url>http://localhost:8081/nexus/content/groups/public/</url>     
  10.         <!--是否下载releases构件-->  
  11.         <releases>     
  12.           <enabled>true</enabled>     
  13.         </releases>     
  14.         <!--是否下载snapshots构件-->  
  15.         <snapshots>     
  16.           <enabled>true</enabled>     
  17.         </snapshots>     
  18.       </repository>     
  19.     </repositories>    
  20.      <pluginRepositories>    
  21.         <!-- 插件仓库,maven的运行依赖插件,也须要从私服下载插件 -->  
  22.         <pluginRepository>    
  23.             <!-- 插件仓库的id不容许重复,若是重复后边配置会覆盖前边 -->  
  24.             <id>public</id>    
  25.             <name>Public Repositories</name>    
  26.             <url>http://localhost:8081/nexus/content/groups/public/</url>    
  27.         </pluginRepository>    
  28.     </pluginRepositories>    
  29.   </profile>    

使用profile定义仓库须要激活才可生效,再在profile结束标签后添加一下代码:

[html] view plain copy

  1. <!--使用profile定义仓库须要激活才可生效-->  
  2.   <activeProfiles>  
  3.    <activeProfile>dev</activeProfile>  
  4.  </activeProfiles>  

四、配置成功后经过eclipse查看ssm_service工程下pom.xml的Effective POM选项,可看到以下代码:

[html] view plain copy

  1. <repositories>  
  2.     <repository>  
  3.       <releases>  
  4.         <enabled>true</enabled>  
  5.       </releases>  
  6.       <snapshots>  
  7.         <enabled>true</enabled>  
  8.       </snapshots>  
  9.       <id>nexus</id>  
  10.       <url>http://localhost:8081/nexus/content/groups/public/</url>  
  11.     </repository>  
  12.     <repository>  
  13.       <snapshots>  
  14.         <enabled>false</enabled>  
  15.       </snapshots>  
  16.       <id>central</id>  
  17.       <name>Central Repository</name>  
  18.       <url>https://repo.maven.apache.org/maven2</url>  
  19.     </repository>  
  20.   </repositories>  
  21.   <pluginRepositories>  
  22.     <pluginRepository>  
  23.       <id>public</id>  
  24.       <name>Public Repositories</name>  
  25.       <url>http://localhost:8081/nexus/content/groups/public/</url>  
  26.     </pluginRepository>  
  27.     <pluginRepository>  
  28.       <releases>  
  29.         <updatePolicy>never</updatePolicy>  
  30.       </releases>  
  31.       <snapshots>  
  32.         <enabled>false</enabled>  
  33.       </snapshots>  
  34.       <id>central</id>  
  35.       <name>Central Repository</name>  
  36.       <url>https://repo.maven.apache.org/maven2</url>  
  37.     </pluginRepository>  
  38.   </pluginRepositories>  

表示当该工程须要的jar在本地仓库没有时,根据这里配置的访问路径自动去maven私服下载。此时再update一下父工程,发现ssm_service的感叹号消失(此时ssm_dao仍是close状态),说明ssm_service工程已经在maven私服内下载了ssm_dao.jar,同时在本地仓库也存在了该jar。

相关文章
相关标签/搜索