Maven私服Nexus的搭建和使用(Mac)

1.下載對應的安裝包
https://www.sonatype.com/oss-thank-you-mac-tgz

注意:目前的版本有2.X 和 3.X ,2.X的支持對Maven更友好一點,3.X的支持範圍更廣,支持ruby和docker。如果單純的maven私服,建議使用2.x

2.解壓安裝包,並進入對應的bin目錄下啓動nexus
./nexus start

注意:3.X要求JDK的版本在1.8以上

3.訪問地址,3.x默認是127.0.0.1:8081 2.x默認是127.0.0.1:8081/nexus ,默認的登陸賬戶密碼爲admin/admin123
修改端口或者密碼,在etc下的nexus-default.properties

4.簡單介紹一下Repository
Repository的type屬性有:proxy,hosted,group三種。
proxy:即你可以設置代理,設置了代理之後,在你的nexus中找不到的依賴就會去配置的代理的地址中找
hosted:你可以上傳你自己的項目到這裏面
group:它可以包含前面兩個,是一個聚合體。一般用來給客戶一個訪問nexus的統一地址。
簡單的說,就是你可以上傳私有的項目到hosted,以及配置proxy以獲取第三方的依賴(比如可以配置中央倉庫的地址)。前面兩個都弄好了之後,在通過group聚合給客戶提供統一的訪問地址

5.如何上傳jar包
先設置settings.xml,這裏主要是配置用戶名和密碼,注意這裏的id要和respositoryId對應
<servers>
      <server>
       <id>nexus</id>
       <username>admin</username>
       <password>admin123</password>
    </server>
</servers>

控制檯
mvn deploy:deploy-file -DgroupId=ebay -DartifactId=ebay -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar  -Dfile=/Users/yuliangliang/ebay-0.0.1-SNAPSHOT.jar -Durl=http://localhost:8081/repository/maven-snapshots/ -DrepositoryId=nexus
效果如下
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-deploy-plugin:2.7:deploy-file (default-cli) @ standalone-pom ---
Downloading: http://localhost:8081/repository/maven-snapshots/ebay/ebay/0.0.1-SNAPSHOT/maven-metadata.xml
Uploading: http://localhost:8081/repository/maven-snapshots/ebay/ebay/0.0.1-SNAPSHOT/ebay-0.0.1-20180102.145036-1.jar
Uploaded: http://localhost:8081/repository/maven-snapshots/ebay/ebay/0.0.1-SNAPSHOT/ebay-0.0.1-20180102.145036-1.jar (34194 KB at 22218.2 KB/sec)
Uploading: http://localhost:8081/repository/maven-snapshots/ebay/ebay/0.0.1-SNAPSHOT/ebay-0.0.1-20180102.145036-1.pom
Uploaded: http://localhost:8081/repository/maven-snapshots/ebay/ebay/0.0.1-SNAPSHOT/ebay-0.0.1-20180102.145036-1.pom (390 B at 5.5 KB/sec)
Downloading: http://localhost:8081/repository/maven-snapshots/ebay/ebay/maven-metadata.xml
Uploading: http://localhost:8081/repository/maven-snapshots/ebay/ebay/0.0.1-SNAPSHOT/maven-metadata.xml
Uploaded: http://localhost:8081/repository/maven-snapshots/ebay/ebay/0.0.1-SNAPSHOT/maven-metadata.xml (758 B at 10.4 KB/sec)
Uploading: http://localhost:8081/repository/maven-snapshots/ebay/ebay/maven-metadata.xml
Uploaded: http://localhost:8081/repository/maven-snapshots/ebay/ebay/maven-metadata.xml (268 B at 4.0 KB/sec)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.567 s
[INFO] Finished at: 2018-01-02T22:50:38+08:00
[INFO] Final Memory: 8M/155M
[INFO] ------------------------------------------------------------------------

也可以同步本地倉庫到私服上去


6.項目中的使用,這樣就能在項目中引用了,具體的引用可以在pom中配置,也可以在maven的settings.xml文件中修改
<mirrors>
    <mirror>
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <url>http://127.0.0.1:8082/nexus/content/groups/public/</url>
    </mirror>
  </mirrors>
<profiles>
   <profile>
      <id>nexus</id>
      <repositories>
        <repository>
          <id>nexus</id>
          <url>http://127.0.0.1:8082/nexus/content/groups/public/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>  
      </repositories>
     <pluginRepositories>
        <pluginRepository>
          <id>nexus</id>
          <url>http://127.0.0.1:8082/nexus/content/groups/public/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>nexus</activeProfile>
  </activeProfiles>

 7.上傳項目到私服上去
先設置settings.xml,配置servers,見步驟5
項目的pom文件添加如下代碼
<distributionManagement>
 <repository>
   <id>release</id>
   <name>Release Repository</name>
   <url>http://ip/nexus/content/repositories/releases</url>
 </repository>
 <snapshotRepository>
   <id>snapshot</id>
   <name>Snapshot Repository</name>
   <url>http://ip/nexus/content/repositories/snapshots</url>
 </snapshotRepository>
</distributionManagement>



執行deploy -e即可

發佈了36 篇原創文章 · 獲贊 11 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章