Maven提高篇系列之(三)——使用自己的Repository(Nexus)

 

这是一个Maven提高篇的系列,包含有以下文章:

  

  1. Maven提高篇系列之(一)——多模块 vs 继承
  2. Maven提高篇系列之(二)——配置Plugin到某个Phase(以Selenium集成测试为例)
  3. Maven提高篇系列之(三)——使用自己的Repository(Nexus)
  4. Maven提高篇系列之(四)——使用Profile
  5. Maven提高篇系列之(五)——处理依赖冲突
  6. Maven提高篇系列之(六)——编写自己的Plugin(本系列完)

 

   

 

平时我们自己做的项目都是直接使用Maven提供的Central Repository,但是对于公司来说直接使用公共的Maven Central Repository就不见得是件好事了,比如我们需要考虑安全问题。此时你可以创建一个公司专属的Repository(Internal Repository),公司的所有项目都只和这个专属的Repository打交道,包括下载依赖,部署等。

 

总的来说,专属Repository有以下好处:

  • 代理外部Repository(比如Maven Central Repository),你可以对外部Repository做各种各样的过滤操作,比如你可以限制只使用Spring的某个版本。

  • 通过代理,专属Repository还可以起到缓存的作用,这样公司的每个开发者只需要从局域网的专属Repository下载依赖,而不用消耗对外网络资源。

  • 发布公司自己的项目,如果你开发的项目需要被公司的其他团队使用,而又不能发布到公司外部的Repository中,那么专属Repository是正中下怀的选择。

  • 发布一些购买的第三方软件产品以供公司所有人使用,比如Oracle的数据库Driver。

 

存在多种专属Repository,比如NexusArtifactory等,你甚至可以用一个FTP服务器作为一个专属Repository。本文将以开源的Nexus为例,演示如何将自己开发的项目部署到Nexus Repository中。

 

 

(一)下载并安装Nexus

下载Nexus的war包(本文使用的是nexus-2.5.war),将该war包放在tomcat服务器的webapps目录下,重启tomcat。打开http://localhost:8080/nexus-2.5/,你将看到以下页面:

 

 

 

点击右上角的“Log In”,输入用户名admin,密码admin123(这是Nexus默认的),此后点击右侧的“Repositories”,显示当前Nexus所管理的Repository,默认情况下Nexus为我们创建了以下主要的Repository:

  • Public Repositories,这是一个Repository Group,它所对应的URL为http://localhost:8080/nexus-2.5/content/groups/public/,该Repository  Group包含了多个Repository,其中包含了Releases、Snapshots、Third Party和Central。Repository Group的作用是我们只需要在自己的项目中配置该Repository Group就行了,它将自动从其所包含的Repository中下载依赖,比如如果我们声明对Spring的依赖,那么根据Repository Group中各个Repository的顺序(可以配置),Nexus将首先从Releases中下载Spring,发现没有,再从Snapshots中下载(极大可能也没有,因为它是个Snapshots的Repository),依次查找,最后可能在Central Repository中找到。在配置项目的Repository时,我们应该首先考虑Public Repositories。

  • 3rd party,该Repository即是存放你公司所购买的第三方软件库的地方,它是一个由Nexus自己维护的一个Repository。

  • Apache Snapshots,看名字你就应该知道这是个什么样的Repository,这是一个代理Repository,即最终的依赖还是得在Apache官网上去下载,然后缓存在Nexus中。

  • Central,这就是代理Maven Central Repository的Repository。

  • Releases,你自己的项目要发布时,就应该发布在这个Repository,他也是Nexus自己维护的Repository,而不是代理。

  • Snapshots,你自己项目Snapshot的Repository。

  

(二)用Nexus Repository取代Maven Central Repository

在完成(一)之后,我们只是创建了一个专属的Nexus Repository,我们的项目默认还是使用的Maven Central Repository,所以这时我们需要将Maven Central Repository换成自己创建的Nexus Repository,可以通过修改~/.m2/settings.xml来达到这样的目的。在该settings.xml文件中(没有的话可以创建一个),加入以下配置:

 

 <mirrors>

   <mirror>

     <!--This sends everything else to /public -->

     <id>nexus</id>

     <mirrorOf>*</mirrorOf>

     <url>http://localhost:8080/nexus-2.5/content/groups/public</url>

   </mirror>

 </mirrors>

 <profiles>

   <profile>

     <id>nexus</id>

     <!--Enable snapshots for the built in central repo to direct -->

     <!--all requests to nexus via the mirror -->

     <repositories>

       <repository>

         <id>central</id>

         <url>http://central</url>

         <releases><enabled>true</enabled></releases>

         <snapshots><enabled>true</enabled></snapshots>

       </repository>

     </repositories>

    <pluginRepositories>

       <pluginRepository>

         <id>central</id>

         <url>http://central</url>

         <releases><enabled>true</enabled></releases>

         <snapshots><enabled>true</enabled></snapshots>

       </pluginRepository>

     </pluginRepositories>

   </profile>

 </profiles>

 <activeProfiles>

   <!--make the profile active all the time -->

   <activeProfile>nexus</activeProfile>

 </activeProfiles>

 

 

以上配置通过mirror和profile的方式将central Repository全部转向到我们自己的Public Repositories,包括release版本和snapshot版本(Maven默认允许从Maven Central Repository下载release版本,这是合理的,如果你使用了别人的snapshot版本,一边你在使用,一边别人在开发,别人将API签名一换,哦豁)。这样一来,我们项目中的所有依赖都从Nexus的Public Repositories下载,由于其中包含了对Maven Central Repository的代理,所以此时Maven Central Repository中的类库也是可以间接下载到的。此时你可以将~/.m2/repository/中所有的内容删除掉,再在项目中执行“mvn clean install”,你将在终端中看到Maven已经开始从Nexus 的Public Repositories中下载依赖了,比如:

 

Downloading: http://localhost:8080/nexus-2.5/content/groups/public/org/codehaus/plexus/plexus-archiver/1.2/plexus-archiver-1.2.pom

 

 

请注意,此时我们的项目本身不需要做任何修改。我们只是创建了另一个Repository和修改了Maven的默认配置(学习完本文后,你应该需要将~/.m2/settings.xml还原,不然如果下次在构建之前自己的Nexus服务器没有启动,构建将失败。)。

 

 

(三)在项目中配置Nexus Repository的信息

接下来,我们开始着手如何将自己的项目部署到Nexus Repository中。这个也简单,第一我们需要在项目中指明部署目的Repository的URL,第二我们需要提供用户名和密码,哪能让你胡来。

 

我们知道,对于一个Maven项目而言,如果你的项目版本号中有“SNAPSHOT”字样,则表示此时的项目是snapshot版本,即处于开发中。否则,Maven则认为这是一个release版本。所以我们在部署时,需要分别配置这两种发布版本所对应的Repository。在项目的pom.xml文件中配置需要发布的目标Repository:

    

<distributionManagement>

       <repository>

           <id>releases</id>

           <name>Nexus Release Repository</name>

           <url>http://localhost:8080/nexus-2.5/content/repositories/releases/</url>

       </repository>

       <snapshotRepository>

           <id>snapshots</id>

           <name>Nexus Snapshot Repository</name>

           <url>http://localhost:8080/nexus-2.5/content/repositories/snapshots/</url>

       </snapshotRepository>

   </distributionManagement>

 

 

在上面的配置中,我们分别配置了release版本和snapshot版本所对应的Repository,如果你项目的版本中包含了“SNAPSHOT”,此时将发布到Nexus的Snapshots Repository,否则发布在Releases Repository。

 

至于提供用户名和密码,这些信息当然不能放在项目中,一是不安全,另外不同的人可能有不同的用户名和密码,你不至于在每次部署时都修改一次吧。所以,Maven将这些信息的存放地点放在了~/.m2/settings.xml文件中,每台机器(基本上就是每个人啦)都可以有不同的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>  

</servers>

 

 

admin和admin123都是Nexus默认的,另外特别需要注意的是,这里的<id>需要和上面项目pom.xml文件中配置Repostory的<id>对应起来。

 

(四)发布到Nexus Repository

万事具备,只欠东风,在项目中执行:

 

mvn deploy 

 

 

部署成功,再在Nexus中查看部署情况:

 

此时我们部署的是项目的snapshot版本,上图中的“me”目录中既包含了作者所部署的项目。

下一篇中,我们将讲到如何使用Profile。

posted @ 2016-08-13 18:24  无知者云  阅读(33776)  评论(0编辑  收藏  举报