1、軟件準備
一、apache-maven-3.0-bin.tar.gz 下載地址:
http://www.apache.org/dyn/closer.cgi/maven/binaries/apache-maven-3.0-bin.tar.gz
二、nexus-oss-webapp-1.8.0-bundle.tar.gz 下載地址:
http://nexus.sonatype.org/downloads/
2、maven安裝配置
一、建立須要操做maven的用戶組以及用戶(若是用root用戶安裝不用建立)
Java代碼
- #groupadd configer //建立用戶組
- #useradd -g configer configer //建立用戶並指定用戶組
- #passwd configer //爲用戶分配密碼
二、建立解壓目錄,並將apache-maven-3.0-bin.tar.gz文件解壓到指定目錄
Java代碼
- #cd /opt
- #mkdir maven
- #chown -R configer:configer /opt/maven
- #chmod 755 /opt/maven
- #su -l configer
- #tar -zvxf apache-maven-3.0-bin.tar.gz
二、配置環境變量
Java代碼
- #vi /home/configer/.bash_profile
在文件中添加以下行:
Java代碼
- M2_HOME=/opt/maven/apache-maven-3.0
- export M2_HOME
- PATH=$PATH:$M2_HOME/bin
- export PATH
三、查看版本
Java代碼
- #cd /opt/maven/apache-maven-3.0/bin
- #mvn --version
若是顯示版本信息,應該會在${user}目錄下建立.m2目錄
四、查看.m2目錄
Java代碼
- #cd /home/configer/.m2
- 若是沒有.m2目錄,則能夠手動添加一個
- #mkdir .m2
五、若是須要把maven的repository目錄指定到其餘目錄,則修改maven安裝目錄下conf中的配置文件settings.xml文件
Java代碼
- #vi /opt/maven/apache-maven-3.0/conf/settings.xml
- 將文件中<localRepository>....</localRepository>的註釋打開
- 或者在文件中增長 在這個註釋下增長
- <localRepository>your repository path</localRepository>
2、搭建nexus私服
一、解壓nexus-oss-webapp-1.8.0-bundle.tar.gz文件到指定目錄
Java代碼
- #tar -zvxf nexus-oss-webapp-1.8.0-bundle.tar.gz
二、啓動nexus
Java代碼
- #cd /opt/maven/nexus-oss-webapp-1.8.0/bin/jsw
-
- 選擇本身機器的版本:
-
- #cd linux-x86-32/
- #./nexus start
-
- 重啓:
- #./nexus restart
- 中止:
- #./nexus stop
三、運行nexus
在瀏覽器中輸入:http://localhost:8081/nexus
就能夠看到nexus 的主頁,點擊右上角Log in
默認用戶名和密碼是:admin/admin123
運行後會自動生成一個nexus工做目錄sonatype-work,nexus下載的jar包會存放在
sonatype-work/nexus/storage中
四、配置
1)點擊左側菜單Repositories
分別將右側列表中
Java代碼
- Apache Snapshots
- Codehaus Snapshots
- Maven Central
三個repository 的Download Remote Index 配置改成True,並保存設置,
而後在列表中分別右鍵點擊三個Repository,點擊ReIndex
2)增長新的Repository,有一些比較經常使用jar包在nexus提供的repository中可能找不到,
通常比較經常使用的有
Java代碼
- JBOSS的兩個:
- http://repository.jboss.org/maven2/
- http://repository.jboss.org/nexus/content/repositories/releases/
- SUN的:
- http://download.java.net/maven/2/
- K-INT的:
- http://developer.k-int.com/maven2/
-
- 由於找juel:juel-impl:2.2.1 這個jar包,因此我還添加了一個本身找的:
- http://repository.exoplatform.org/content/groups/public/
添加步驟:
Java代碼
- 點擊Add->Proxy Repository->填寫Repository ID, Repository Name, 以及Remote Storage Location 其餘的默認便可。
3) 將新增的Repository添加到Public Repositories中
在Public Repositories 的Configuration中,將多選Select中的項所有添加到左邊,而後保存。
4) 添加本身的jar包
Java代碼
- 在repository列表中有一個3rd party,也就是第三方jar包,點擊會看到一個Artifact Upload選項卡,點擊後,填寫相應的信息。
- GAV Definition 通常選擇 GAV Parameters
- 而後添加Group:Artifact:Version:Package
- 示例 juel:juel-impl:2.2.1:jar
而後選擇要上傳的jar包,保存便可
5) nexus中設置代理服務器
選擇左側administrator菜單中的Server選項,在右側打開的頁面的中下部,有一個選擇項:Default HTTP Proxy Settings(optional) 將複選框選中,填寫相應的代理服務器信息便可。
6) 編寫本身的settings.xml,文件內容以下:
Java代碼
- <settings>
- <proxies>
- <proxy>
- <id>normal</id>
- <active>true</active>
- <protocol>http</protocol>
- <username>deployment</username>
- <password>deploy</password>
- <host>localhost:8081/nexus</host>
- <port>80</port>
- <nonProxyHosts>localhost:8081/nexus</nonProxyHosts>
- </proxy>
- </proxies>
-
- <mirrors>
- <mirror>
- <!--This is used to direct the public snapshots repo in the
- profile below over to a different nexus group -->
- <id>nexus-public-snapshots</id>
- <mirrorOf>public-snapshots</mirrorOf>
- <url>http://localhost:8081/nexus/content/groups/public-snapshots</url>
- </mirror>
- <mirror>
- <!--This sends everything else to /public -->
- <id>nexus</id>
- <mirrorOf>*</mirrorOf>
- <url>http://localhost:8081/nexus/content/groups/public</url>
- </mirror>
- </mirrors>
-
- <profiles>
- <profile>
- <id>development</id>
- <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>
- <profile>
- <!--this profile will allow snapshots to be searched when activated-->
- <id>public-snapshots</id>
- <repositories>
- <repository>
- <id>public-snapshots</id>
- <url>http://public-snapshots</url>
- <releases><enabled>false</enabled></releases>
- <snapshots><enabled>true</enabled></snapshots>
- </repository>
- </repositories>
- <pluginRepositories>
- <pluginRepository>
- <id>public-snapshots</id>
- <url>http://public-snapshots</url>
- <releases><enabled>false</enabled></releases>
- <snapshots><enabled>true</enabled></snapshots>
- </pluginRepository>
- </pluginRepositories>
- </profile>
- </profiles>
- <activeProfiles>
- <activeProfile>development</activeProfile>
- </activeProfiles>
- </settings>
將settings.xml文件複製到${user}/.m2目錄下
7) 測試
建立一個maven項目
Java代碼
- #mvn archetype:generate
若是建立一個web項目能夠選擇83
填寫相應的信息,maven會從私服上下載相應的jar包,看到的地址應該都是localhost的
項目建立成功後,將本身的pom.xml文件放在項目,執行
Java代碼
- #mvn install 或者#mvn package
mvn會下載pom.xml文件定義的jar依賴 下載完成後,你能夠在本身指定的repository目錄下,若是沒有指定則是在.m2/repository目錄下查看本身的jar包 至此,咱們的maven安裝以及私服的搭建工做就完成了。