【Maven實戰】倉庫介紹和Nexus的安裝

在Maven中咱們以前進行配置一個項目的依賴時,引用一下jar包,這些jar包第一次都會從一個地方進行下載,這個地方稱爲倉庫,而對於倉庫通常有本地倉庫和中心倉庫之分,可是咱們通常在作項目時會在本身的服務器上配置一個私有倉庫,那麼咱們下面就來建立一個私有倉庫,這裏咱們使用的一個工具叫作Nexus。java

一、首先到http://www.sonatype.org/nexus/下載對應的nexus安裝包:nexus-latest-bundle.zip瀏覽器

二、解壓縮獲得的壓縮包,而後將此壓縮包中的bin目錄(這裏爲:D:\docs\nexus-latest-bundle\nexus-2.5.1-01\bin)配置到path環境變量中:服務器

三、接着打開cmd,輸入nexus:app

這邊會讓咱們執行start、stop、install、uninstall等命令,咱們首先執行install命令:maven

此時就會提示咱們安裝成功了,而且會在系統服務中生成nexus的服務:工具

此時咱們再修改D:\docs\nexus-latest-bundle\nexus-2.5.1-01\bin\jsw\conf,下的wrapper.conf文件,以下:this

1 # Set the JVM executable 
2 # (modify this to absolute path if you need a Java that is not on the OS path)
3 wrapper.java.command=java

 

其中的wrapper.java.command=java修改成本機安裝的JDK路徑:url

1 # Set the JVM executable 
2 # (modify this to absolute path if you need a Java that is not on the OS path)
3 wrapper.java.command=C:\Program Files\Java\jdk1.7.0_17\bin\java

 

而後執行nexus start命令:spa

四、打開瀏覽器,輸入:http://localhost:8081/nexus就能進行訪問了:3d

 五、點擊右上角的login進行登錄:用戶名:admin,密碼:admin123,登錄完成之後就會有不少的命令能夠供咱們進行操做了

 

 這裏咱們注意觀察type屬性中的內容:

  • hosted:表示是內部項目發佈倉庫

  • proxy倉庫:從遠程中央倉庫中尋找數據的倉庫,代理倉庫。

  • group倉庫:組倉庫,方便開發人員用來訪問的倉庫,這裏麪包含了其餘的全部倉庫,這樣咱們引用倉庫時就很是方便了

六、倉庫創建完成以後,咱們就能夠設置項目引用的倉庫了,這裏咱們在user-core項目中引用咱們的group倉庫:

1 <repositories>
2      <repository>
3         <id>group</id>
4         <name>group repository</name>
5         <url>http://localhost:8081/nexus/content/groups/public/</url>    //這裏的url咱們能夠到nexus的控制面板中找到
6         <releases><enabled>true</enabled></releases>
7         <snapshots><enabled>true</enabled></snapshots>
8      </repository>
9   </repositories>

 

配置完成之後,咱們user-aggregation項目中隨便的加入一個jar包的依賴,這裏加入的是commons-io:

1 <dependency>
2             <groupId>commons-io</groupId>
3             <artifactId>commons-io</artifactId>
4             <version>2.4</version>
5         </dependency>

 

這樣咱們能夠看到默認的就會到咱們本身的本地倉庫進行下載了:

有的時候可能不會出現以上的這種狀況,程序可能還會自動的從中央倉庫進行下載,這時咱們若是配置其不讓從中央倉庫下載的話,咱們能夠找到maven中的settings文件,而後找到其中的mirrors節點,爲咱們的中央倉庫配置一下鏡像:

 1  <mirrors>
 2     <!-- mirror
 3      | Specifies a repository mirror site to use instead of a given repository. The repository that
 4      | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
 5      | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
 6      |
 7     <mirror>
 8       <id>mirrorId</id>
 9       <mirrorOf>repositoryId</mirrorOf>
10       <name>Human Readable Name for this Mirror.</name>
11       <url>http://my.repository.com/repo/path</url>
12     </mirror>
13      -->
14      <mirror>
15       <id>central</id>
16       <mirrorOf>central</mirrorOf>
17       <name>Human Readable Name for this Mirror.</name>
18       <url>http://localhost:8081/nexus/content/groups/public/</url>   //爲中央倉庫設置鏡像url,這樣當訪問中央倉庫時不是訪問中央倉庫的地址,而是咱們配置的鏡像地址,也就是咱們本身服務器的地址
19     </mirror>
20   </mirrors>

 

這樣當程序試圖訪問中央倉庫時會自動的轉到咱們本身的倉庫了。

相關文章
相關標籤/搜索