在项目中使用nexus

settings.xml 中配置远程仓库

Pom.xml的作用范围是一个项目,一个公司不可能只做一个项目,那么为了避免重复配置,那么我们需要把一些公共信息配置在 setting.xml中。但是setting.xml中并不支持<repositories> 及<pluginRepositories>,为了解决这个问题我们使用profile:

  1. <settings>   
  2.   …   
  3.   <profiles>   
  4.     <profile>   
  5.       <id> myProfiel</id>   
  6.       <!—在这里加入<repositories>及<pluginRepositories>–>  
  7.     </profile>   
  8.   </profiles>   
  9.   <activeProfiles>   
  10.     <activeProfile> myProfiel </activeProfile>   
  11.   </activeProfiles>   
  12.   …   
  13. </settings>   

这里通过<activeProfile>元素来激活这个profile,这样我们就可以全局的使用这个配置,不再需要为每个POM做重复的配置了。

在实际的操作过程中,这里我们最好不要配置远程仓库,最好能够通过nexus建立公司或者组织自己的仓库,然后这把把地址指向自己的仓库,后面我会介绍为什么要这么做,怎么做。

配置镜像

 

如果你想覆盖中央仓库的默认地址,那么这里我们就会使用的镜像了,还在setting.xml里面配置:

  1. <settings>   
  2. …   
  3.   <mirrors>   
  4.     <mirror>   
  5.       <id> maven-net-cn</id>   
  6.       <name> Maven China Mirror</name>   
  7.       <url> http://maven.net.cn/content/groups/public/</url>   
  8.       <mirrorOf> central</mirrorOf>   
  9.     </mirror>   
  10.   </mirrors>   
  11. …   
  12. </settings>   

这里解释一下<mirrorOf>,表示只为central仓库做镜像,如果想为所有的仓库做镜像那么可以改为:<mirrorOf>*</mirrorOf>

Maven 中使用 Nexus

   到此为止我们介绍了如何安装和使用Nexus以及其基本配置, 下面我们介绍下如何让Maven来使用Nexus本地仓库用来替代使用远程仓库. 在Maven使用Nexus本地仓库只需稍作配置, 在settings.xml中加入以下代码:

  <profiles>

    <profile>

       <id>dev</id>

          <repositories>

              <repository>

                 <id>nexus</id>

                  <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>

                  <releases>

                     <enabled>true</enabled>

                  </releases>

                  <snapshots>

                     <enabled>true</enabled>

                  </snapshots>

               </repository>

             </repositories>           

             <pluginRepositories>

                 <pluginRepository>

                     <id>nexus</id>

                     <url>http://127.0.0.1:8081/nexus/content/groups/public</url>

                     <releases>

                         <enabled>true</enabled>

                     </releases>

                     <snapshots>

                         <enabled>true</enabled>

                     </snapshots>

                 </pluginRepository>

             </pluginRepositories>

         </profile>

     </profiles>

    <activeProfiles>

         <activeProfile>dev</activeProfile>

     </activeProfiles>

  

这里配置了repository和pluginRepository, Maven在使用第三方构件和插件时是分开来配置的,所以如果我们也希望插件的下载也通过我们的本地仓库来下载,那么我们就需要配置pluginRepository.

红色字体部分就是我们之前安装的Nexus的地址, 这个地址可以是你们公司局域网内部的一台仓库服务器.

<releases> <enabled>true</enabled></releases>这个标签的作用是设定是否允许下载 

release版本的载构件, 同样snapshots标签可以设定是否允许下载snapshot版本的构件.

通常,我们不建议下载snapshot版本的构件,因为它是不稳定的版本, 除非你有特殊的需

求.

构件部署

 

有些时候我们需要部署构件到Nexus的3rd party, 比如我们在中央仓库找不到我们需要的构件, 我们可以通过Nexus的UI来上传构件:

点击左边菜单栏的 Repositories, 然后点击右边界面的3rd party, 选择界面下方的Artifact Upload, 这个时候出现以下界面:

上传构件需要两个步骤,一个是定义文件的上传,再就是构件的实体文件.

第一部分定义文件可以是POM文件, 这也是比较推荐的方式, 如果没有pom文件,可以

选择以参数的形式输入.

第二部分是上传构件的实体文件,这里简单说一下Classifier和Extension, 这两个都是选

填相, Classifier用来区别同功能的构件用于不同的场景, 比如这个构件是分别针对JDK14

和JDK15做了2个功能一样的Jar, 这个时候你就需要指定这个构件的Classifier为JDK14

还是JDK15. Extension是指扩展名,如果不提供,那么会自动取这个构件的Packaging Type

作为扩展名, 比如 ear, jar, war 等等. (Packaging Type是在第一步中通过pom文件或者手

工输入得到的)

刚才说了3rd party的部署, 关于releases 和 snapshots的UI部署也是一样的操作过程.

我们之前也讲过, 这里的releases和snapshots是用来部署我们自己的项目构件的, 通过

UI部署是可以,但是不是最高效的, 我们可以通过配置Maven来自动部署我们的项目构

件,这也是我们建立自己的仓库的一个非常重要的原因, 下面就让我们看看如何配置:

首先需要在POM文件中加入以下代码:

  1. <project>   
  2. …   
  3. <distributionManagement>   
  4.   <repository>   
  5.     <id> nexus-releases</id>   
  6.       <name> Nexus Release Repository</name>   
  7.       <url> http://127.0.0.1:8081/nexus/content/repositories/releases/</url>   
  8.   </repository>   
  9.   <snapshotRepository>   
  10.     <id> nexus-snapshots</id>   
  11.     <name> Nexus Snapshot Repository</name>   
  12.     <url> http://127.0.0.1:8081/nexus/content/repositories/snapshots/</url>   
  13.   </snapshotRepository>   
  14. </distributionManagement>   
  15. …   
  16. </project>   

这里配置,让Maven知道当我要发布release版本或者snapshot版本是需要发布到哪个地址.

然后我们需要在setting.xml里面配置一下Nexus的帐号和密码:

  1. <settings>   
  2. …   
  3. <servers>   
  4.   <server>   
  5.     <id> nexus-releases</id>   
  6.     <username> admin</username>   
  7.     <password> admin123</password>   
  8.   </server>   
  9.   <server>   
  10.     <id> nexus-snapshots</id>   
  11.     <username> admin</username>   
  12.     <password> admin123</password>   
  13.   </server>      
  14. </servers>   
  15. …   
  16. </settings>

 到此为止, 我们就可以通过命令mvn deploy或者通过IDE的可视化界面点击deploy来发布我们项目到本地仓库了. 通过这种方式我们可以很方便的进行模块间的依赖开发, 在后面的文章中我会详细介绍如何通过snapshot来让我们的依赖开发变得简单.

小结

本文介绍了Maven仓库, 如何通过Nexus建立自己本地仓库, 通过和远程仓库的比较, 我们知道Nexus给我带来很多方便之处,  方便我们管理, 方便我们的项目构件部署, 项目的依赖开发等. 还在等什么, 创建你自己的仓库吧

settings.xml 中配置远程仓库

Pom.xml的作用范围是一个项目,一个公司不可能只做一个项目,那么为了避免重复配置,那么我们需要把一些公共信息配置在 setting.xml中。但是setting.xml中并不支持<repositories> 及<pluginRepositories>,为了解决这个问题我们使用profile:

  1. <settings>   
  2.   …   
  3.   <profiles>   
  4.     <profile>   
  5.       <id> myProfiel</id>   
  6.       <!—在这里加入<repositories>及<pluginRepositories>–>  
  7.     </profile>   
  8.   </profiles>   
  9.   <activeProfiles>   
  10.     <activeProfile> myProfiel </activeProfile>   
  11.   </activeProfiles>   
  12.   …   
  13. </settings>   

这里通过<activeProfile>元素来激活这个profile,这样我们就可以全局的使用这个配置,不再需要为每个POM做重复的配置了。

在实际的操作过程中,这里我们最好不要配置远程仓库,最好能够通过nexus建立公司或者组织自己的仓库,然后这把把地址指向自己的仓库,后面我会介绍为什么要这么做,怎么做。

配置镜像

 

如果你想覆盖中央仓库的默认地址,那么这里我们就会使用的镜像了,还在setting.xml里面配置:

  1. <settings>   
  2. …   
  3.   <mirrors>   
  4.     <mirror>   
  5.       <id> maven-net-cn</id>   
  6.       <name> Maven China Mirror</name>   
  7.       <url> http://maven.net.cn/content/groups/public/</url>   
  8.       <mirrorOf> central</mirrorOf>   
  9.     </mirror>   
  10.   </mirrors>   
  11. …   
  12. </settings>   

这里解释一下<mirrorOf>,表示只为central仓库做镜像,如果想为所有的仓库做镜像那么可以改为:<mirrorOf>*</mirrorOf>

Maven 中使用 Nexus

   到此为止我们介绍了如何安装和使用Nexus以及其基本配置, 下面我们介绍下如何让Maven来使用Nexus本地仓库用来替代使用远程仓库. 在Maven使用Nexus本地仓库只需稍作配置, 在settings.xml中加入以下代码:

  <profiles>

    <profile>

       <id>dev</id>

          <repositories>

              <repository>

                 <id>nexus</id>

                  <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>

                  <releases>

                     <enabled>true</enabled>

                  </releases>

                  <snapshots>

                     <enabled>true</enabled>

                  </snapshots>

               </repository>

             </repositories>           

             <pluginRepositories>

                 <pluginRepository>

                     <id>nexus</id>

                     <url>http://127.0.0.1:8081/nexus/content/groups/public</url>

                     <releases>

                         <enabled>true</enabled>

                     </releases>

                     <snapshots>

                         <enabled>true</enabled>

                     </snapshots>

                 </pluginRepository>

             </pluginRepositories>

         </profile>

     </profiles>

    <activeProfiles>

         <activeProfile>dev</activeProfile>

     </activeProfiles>

  

这里配置了repository和pluginRepository, Maven在使用第三方构件和插件时是分开来配置的,所以如果我们也希望插件的下载也通过我们的本地仓库来下载,那么我们就需要配置pluginRepository.

红色字体部分就是我们之前安装的Nexus的地址, 这个地址可以是你们公司局域网内部的一台仓库服务器.

<releases> <enabled>true</enabled></releases>这个标签的作用是设定是否允许下载 

release版本的载构件, 同样snapshots标签可以设定是否允许下载snapshot版本的构件.

通常,我们不建议下载snapshot版本的构件,因为它是不稳定的版本, 除非你有特殊的需

求.

构件部署

 

有些时候我们需要部署构件到Nexus的3rd party, 比如我们在中央仓库找不到我们需要的构件, 我们可以通过Nexus的UI来上传构件:

点击左边菜单栏的 Repositories, 然后点击右边界面的3rd party, 选择界面下方的Artifact Upload, 这个时候出现以下界面:

上传构件需要两个步骤,一个是定义文件的上传,再就是构件的实体文件.

第一部分定义文件可以是POM文件, 这也是比较推荐的方式, 如果没有pom文件,可以

选择以参数的形式输入.

第二部分是上传构件的实体文件,这里简单说一下Classifier和Extension, 这两个都是选

填相, Classifier用来区别同功能的构件用于不同的场景, 比如这个构件是分别针对JDK14

和JDK15做了2个功能一样的Jar, 这个时候你就需要指定这个构件的Classifier为JDK14

还是JDK15. Extension是指扩展名,如果不提供,那么会自动取这个构件的Packaging Type

作为扩展名, 比如 ear, jar, war 等等. (Packaging Type是在第一步中通过pom文件或者手

工输入得到的)

刚才说了3rd party的部署, 关于releases 和 snapshots的UI部署也是一样的操作过程.

我们之前也讲过, 这里的releases和snapshots是用来部署我们自己的项目构件的, 通过

UI部署是可以,但是不是最高效的, 我们可以通过配置Maven来自动部署我们的项目构

件,这也是我们建立自己的仓库的一个非常重要的原因, 下面就让我们看看如何配置:

首先需要在POM文件中加入以下代码:

  1. <project>   
  2. …   
  3. <distributionManagement>   
  4.   <repository>   
  5.     <id> nexus-releases</id>   
  6.       <name> Nexus Release Repository</name>   
  7.       <url> http://127.0.0.1:8081/nexus/content/repositories/releases/</url>   
  8.   </repository>   
  9.   <snapshotRepository>   
  10.     <id> nexus-snapshots</id>   
  11.     <name> Nexus Snapshot Repository</name>   
  12.     <url> http://127.0.0.1:8081/nexus/content/repositories/snapshots/</url>   
  13.   </snapshotRepository>   
  14. </distributionManagement>   
  15. …   
  16. </project>   

这里配置,让Maven知道当我要发布release版本或者snapshot版本是需要发布到哪个地址.

然后我们需要在setting.xml里面配置一下Nexus的帐号和密码:

  1. <settings>   
  2. …   
  3. <servers>   
  4.   <server>   
  5.     <id> nexus-releases</id>   
  6.     <username> admin</username>   
  7.     <password> admin123</password>   
  8.   </server>   
  9.   <server>   
  10.     <id> nexus-snapshots</id>   
  11.     <username> admin</username>   
  12.     <password> admin123</password>   
  13.   </server>      
  14. </servers>   
  15. …   
  16. </settings>

 到此为止, 我们就可以通过命令mvn deploy或者通过IDE的可视化界面点击deploy来发布我们项目到本地仓库了. 通过这种方式我们可以很方便的进行模块间的依赖开发, 在后面的文章中我会详细介绍如何通过snapshot来让我们的依赖开发变得简单.

小结

本文介绍了Maven仓库, 如何通过Nexus建立自己本地仓库, 通过和远程仓库的比较, 我们知道Nexus给我带来很多方便之处,  方便我们管理, 方便我们的项目构件部署, 项目的依赖开发等. 还在等什么, 创建你自己的仓库吧

猜你喜欢

转载自xuelu.iteye.com/blog/1815148