Linux 使用nexus搭建maven私服


一、准備工作

     系統:LINUX
          JDK:已安裝(未安裝詳見jdk安裝教程:http://www.cnblogs.com/muzi1994/p/5818099.html)
          Maven:已安裝(未安裝詳見maven安裝教程:http://www.cnblogs.com/muzi1994/p/6030181.html)
          Nexus: http://www.sonatype.org/nexus/go
                     所有版本下載地址: http://www.sonatype.org/nexus/archived/
          Nexus是一個強大的Maven倉庫管理器,它極大地簡化了自己內部倉庫的維護和外部倉庫的訪問。

二、安裝Nexus

  1.解壓nexus文件

1
[root @centos6  var]# tar -zvxf nexus- 2.12 . 0 - 01 -bundle.tar.gz

  

      注:解壓后有兩個文件夾:

                  nexus-2.12.0-01: 是nexus的核心文件

                  sonatype-work :maven下載jar存放地址

  2.啟動Nexus

1
2
3
4
5
6
[root @centos6  nexus- 2.12 . 0 - 01 ]# ./bin/nexus start
 
- ****************************************
WARNING - NOT RECOMMENDED TO RUN AS ROOT
****************************************
If you insist running as root, then set the environment variable RUN_AS_USER=root before running  this  script.

  默認情況下,不建議以root用戶運行Nexus,可以修改bin/nexus中的配置跳過警告(修改RUN_AS_USER=root)

1
[root @centos6  nexus- 2.12 . 0 - 01 ]# vi bin/nexus

  

     重新啟動Nexus

1
2
3
4
5
6
7
[root @centos6  nexus- 2.12 . 0 - 01 ]# ./bin/nexus start
 
- ****************************************
WARNING - NOT RECOMMENDED TO RUN AS ROOT
****************************************
Starting Nexus OSS...
Started Nexus OSS.

  注:Nexus默認端口8081,如果想修改端口。修改/conf/nexus.properties文件

      

      訪問網址:http://192.168.1.11:8081/nexus/#welcome

      

      點擊右上角的 Log In 按鈕即可登陸了。默認登錄賬號/密碼為: admin/admin123 ,登陸成功后的界面

      

  點擊Repositories,將列表中所有Type為proxy 的項目的 Configuration 中的 Download Remote Indexes 設置為True

      

     將Releases倉庫的Deployment Policy設置為 Allow ReDeploy

      

      當然我們也避免不了會使用到一些第三方的 jar ,而這些jar包也不存在於互聯網上的maven中央倉庫中,這時我們可以手工添加jar 到我們的私服中。
      添加第三方 jar 如下:
      

      填寫完必選字段,點擊Upload Artifact(s)按鈕即可。

  3.配置本地項目引用私服

   自動發布構件到遠程倉庫,在工程pom.xml中添加

1
2
3
4
5
6
7
8
9
10
<distributionManagement>
     <repository>
         <id>releases</id><!--這個ID需要與你的release倉庫的Repository ID一致-->
         <url>http: //192.168.1.11:8081/nexus/content/repositories/releases</url>
     </repository>
     <snapshotRepository>
         <id>snapshots</id><!--這個ID需要與你的snapshots倉庫的Repository ID一致-->
         <url>http: //192.168.1.11:8081/nexus/content/repositories/snapshots</url>
     </snapshotRepository>
</distributionManagement>

  修改本地$MAVEN_HOME\conf目錄下的settings.xml配置文件,添加如下配置

1
2
3
4
5
6
7
8
9
10
11
12
<servers>
     <server>
         <id>releases</id>
         <username>admin</username>
         <password>admin123</password>
     </server>
     <server>
         <id>snapshots</id>
         <username>admin</username>
         <password>admin123</password>
     </server>
</servers>

  在本地工程目錄下執行:

1
mvn deploy

  所部署的包就自動上傳到了nexus安裝目錄下的

      

  4.配置Maven從Nexus下載構件

    在POM中配置Nexus私服,這樣的配置只對當前的Maven項目有效。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!--指定Nexus的構件倉庫-->
<repositories>
     <repository>
         <id> public </id>
         <name>Team Maven Repository</name>
         <url>http: //192.168.1.11:8081/nexus/content/groups/public/</url>
         <releases>
             <enabled> true </enabled>
         </releases>
         <snapshots>
             <enabled> true </enabled>
         </snapshots>
     </repository>
</repositories>
 
<!--指定Nexus的插件倉庫-->
<pluginRepositories>
     <pluginRepository>
         <id> public </id>
         <name>Team Maven Repository</name>
         <url>http: //192.168.1.11:8081/nexus/content/groups/public/</url>
         <releases>
             <enabled> true </enabled>
         </releases>
         <snapshots>
             <enabled> true </enabled>
         </snapshots>
     </pluginRepository>
</pluginRepositories>

  在settings.xml中配置profile元素,這樣就能讓本機所有的Maven項目都使用自己的Maven私服。

1
2
3
4
5
6
7
8
9
10
11
12
13
<properties>
         <repository>
             <id> public </id>
             <name>Team Maven Repository</name>
             <url>http: //192.168.1.11:8081/nexus/content/groups/public/</url>
             <releases>
                 <enabled> true </enabled>
             </releases>
             <layout> default </layout>
             <snapshots>
                 <enabled> true </enabled>
             </snapshots>
         </repository><br></properties>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM