【转】Linux安装配置maven3.0以及搭建nexus私服

一、软件准备 

1apache-maven-3.0-bin.tar.gz下载地址:http://www.apache.org/dyn/closer.cgi/maven/binaries/apache-maven-3.0-bin.tar.gz 
2
nexus-oss-webapp-1.8.0-bundle.tar.gz下载地址:http://nexus.sonatype.org/downloads/

二、maven安装配置 
1
、创建需要操作maven的用户组以及用户(如果用root用户安装不用创建) 

1.   #groupadd apaduser  //创建用户组  

2.   #useradd -g apaduser apaduser  //创建用户并指定用户组  

3.   #passwd apaduser  //为用户分配密码  

2、创建解压目录,并将apache-maven-3.0-bin.tar.gz文件解压到指定目录 

1.  #cd /opt  

2.  #mkdir maven  

3.   #chown -R apaduser:apaduser /opt/maven  

4.   #chmod 755 /opt/maven  

5.   #su -l apaduser  

6.  #tar -zvxf apache-maven-3.0-bin.tar.gz

7.  #ln –s apache-maven-3.0 apache-maven  

3、配置环境变量 (因为每种linux系统配置文件不一样,

详细请看http://blog.csdn.net/cuker919/archive/2010/11/17/6014727.aspx

1.  #cd /home/apaduser (“sudo su –“ for root user)

2.  #vi .bash_profile

3.  #. .bash_profile

在文件中添加如下行: 

1.  M2_HOME=/opt/maven/apache-maven  

2.  export M2_HOME

3.  PATH=$PATH:$M2_HOME/bin  

4.  export PATH  

4、查看版本 

1.  #cd /opt/maven/apache-maven/bin  

2.  #mvn –version  

如果显示版本信息,应该会在${user}目录下创建.m2目录 

5、查看.m2目录 

1.  #cd /home/apaduser/.m2  

2.  如果没有.m2目录,则可以手动添加一个  

3.  #mkdir .m2  

6、如果需要把mavenrepository目录指定到其他目录,则修改maven安装目录下conf中的配置文件settings.xml文件 

1.  #vi /opt/maven/apache-maven/conf/settings.xml  

2.  将文件中<localRepository>….</localRepository>的注释打开  

3.  或者在文件中增加 在这个注释下增加  

4.  <localRepository>your repository path</localRepository>  

二、搭建nexus私服 
1
、解压nexus-oss-webapp-1.8.0-bundle.tar.gz文件到指定目录 

1.  #cp nexus-oss-webapp-1.8.0-bundle.tar.gz /usr/local

2.  #tar -zvxf nexus-oss-webapp-1.8.0-bundle.tar.gz

3.  #ln –s nexus-oss-webapp-1.8.0 nexus  

2、启动nexus 

1.  #cd /usr/local/nexus/bin/jsw  

2.    

3.  选择自己机器的版本:  

4.    

5.  #cd linux-x86-64/  

6.  #./nexus start  

7.    

8.  重启:  

9.  #./nexus restart  

10.   停止:  

11.   #./nexus stop  

3、运行nexus 
在浏览器中输入:http://changethislocalhost:8081/nexus 
就可以看到nexus 的主页,点击右上角Log in 
默认用户名和密码是:admin/admin123 
运行后会自动生成一个nexus工作目录sonatype-worknexus下载的jar包会存放在 
sonatype-work/nexus/storage
 

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

4、Nexus仓库

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

               group: 仓库组

               hosted:宿主

              proxy:代理

              virtual:虚拟

            首次登陆nexus后可以看到以下一个仓库组和多个仓库。

图(4)

                       Public Repositories:  仓库组

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

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

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

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

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

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

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

5、配置nexus

5.1、开启远程索引

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

点 击左边Administration菜单下面的Repositories,找到右边仓库列表中的三个仓库Apache Snapshots,Codehaus Snapshots和Maven Central,然后再没有仓库的configuration下把Download Remote Indexes修改为true。如下图

 

 

然后在Apache Snapshots,Codehaus Snapshots和Maven Central这三个仓库上分别右键,选择Repari Index,这样Nexus就会去下载远程的索引文件。

 

 

 

 

这样设置以后, Nexus会自动从远程中央仓库下载索引文件, 为了检验索引文件自动下载是否生效,可以却换到Browse Index

 

 

在左边菜单栏里面有个Artifact Search, 在输入框里面输入你想要搜索的构件名字,比如:maven, 那么查询结果如下:

 

 

true是开启,false是关闭 由于中央仓库内容比较多,因此其索引文件比较大,需要查看下载是否完成了,我们可以单击界面左边导航栏中的 Scheduled Tasks 链接后,就可以看到系统调度的任务其状态为 runing,在说哦因下载完毕之后,该任务就会消失。

Scheduled Tasks 界面:


只有下载了,在search查询才会有东西,不然什么都搜索不到。

5.2建立宿主仓库

新 建公司的内部仓库,步骤为Repositories –> Add –> Hosted Repository,在页面的下半部分输入框中填入Repository ID和Repository Name即可,比如分别填入

zfy 和 zfy repostiory,另外把Deployment Policy设置为Allow Redeploy,点击save就创建完成了。

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

图(6)
 

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

 

 

5.3、创建Nexus仓库组

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

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

 

 

 

 

 

 

 

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

 

 

 

5.4、创建Nexus代理仓库
 

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

图(7)

      以上本人分别创建 了  hosted和group ,没有创建proxy,经maven测试通过。
 
 
6、      settings.xml的配置
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"   
  3.           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.           xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">  
  5.   
  6.   <pluginGroups></pluginGroups>  
  7.   <proxies></proxies>  
  8.   
  9.   <servers>  
  10.       <server>  
  11.       <id>nexus-releases</id>  
  12.       <username>admin</username>  
  13.       <password>admin123</password>  
  14.     </server>  
  15.     <server>  
  16.       <id>nexus-snapshots</id>  
  17.       <username>admin</username>  
  18.       <password>admin123</password>  
  19.     </server>  
  20.   </servers>  
  21.   
  22.   <mirrors>   
  23.     <mirror>   
  24.       <id>nexus-releases</id>   
  25.       <mirrorOf>*</mirrorOf>   
  26.       <url>http://localhost:8081/nexus/content/groups/public</url>   
  27.     </mirror>  
  28.     <mirror>   
  29.       <id>nexus-snapshots</id>   
  30.       <mirrorOf>*</mirrorOf>   
  31.       <url>http://localhost:8081/nexus/content/groups/public-snapshots</url>   
  32.     </mirror>   
  33.   </mirrors>   
  34.    
  35.   <profiles>  
  36.    <profile>  
  37.       <id>nexus</id>  
  38.       <repositories>  
  39.         <repository>  
  40.           <id>nexus-releases</id>  
  41.           <url>http://nexus-releases</url>  
  42.           <releases><enabled>true</enabled></releases>  
  43.           <snapshots><enabled>true</enabled></snapshots>  
  44.         </repository>  
  45.         <repository>  
  46.           <id>nexus-snapshots</id>  
  47.           <url>http://nexus-snapshots</url>  
  48.           <releases><enabled>true</enabled></releases>  
  49.           <snapshots><enabled>true</enabled></snapshots>  
  50.         </repository>  
  51.       </repositories>  
  52.       <pluginRepositories>  
  53.          <pluginRepository>  
  54.                 <id>nexus-releases</id>  
  55.                  <url>http://nexus-releases</url>  
  56.                  <releases><enabled>true</enabled></releases>  
  57.                  <snapshots><enabled>true</enabled></snapshots>  
  58.                </pluginRepository>  
  59.                <pluginRepository>  
  60.                  <id>nexus-snapshots</id>  
  61.                   <url>http://nexus-snapshots</url>  
  62.                 <releases><enabled>true</enabled></releases>  
  63.                  <snapshots><enabled>true</enabled></snapshots>  
  64.              </pluginRepository>  
  65.          </pluginRepositories>  
  66.     </profile>  
  67.   </profiles>  
  68.   
  69.   <activeProfiles>  
  70.       <activeProfile>nexus</activeProfile>  
  71.   </activeProfiles>  
  72.    
  73. </settings>  
  1.    
  1. 7、 maven 项目 的pom.xml配置  
  1.    
  1. <pre class="html" name="code"> <distributionManagement>  
  2.         <!-- 两个ID必须与 setting.xml中的<server><id>nexus-releases</id></server>保持一致-->  
  3.         <repository>  
  4.             <id>nexus-releases</id>  
  5.             <name>Nexus Release Repository</name>  
  6.             <url>http://localhost:8081/nexus/content/repositories/releases</url>  
  7.         </repository>  
  8.         <snapshotRepository>  
  9.             <id>nexus-snapshots</id>  
  10.             <name>Nexus Snapshot Repository</name>  
  11.             <url>http://localhost:8081/nexus/content/repositories/snapshots</url>  
  12.         </snapshotRepository>  
  13.     </distributionManagement></pre><pre class="html" name="code"> </pre><pre class="html" name="code">8、mvn:deploy命令上传包,会发现下载路径必不是中央库的路径,而是nexus里面的仓库组路径。这个说明配置环境成功。</pre><pre class="html" name="code"><img alt="" src="http://img.my.csdn.net/uploads/201301/10/1357787083_3866.png"><img alt="" src="http://img.my.csdn.net/uploads/201301/10/1357787109_8992.png"></pre><pre class="html" name="code"> </pre>  
  14. <pre></pre>  
  15. <pre></pre> 

2)增加新的Repository,有一些比较常用jar包在nexus提供的repository中可能找不到, 
一般比较常用的有 

1.  JBOSS的两个:  

2.  http://repository.jboss.org/maven2/  

3.  http://repository.jboss.org/nexus/content/repositories/releases/  

4.  SUN的:  

5.  http://download.java.net/maven/2/  

6.  K-INT的:  

7.  http://developer.k-int.com/maven2/  

8.    

9.  因为找juel:juel-impl:2.2.1 这个jar包,所以我还添加了一个自己找的:  

10.   http://repository.exoplatform.org/content/groups/public/  

添加步骤: 

点击Add->Proxy Repository->填写Repository ID, Repository Name, 以及Remote Storage Location 其他的默认即可。  

3) 将新增的Repository添加到Public Repositories 
Public Repositories Configuration中,将多选Select中的项全部添加到左边,然后保存。 

4) 添加自己的jar 

1.  repository列表中有一个3rd party,也就是第三方jar包,点击会看到一个Artifact Upload选项卡,点击后,填写相应的信息。  

2.  GAV Definition 一般选择 GAV Parameters  

3.  然后添加Group:Artifact:Version:Package  

4.  示例    juel:juel-impl:2.2.1:jar  

然后选择要上传的jar包,保存即可 

比如我们项目需要如下几个包:

javax.jms:jms:1.1:jar

proxool:proxool:0.9.1:jar

proxool:proxool-cglib:0.9.1:jar

com.lowagie:itextasian:1.0:jar

com.danga.MemCached:memcached:2.0.1:jar


5) nexus
中设置代理服务器 
选择左侧administrator菜单中的Server选项,在右侧打开的页面的中下部,有一个选择项:Default HTTP Proxy Settings(optional) 将前面的复选框选中,填写相应的代理服务器信息即可。 web-proxy.cce.hp.com:8080

6) 编写自己的settings.xml,文件内容如下: 

1.   <settings>  

2.     <proxies>  

3.       <proxy>  

4.         <id>normal</id>  

5.         <active>true</active>  

6.         <protocol>http</protocol>  

7.         <username>deployment</username>  

8.         <password>deploy</password>  

9.         <host>changethislocalhost:8081/nexus</host>  

10.        <port>80</port>  

11.        <nonProxyHosts>changethislocalhost:8081/nexus</nonProxyHosts>  

12.      </proxy>  

13.    </proxies>  

14.    

15.    <mirrors>  

16.      <mirror>  

17.        <!–This is used to direct the public snapshots repo in the   

18.            profile below over to a different nexus group –>  

19.        <id>nexus-public-snapshots</id>  

20.        <mirrorOf>public-snapshots</mirrorOf>  

21.        <url>http://changethislocalhost:8081/nexus/content/groups/public-snapshots</url>  

22.      </mirror>  

23.      <mirror>  

24.        <!–This sends everything else to /public –>  

25.        <id>nexus</id>  

26.        <mirrorOf>*</mirrorOf>  

27.        <url>http://changethislocalhost:8081/nexus/content/groups/public</url>  

28.      </mirror>  

29.    </mirrors>  

30.      

31.    <profiles>  

32.      <profile>  

33.        <id>nexus</id>  

34.        <repositories>  

35.          <repository>  

36.            <id>central</id>  

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

38.            <releases><enabled>true</enabled></releases>  

39.            <snapshots><enabled>true</enabled></snapshots>  

40.          </repository>  

41.        </repositories>  

42.       <pluginRepositories>  

43.          <pluginRepository>  

44.            <id>central</id>  

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

46.            <releases><enabled>true</enabled></releases>  

47.            <snapshots><enabled>true</enabled></snapshots>  

48.          </pluginRepository>  

49.        </pluginRepositories>  

50.      </profile>  

51.      <profile>  

52.        <!–this profile will allow snapshots to be searched when activated–>  

53.        <id>public-snapshots</id>  

54.        <repositories>  

55.          <repository>  

56.            <id>public-snapshots</id>  

57.            <url>http://public-snapshots</url>  

58.            <releases><enabled>false</enabled></releases>  

59.            <snapshots><enabled>true</enabled></snapshots>  

60.          </repository>  

61.        </repositories>  

62.       <pluginRepositories>  

63.          <pluginRepository>  

64.            <id>public-snapshots</id>  

65.            <url>http://public-snapshots</url>  

66.            <releases><enabled>false</enabled></releases>  

67.            <snapshots><enabled>true</enabled></snapshots>  

68.          </pluginRepository>  

69.        </pluginRepositories>  

70.      </profile>  

71.    </profiles>  

72.    <activeProfiles>  

73.      <activeProfile>nexus</activeProfile>  

74.    </activeProfiles>  

75.  </settings>

settings.xml文件复制到${user}/.m2目录下 

7) 测试 
创建一个maven项目 

#mvn archetype:generate  

如果创建一个web项目可以选择83 
填写相应的信息,maven会从私服上下载相应的jar包,看到的地址应该都是changethislocalhost 
项目创建成功后,将自己的pom.xml文件放在项目,执行 

 #mvn install 或者#mvn package   

mvn会下载pom.xml文件定义的jar依赖 

下载完成后,你可以在自己指定的repository目录下, 如果没有指定则是在 .m2/repository目录下查看自己的jar 

至此,我们的maven安装以及私服的搭建工作就完成了。

转自:http://blog.csdn.net/cuker919/article/details/6338039

猜你喜欢

转载自xiaobenbenxiong.iteye.com/blog/2093184