Windows 下Nexus搭建Maven私服



   

       Windows 下Nexus搭建Maven私服            

       分类:            java            2014-10-16 11:46    577人阅读    评论(0)    收藏    举报    html

一、 为何使用Nexus java

        若是没有私服,咱们所需的全部构件都须要经过maven的中央仓库和第三方的Maven仓库下载到本地,而一个团队中的全部人都重复的从maven仓库下 载构件无疑加大了仓库的负载和浪费了外网带宽,若是网速慢的话,还会影响项目的进程。不少状况下项目的开发都是在内网进行的,链接不到maven仓库怎么 办呢?开发的公共构件怎么让其它项目使用?这个时候咱们不得不为本身的团队搭建属于本身的maven私服,这样既节省了网络带宽也会加速项目搭建的进程, 固然前提条件就是你的私服中拥有项目所需的全部构件。windows

 

二、Nexus下载 网络

       下载地址:http://www.sonatype.org/nexus/goapp

 

三、Nexus启动 maven

       我下载的是zip包,解压后进入\nexus-2.1.2-bundle\nexus-2.1.2\bin\jsw\,根据操做系统类型选择文件夹,我选的是windows-x86-32文件夹,进入后可看到以下所示bat文件。测试




双击console-nexus.bat运行。游览器中输入http://127.0.0.1:8081/nexus/,出现图(2)所示就表明nexus已经启动成功。url




 

8081为默认的端口号,要修改端口号可进入nexus-2.1.2-bundle\nexus-2.1.2\conf\打开nexus.properties文件,修改application-port属性值就能够了。spa


默认的用户名和密码:admin/admin123,登陆后看到图(3)所示:操作系统


 

图(3)

 

四、Nexus仓库

     nexus的仓库类型分为如下四种:

               group: 仓库组

               hosted:宿主

              proxy:代理

              virtual:虚拟

 

            首次登录nexus后能够看到如下一个仓库组和多个仓库。

 

 



   Public Repositories:  仓库组

                      3rd party: 没法从公共仓库得到的第三方发布版本的构件仓库

                      Apache Snapshots: 用了代理ApacheMaven仓库快照版本的构件仓库

                      Central: 用来代理maven中央仓库中发布版本构件的仓库

                      Central M1 shadow: 用于提供中央仓库中M1格式的发布版本的构件镜像仓库

                      Codehaus Snapshots: 用来代理CodehausMaven 仓库的快照版本构件的仓库

                      Releases: 用来部署管理内部的发布版本构件的宿主类型仓库

                      Snapshots:用来部署管理内部的快照版本构件的宿主类型仓库

 

4.一、建立Nexus宿主仓库

在Repositories选项页的菜单栏上点击Add按钮会出现以下所示,选择要添加的仓库类型。

 



这里我点击添加宿主类型的仓库,在仓库列表的下方会出现新增仓库的配置,以下所示:




 点击save按钮后就会在仓库列表中看到刚才新增的仓库。


4.二、建立Nexus代理仓库


点击菜单栏上的Add按钮后选择Proxy Repository,看到以下所示配置界面:

 


 

 

4.三、建立Nexus仓库组


仓库组和仓库关系是一对多的关系,一个仓库组能够指向多个仓库。

点击菜单栏上的Add按钮选择Repository Group就能够看到仓库组的配置界面,以下所示:




 


点击save后就可在仓库列表中看到新增的仓库组了,项目中若是要下载构件的话,配置文件中通常都用仓库组的URL。


五、修改Maven配置文件从Nexus下载构件

1) 若是想对操做系统的全部用户统一配置maven,则只需修改maven\conf\setting.xml 文件就能够了,若是只想对用户单独配置maven,只需将conf\setting.xml文件复制到C:\Documents and Settings\Administrator\.m2文件夹下(我这里假设系统装在c盘,用户为Administrator)。

 

2)  打开setting.xml文件,能够看到以下代码:

 

Java代码 复制代码 收藏代码

  1. <!-- localRepository     

  2.    | The path to the local repository maven will use to store artifacts.     

  3.    |     

  4.    | Default: ~/.m2/repository      

  5.   <localRepository></localRepository>     

  6.  -->  

[java] view plaincopy在CODE上查看代码片派生到个人代码片

  1. <!-- localRepository    

  2.    | The path to the local repository maven will use to store artifacts.    

  3.    |    

  4.    | Default: ~/.m2/repository     

  5.   <localRepository></localRepository>    

  6.  -->  

 

表示若是不设置localRepository,maven会默认将本地仓库建到/.m2/repository文件夹下。


设置localRepository以下代码所示:

 

Html代码 复制代码 收藏代码

  1. <localRepository>F:\myCenterRepository</localRepository>    

[html] view plaincopy在CODE上查看代码片派生到个人代码片

  1. <localRepository>F:\myCenterRepository</localRepository>    

 

表示在myCenterRepository文件夹下创建本地仓库。我的建议不要采用默认的仓库地址,由于项目若是不少的话,那么本地仓库所占的磁盘空间就比较多了,因此指定仓库地址到其余盘符,更方便管理。

 

5.二、在POM文件中配置Nexus仓库

在项目的pom文件中添加以下代码:

 

Xml代码 复制代码 收藏代码

  1. <repositories>        

  2.     <repository>        

  3.       <id>nexus</id>        

  4.       <name>my-nexus-repository</name>        

  5.       <url>http://127.0.0.1:7788/nexus/content/groups/public/</url>        

  6.       <releases>        

  7.         <enabled>true</enabled>        

  8.       </releases>        

  9.       <snapshots>        

  10.         <enabled>false</enabled>        

  11.       </snapshots>        

  12.     </repository>        

  13.   </repositories>        

  14.   <pluginRepositories>        

  15.     <pluginRepository>        

  16.       <id>nexus</id>        

  17.       <name>my-nexus-repository</name>        

  18.       <url>http://127.0.0.1:7788/nexus/content/groups/public/</url>        

  19.       <releases>        

  20.         <enabled>true</enabled>        

  21.       </releases>        

  22.       <snapshots>        

  23.         <enabled>false</enabled>        

  24.       </snapshots>             

  25.     </pluginRepository>        

  26.   </pluginRepositories>       

[xml] view plaincopy在CODE上查看代码片派生到个人代码片

  1. <repositories>       

  2.     <repository>       

  3.       <id>nexus</id>       

  4.       <name>my-nexus-repository</name>       

  5.       <url>http://127.0.0.1:7788/nexus/content/groups/public/</url>       

  6.       <releases>       

  7.         <enabled>true</enabled>       

  8.       </releases>       

  9.       <snapshots>       

  10.         <enabled>false</enabled>       

  11.       </snapshots>       

  12.     </repository>       

  13.   </repositories>       

  14.   <pluginRepositories>       

  15.     <pluginRepository>       

  16.       <id>nexus</id>       

  17.       <name>my-nexus-repository</name>       

  18.       <url>http://127.0.0.1:7788/nexus/content/groups/public/</url>       

  19.       <releases>       

  20.         <enabled>true</enabled>       

  21.       </releases>       

  22.       <snapshots>       

  23.         <enabled>false</enabled>       

  24.       </snapshots>            

  25.     </pluginRepository>       

  26.   </pluginRepositories>       

 

在pom文件中配置只对当前项目有效,而实际开发中不可能在每一个项目中重复配置信息,因此不建议在pom文件中配置。

 

5.三、在setting.xml文件中配置Nexus仓库

1)maven提供了profile来配置仓库信息,以下所示:

 

Xml代码 复制代码 收藏代码

  1. <profiles>     

  2.     <profile>     

  3.       <id>myprofile</id>     

  4.       <repositories>     

  5.             <repository>     

  6.                 <id>central</id>                                        

  7.                 <url>http://central</url>                           

  8.                 <releases>     

  9.                     <enabled>true</enabled>     

  10.                 </releases>     

  11.                 <snapshots>     

  12.                     <enabled>true</enabled>     

  13.                 </snapshots>     

  14.             </repository>     

  15.         </repositories>        

  16.          <pluginRepositories>     

  17.             <pluginRepository>     

  18.               <id>central</id>     

  19.               <url>http://central</url>     

  20.               <releases>     

  21.                 <enabled>true</enabled>     

  22.               </releases>     

  23.               <snapshots>     

  24.                 <enabled>false</enabled>     

  25.               </snapshots>     

  26.             </pluginRepository>     

  27.         </pluginRepositories>     

  28.     </profile>     

  29. </profiles>    

[xml] view plaincopy在CODE上查看代码片派生到个人代码片

  1. <profiles>    

  2.     <profile>    

  3.       <id>myprofile</id>    

  4.       <repositories>    

  5.             <repository>    

  6.                 <id>central</id>                                       

  7.                 <url>http://central</url>                          

  8.                 <releases>    

  9.                     <enabled>true</enabled>    

  10.                 </releases>    

  11.                 <snapshots>    

  12.                     <enabled>true</enabled>    

  13.                 </snapshots>    

  14.             </repository>    

  15.         </repositories>       

  16.          <pluginRepositories>    

  17.             <pluginRepository>    

  18.               <id>central</id>    

  19.               <url>http://central</url>    

  20.               <releases>    

  21.                 <enabled>true</enabled>    

  22.               </releases>    

  23.               <snapshots>    

  24.                 <enabled>false</enabled>    

  25.               </snapshots>    

  26.             </pluginRepository>    

  27.         </pluginRepositories>    

  28.     </profile>    

  29. </profiles>    

 

 

 

2) 激活profile

Xml代码 复制代码 收藏代码

  1. <activeProfiles>     

  2.     <activeProfile>myprofile</activeProfile>     

  3.   </activeProfiles>    

[xml] view plaincopy在CODE上查看代码片派生到个人代码片

  1. <activeProfiles>    

  2.     <activeProfile>myprofile</activeProfile>    

  3.   </activeProfiles>    

 

3)配置镜像

Xml代码 复制代码 收藏代码

  1. <mirrors>     

  2.     <mirror>        

  3.      <id>nexus</id>         

  4.      <url>http://127.0.0.1:7788/nexus/content/groups/public/</url>        

  5.      <mirrorOf>*</mirrorOf>        

  6.    </mirror>     

  7.  </mirrors>    

[xml] view plaincopy在CODE上查看代码片派生到个人代码片

  1. <mirrors>    

  2.     <mirror>       

  3.      <id>nexus</id>        

  4.      <url>http://127.0.0.1:7788/nexus/content/groups/public/</url>       

  5.      <mirrorOf>*</mirrorOf>       

  6.    </mirror>    

  7.  </mirrors>    

 

这里配置mirrorOf的值为*,表明maven的全部访问请求都会指向到Nexus仓库组。

 

六、部署构件到Nexus仓库

6.一、maven部署

1) 修改pom文件

在pom文件中添加以下配置:

Xml代码 复制代码 收藏代码

  1. <distributionManagement>     

  2.        <repository>     

  3.            <id>my-nexus-releases</id>     

  4.            <url>http://127.0.0.1:7788/nexus/content/repositories/releases/</url>     

  5.        </repository>     

  6.             

  7.        <snapshotRepository>     

  8.            <id>my-nexus-snapshot</id>     

  9.            <url>http://127.0.0.1:7788/nexus/content/repositories/snapshots/</url>     

  10.        </snapshotRepository>     

  11.   </distributionManagement>    

[xml] view plaincopy在CODE上查看代码片派生到个人代码片

  1. <distributionManagement>    

  2.        <repository>    

  3.            <id>my-nexus-releases</id>    

  4.            <url>http://127.0.0.1:7788/nexus/content/repositories/releases/</url>    

  5.        </repository>    

  6.            

  7.        <snapshotRepository>    

  8.            <id>my-nexus-snapshot</id>    

  9.            <url>http://127.0.0.1:7788/nexus/content/repositories/snapshots/</url>    

  10.        </snapshotRepository>    

  11.   </distributionManagement>    

 2)在setting.xml文件中添加认证信息:

Xml代码 复制代码 收藏代码

  1. <servers>     

  2.      <server>     

  3.       <id>my-nexus-releases</id>     

  4.       <username>admin</username>     

  5.       <password>admin123</password>     

  6.     </server>     

  7.     <server>     

  8.       <id>my-nexus-snapshot</id>     

  9.       <username>admin</username>     

  10.       <password>admin123</password>     

  11.     </server>     

  12. </servers>    

[xml] view plaincopy在CODE上查看代码片派生到个人代码片

  1. <servers>    

  2.      <server>    

  3.       <id>my-nexus-releases</id>    

  4.       <username>admin</username>    

  5.       <password>admin123</password>    

  6.     </server>    

  7.     <server>    

  8.       <id>my-nexus-snapshot</id>    

  9.       <username>admin</username>    

  10.       <password>admin123</password>    

  11.     </server>    

  12. </servers>    

 上面的配置中我用的是超级管理员的帐户,开发项目中能够改成具备部署构件权限的用户就能够了。

3)执行部署

测试的构件项目信息以下:

Xml代码 复制代码 收藏代码

  1. <groupId>com.ez</groupId>     

  2.   <artifactId>TestJar</artifactId>     

  3.   <version>1.0</version>     

  4.   <packaging>jar</packaging>     

  5.   <name>TestJar</name>    

[xml] view plaincopy在CODE上查看代码片派生到个人代码片

  1. <groupId>com.ez</groupId>    

  2.   <artifactId>TestJar</artifactId>    

  3.   <version>1.0</version>    

  4.   <packaging>jar</packaging>    

  5.   <name>TestJar</name>    

 

从上面的信息中能够看出构件为发布版本,因此部署构件的话会自动部署至releases仓库中。

 

在命令行中执行:mvn clean deploy

若是以前没用执行过该命令,maven会自动到中央仓库中下载部署所需的插件。最后在命令行中看到以下所示就表明构件已经部署成功。

 

 


到nexus的releases仓库中查看刚刚部署好的构件信息以下所示:

 


若是部署失败的要检查一下用户是否有部署的权限,目标仓库是否容许部署,是否缺乏部署所需的构件。


6.二、手动部署

手动部署构件则更为简单了,在nexus的仓库列表中点击要部署的目标仓库,而后点击Artifact Upload选项卡看到下图所示:

 


经过以上配置运用Nexus搭建的私服基本上能够用了,更多配置需求可参考Nexus book.


Nexus book下载地址:http://download.csdn.net/detail/yaoqinzhou1943/4589583



       

    相关文章
    相关标签/搜索