Linux 安裝配置maven3.0 以及搭建nexus私服

來源:互聯網
上載者:User

標籤:

一、軟體準備
1、apache-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使用者安裝不用建立)
Java代碼  
  1. #groupadd configer  //建立使用者組  
  2. #useradd -g configer configer  //建立使用者並指定使用者組  
  3. #passwd configer  //為使用者指派密碼  

2、建立解壓目錄,並將apache-maven-3.0-bin.tar.gz檔案解壓到指定目錄

Java代碼  
  1. #cd /opt  
  2. #mkdir maven  
  3. #chown -R configer:configer /opt/maven  
  4. #chmod 755 /opt/maven  
  5. #su -l configer  
  6. #tar -zvxf apache-maven-3.0-bin.tar.gz  


2、配置環境變數
Java代碼  
  1. #vi /home/configer/.bash_profile  

在檔案中添加如下行:
Java代碼  
  1. M2_HOME=/opt/maven/apache-maven-3.0  
  2. export M2_HOME  
  3. PATH=$PATH:$M2_HOME/bin  
  4. export PATH  


3、查看版本
Java代碼  
  1. #cd /opt/maven/apache-maven-3.0/bin  
  2. #mvn --version  

如果顯示版本資訊,應該會在${user}目錄下建立.m2目錄

4、查看.m2目錄
Java代碼  
  1. #cd /home/configer/.m2  
  2. 如果沒有.m2目錄,則可以手動添加一個  
  3. #mkdir .m2  


5、如果需要把maven的repository目錄指定到其他目錄,則修改maven安裝目錄下conf中的設定檔settings.xml檔案
Java代碼  
  1. #vi /opt/maven/apache-maven-3.0/conf/settings.xml  
  2. 將檔案中<localRepository>....</localRepository>的注釋開啟  
  3. 或者在檔案中增加 在這個注釋下增加  
  4. <localRepository>your repository path</localRepository>  



二、搭建nexus私服
1、解壓nexus-oss-webapp-1.8.0-bundle.tar.gz檔案到指定目錄
Java代碼  
  1. #tar -zvxf nexus-oss-webapp-1.8.0-bundle.tar.gz  


2、啟動nexus
Java代碼  
  1. #cd /opt/maven/nexus-oss-webapp-1.8.0/bin/jsw  
  2.   
  3. 選擇自己機器的版本:  
  4.   
  5. #cd linux-x86-32/  
  6. #./nexus start  
  7.   
  8. 重啟:  
  9. #./nexus restart  
  10. 停止:  
  11. #./nexus stop  


3、運行nexus
在瀏覽器中輸入:http://localhost:8081/nexus
就可以看到nexus 的首頁,點擊右上方Log in
預設使用者名和密碼是:admin/admin123
運行後會自動產生一個nexus工作目錄sonatype-work,nexus下載的jar包會存放在
sonatype-work/nexus/storage中

4、配置
1)點擊左側菜單Repositories
分別將右側列表中
Java代碼  
  1. Apache Snapshots  
  2. Codehaus Snapshots  
  3. Maven Central  

三個repository 的Download Remote Index 配置改為True,並儲存設定,
然後在列表中分別右鍵點擊三個Repository,點擊ReIndex

2)增加新的Repository,有一些比較常用jar包在nexus提供的repository中可能找不到,
一般比較常用的有
Java代碼  
  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/  


添加步驟:
Java代碼  
  1. 點擊Add->Proxy Repository->填寫Repository ID, Repository Name, 以及Remote Storage Location 其他的預設即可。  


3) 將新增的Repository添加到Public Repositories中
在Public Repositories 的Configuration中,將多選Select中的項全部添加到左邊,然後儲存。

4) 添加自己的jar包
Java代碼  
  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包,儲存即可

5) nexus中設定Proxy 伺服器
選擇左側administrator菜單中的Server選項,在右側開啟的頁面的中下部,有一個選擇項:Default HTTP Proxy Settings(optional) 將複選框選中,填寫相應的Proxy 伺服器資訊即可。

6) 編寫自己的settings.xml,檔案內容如下:
Java代碼  
  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>localhost:8081/nexus</host>  
  10.       <port>80</port>  
  11.       <nonProxyHosts>localhost: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://localhost: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://localhost:8081/nexus/content/groups/public</url>  
  28.     </mirror>  
  29.   </mirrors>  
  30.     
  31.   <profiles>  
  32.     <profile>  
  33.       <id>development</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>development</activeProfile>  
  74.   </activeProfiles>  
  75. </settings>  

將settings.xml檔案複製到${user}/.m2目錄下

7) 測試
建立一個maven項目
Java代碼  
  1. #mvn archetype:generate  

如果建立一個web項目可以選擇83
填寫相應的資訊,maven會從私服上下載相應的jar包,看到的地址應該都是localhost的
項目建立成功後,將自己的pom.xml檔案放在項目,執行
Java代碼  
  1. #mvn install 或者#mvn package   

mvn會下載pom.xml檔案定義的jar依賴

下載完成後,你可以在自己指定的repository目錄下,如果沒有指定則是在.m2/repository目錄下查看自己的jar包

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

Linux 安裝配置maven3.0 以及搭建nexus私服

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.