使用nexus搭建maven私服

使用maven的好处就是可以对项目的各种依赖进行统一管理,在pom文件中定以好依赖,就可以从maven*库或者第三方库中下载到本地。但在企业内部使用也会遇到一些问题,每个使用者都需要去下载相应的依赖包或者插件,效率低下,所以搭建企业内部的私服就很有必要。

搭建私服后,所有的依赖就可以从私服下载,私服会自动判定,如果私服库里没有这个资源,则私服会自动去网上下载,如果私服已经包含所需资源,则可以通过内网提供给使用者,大大提高工作效率。

nexus是maven常用的私服,安装使用都还算方便,用于搭建企业内部的maven私服。

安装nexus

1、下载

下 载地址:http://nexus.sonatype.org/downloads/,这里列出了所有版本,挑一个最新版本来使用。Nexus提供了两种 安装方式,一种是内嵌Jetty的bundle,只要你有JRE就能直接运行。第二种方式是WAR,你只须简单的将其发布到web容器中即可使用。

为了方便就直接选用bundle版本。本文下载的是nexus-oss-webapp-1.9.2.3-bundle.tar.gz 。

2、安装

在指定的目录解压下载的文件

tar xvf nexus-oss-webapp-1.9.2.3-bundle.tar.gz

解 压后会看到两个文件夹,分别是nexus-oss-webapp-1.9.2.3和sonatype-work,前者包含了nexus的运行环境和应用程 序,后者包含了你自己的配置和存储构件的地方。nexus-oss-webapp-1.9.2.3/conf/plexus.properties中可以 修改端口信息已经工作区的路径。

3、启动nexus

进 入nexus-oss-webapp-1.9.2.3/bin/jsw/目录,然后根据OS的版本进入相应的目录,在linux下,运行./nexus start即启动了服务,直接运行./nexus会看到提示信息。neuxs默认监听端口是8081,此时在浏览器中运行访问http://nexus- server-ip:8081/nexus应该可以看到neuxs的界面。

使用nexus搭建maven私服

4.配置nexus开启自动启动

以centos为例:

cp /opt/nexus/nexus-2.0.3/bin/nexus /etc/init.d/

打开/etc/init.d/nexus 修改

# Set this to the root of the Nexus installation
NEXUS_HOME="/opt/nexus/nexus-2.0.3"

chkconfig nexus on   即可


如果是ubuntu 

复制过去后运行:update-rc.d nexus defaults

注意配置防火墙规则:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 8081 -j ACCEPT

配置nexus

开启远程索引

新搭建的neuxs环境只是一个空的仓库,需要手动和远程中心库进行同步,nexus默认是关闭远程索引下载,最重要的一件事情就是开启远程索引下载。登陆nexus系统,默认用户名密码为admin/admin123。

点击左边Administration菜单下面的Repositories,找到右边仓库列表中的三个仓库Apache Snapshots,Codehaus Snapshots和Maven Central,然后再没有仓库的configuration下把Download Remote Indexes修改为true。然后在这三个仓库上分别右键,选择Repari Index,这样Nexus就会去下载远程的索引文件。

建立内部仓库

新 建公司的内部仓库,步骤为Repositories –> Add –> Hosted Repository,在页面的下半部分输入框中填入Repository ID和Repository Name即可,比如分别填入myrepo和 my repository,另外把Deployment Policy设置为Allow Redeploy,点击save就创建完成了。

修改neuxs仓库组

Nexus 中仓库组的概念是Maven没有的,在Maven看来,不管你是hosted也好,proxy也好,或者group也好,对我都是一样的,我只管根据 groupId,artifactId,version等信息向你要构件。为了方便Maven的配置,Nexus能够将多个仓库,hosted或者 proxy合并成一个group,这样,Maven只需要依赖于一个group,便能使用所有该group包含的仓库的内容。

neuxs- 1.9.2.3中默认自带了一个名为“Public Repositories”组,点击该组可以对他保护的仓库进行调整,把刚才建立的公司内部仓库加入其中,这样就不需要再在maven中明确指定内部仓库 的地址了。同时创建一个Group ID为public-snapshots、Group Name为Public Snapshots Repositories的组,把Apache Snapshots、Codehaus Snapshots和Snapshots加入其中。

到这里neuxs的安装配置就完成了,下面介绍如何在maven中使用自己的私服。


项目使用maven私服

配置从nexus私服下载构件

maven 安装好默认是没有配置仓库信息,此时maven都默认去远程的中心仓库下载所依赖的构件。既然使用了私服,就需要明确告诉maven去哪里下载构件,可以 在三个地方进行配置,分别是是maven的安装目录下的conf下的settings.xml文件,这个全局配置,再一个是在 userprofile/.m2目录下的settings.xml,如果不存在该文件,可以复制conf下settings.xml,这个是针对当前用户 的,还有一个是在pom.xml中指定,其只对该项目有效,三个文件的优先级是pom.xml大于.m2,.m2大于conf下。

settings.xml的配置如下:

在profiles段增加一个profile


  1. <profile>  
  2.   <id>nexus</id>  
  3.   <repositories>  
  4.     <repository>  
  5.         <id>nexus</id>  
  6.         <name>local private nexus</name>  
  7.         <url>http://192.168.1.68:8081/nexus/content/groups/public</url>  
  8.         <releases><enabled>true</enabled></releases>  
  9.         <snapshots><enabled>false</enabled></snapshots>  
  10.     </repository>  
  11.     <repository>  
  12.         <id>nexus-snapshots</id>  
  13.         <name>local private nexus</name>  
  14.         <url>http://192.168.1.68:8081/nexus/content/groups/public-snapshots</url>  
  15.         <releases><enabled>false</enabled></releases>  
  16.         <snapshots><enabled>true</enabled></snapshots>  
  17.     </repository>  
  18.   </repositories>  
  19.   <pluginRepositories>  
  20.     <pluginRepository>  
  21.         <id>nexus</id>  
  22.         <name>local private nexus</name>  
  23.         <url>http://192.168.1.68:8081/nexus/content/groups/public</url>  
  24.         <releases><enabled>true</enabled></releases>  
  25.         <snapshots><enabled>false</enabled></snapshots>  
  26.     </pluginRepository>  
  27.     <pluginRepository>  
  28.         <id>nexus-snapshots</id>  
  29.         <name>local private nexus</name>  
  30.         <url>http://192.168.1.68:8081/nexus/content/groups/public-snapshots</url>  
  31.         <releases><enabled>false</enabled></releases>  
  32.         <snapshots><enabled>true</enabled></snapshots>  
  33.     </pluginRepository>  
  34.    </pluginRepositories>  
  35. </profile>  

上 述的id,name可以根据自己的喜好来定义,保证多个repository的id不重复即可,主要是url的配置,可以在nexus的 repositories列表中找到对应的url,就是repositories的Repository Path,刚才我们已经在neuxs中定于了仓库组,这里就取对应组的Repository Path信息即可。

另 外,仓库是两种主要构件的家。第一种构件被用作其它构件的依赖。这是*仓库中存储的大部分构件类型。另外一种构件类型是插件。如果不配置 pluginRepositories,那么执行maven动作时,还是会看到去远程中心库下载需要的插件构件,所以这里一定要加上这个。

在settings.xml中指使配置了profile还不行,需要**它,在activeProfiles段,增加如下配置:

<activeProfile>nexus</activeProfile>

此处activeProfile中的内容就上面定义profile的id。

其实也可以不进行上述配置,只在项目的pom.xml里配置即可。配置如下
pom.xml配置如下:


  1. <repositories>  
  2.       <repository>  
  3.           <id>nexus</id>  
  4.           <name>local private nexus</name>  
  5.           <url>http://192.168.1.68:8081/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>local private nexus</name>  
  18.           <url>http://192.168.1.68:8081/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>  

其中id,name都无所谓,关键不能把url弄错。

有了上述配置,当在项目执行maven操作时,如果本地库中没有依赖的构件,maven会去私服下载,如果私服也没有,私服会去远程中心库下载,同时会在私服的本地缓存,这样如果第二个人再要用到这个构件,私服就直接提供下载。

deploy项目到私服

对于企业自身的项目,也可以提交到搭建的nexus私服中,首先需要在项目的pom.xml中配置deploy到哪,配置如下:

  1. <distributionManagement>        
  2. <repository>            
  3. <id>releases</id>             
  4. <name>Nexus Release Repository</name>             
  5. <url>http://10.1.81.199:8081/nexus/content/repositories/releases/</url>       
  6. </repository>       
  7. <snapshotRepository>            
  8. <id>snapshots</id>            
  9. <name>Nexus Snapshot Repository</name>            
  10. <url>http://10.1.81.199:8081/nexus/content/repositories/snapshots/</url>          
  11. </snapshotRepository>   
  12. </distributionManagement>  
这是提交还是会报错,因为没有提交到nexus的权限

在maven的conf目录下,配置setting.xml

  1. <server>  
  2.   <id>releases</id>  
  3.   <username>admin</username>  
  4.   <password>admin123</password>  
  5. </server>  
  6. lt;server>  
  7. <id>snapshots</id>  
  8. <username>admin</username>  
  9. <password>admin123</password>  
  10. </server>  

注意这里的id应该和pom中配置的远程发布管理库的ID所对应才行。用户名和密码都是nexus的。再次deploy即可。

发布的项目带源码发布到nexus

需要配置插件来实现,在pom.xml中增加如下插件
  1. <plugin>  
  2.             <groupId>org.apache.maven.plugins</groupId>  
  3.             <artifactId>maven-source-plugin</artifactId>  
  4.             <executions>  
  5.                 <execution>  
  6.                     <id>attach-sources</id>  
  7.                     <goals>  
  8.                         <goal>jar</goal>  
  9.                     </goals>  
  10.                 </execution>  
  11.             </executions>  
  12.         </plugin>  

deploy时候不进行test

由于我deploy时候,自动进行test,而我的test需要的环境可能已经没有了导致无法deploy
于是可增加参数
deploy -Dmaven.test.skip=true
就可以跳过test环节。我是在eclipse中用的, run---build... 里输入这些就可以了

常见问题:

更新遇到错误:

was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced
去自己的.m2 文件夹下把 xxx.lastUpdated文件全部删掉,重新运行maven,ok!

或者在用maven时加 -U参数,就可以忽略xxx.lastUpdated..


参考:http://www.linuxidc.com/Linux/2012-02/54827.htm

转自:http://blog.csdn.net/happyteafriends/article/details/7451061