標籤:
一、軟體準備
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代碼
- #groupadd configer //建立使用者組
- #useradd -g configer configer //建立使用者並指定使用者組
- #passwd configer //為使用者指派密碼
2、建立解壓目錄,並將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
2、配置環境變數
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
3、查看版本
Java代碼
- #cd /opt/maven/apache-maven-3.0/bin
- #mvn --version
如果顯示版本資訊,應該會在${user}目錄下建立.m2目錄
4、查看.m2目錄
Java代碼
- #cd /home/configer/.m2
- 如果沒有.m2目錄,則可以手動添加一個
- #mkdir .m2
5、如果需要把maven的repository目錄指定到其他目錄,則修改maven安裝目錄下conf中的設定檔settings.xml檔案
Java代碼
- #vi /opt/maven/apache-maven-3.0/conf/settings.xml
- 將檔案中<localRepository>....</localRepository>的注釋開啟
- 或者在檔案中增加 在這個注釋下增加
- <localRepository>your repository path</localRepository>
二、搭建nexus私服
1、解壓nexus-oss-webapp-1.8.0-bundle.tar.gz檔案到指定目錄
Java代碼
- #tar -zvxf nexus-oss-webapp-1.8.0-bundle.tar.gz
2、啟動nexus
Java代碼
- #cd /opt/maven/nexus-oss-webapp-1.8.0/bin/jsw
-
- 選擇自己機器的版本:
-
- #cd linux-x86-32/
- #./nexus start
-
- 重啟:
- #./nexus restart
- 停止:
- #./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代碼
- 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中設定Proxy 伺服器
選擇左側administrator菜單中的Server選項,在右側開啟的頁面的中下部,有一個選擇項:Default HTTP Proxy Settings(optional) 將複選框選中,填寫相應的Proxy 伺服器資訊即可。
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安裝以及私服的搭建工作就完成了。
Linux 安裝配置maven3.0 以及搭建nexus私服