使用nexus3搭建私有倉庫

nexus是廣為人知的搭建maven私有倉庫的工具。
本文記錄nexus在安裝配置過程中的一些筆記。

使用docker安裝nexus

docker-compose.yml配置:

version: '2'

services:
  nexus:
    image: sonatype/nexus3:3.2.0
    ports:
      - 8081:8081
    volumes:
      - /mnt/nexus-data:/nexus-data:Z

第一次啟動時會報錯,提示掛載的目錄寫權(quán)限不足。需要修改一下掛載目錄的所有者:

mkdir nexus-data && chown -R 200 /mnt/nexus-data

參考文檔:
https://github.com/sonatype/docker-nexus3

在maven配置文件內(nèi),設(shè)置私有倉庫的賬號密碼。

有兩個地方可以修改maven配置。

一個是global配置。
mac下:如果是使用brew安裝:/usr/local/Cellar/maven/3.3.9/libexec/conf/settings.xml
centOS下: /usr/local/apache-maven-3.3.9/conf/settings.xml
Intellij IDE:/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/conf/settings.xml

一個是當(dāng)前用戶的配置:~/.m2/settings.xml
該文件默認(rèn)不存在,手動創(chuàng)建后, 會覆蓋global的配置。
創(chuàng)建settings.xml文件,并將代碼拷貝進(jìn)去:

<settings>
  <mirrors>
    <mirror>
      <!--This sends everything else to /public -->
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <url>http://localhost:8081/repository/maven-public/</url>
    </mirror>
  </mirrors>
  <profiles>
    <profile>
      <id>nexus</id>
      <!--Enable snapshots for the built in central repo to direct -->
      <!--all requests to nexus via the mirror -->
      <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>
  </profiles>
  <activeProfiles>
    <!--make the profile active all the time -->
    <activeProfile>nexus</activeProfile>
  </activeProfiles>

<servers>
    <server>
      <id>nexus</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
  </servers>

</settings>

<mirror><url>標(biāo)簽內(nèi)的地址修改成nexus服務(wù)的地址。
<servers>標(biāo)簽內(nèi)填寫nexus服務(wù)的賬號密碼,發(fā)布maven項目到nexus時,需要用到。
<server><id>下id需要跟<mirror><id>一致。

拉取maven項目

當(dāng)拉取maven項目時,流程是:

  1. nexus檢查本地是否存在該項目。
  2. 如果存在, 直接將該項目返回給客戶端。
  3. 如果不存在,從maven官方倉庫中拉取項目,并保存到本地。之后返回給客戶端。

在nexus的web頁面上,可以搜索到之前拉取的項目。

search2.png

發(fā)布項目到nexus倉庫

首先在項目的pom.xml文件內(nèi),指定發(fā)布地址:

    <distributionManagement>
        <repository>
            <id>nexus</id>
            <name>Releases</name>
            <url>http://{your-nexus-ip}/repository/maven-releases</url>
        </repository>
        <snapshotRepository>
            <id>nexus</id>
            <name>Snapshot</name>
            <url>http://{your-nexus-ip}/repository/maven-snapshots</url>
        </snapshotRepository>
    </distributionManagement>

此處注意:release版本要配置到<repository>標(biāo)簽內(nèi)。snapshot版本配置到<snapshotRepository>標(biāo)簽內(nèi)。

使用命令發(fā)布項目:mvn clean deploy
log顯示上傳發(fā)布的地址:

[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ base ---
[INFO] Installing /Users/Franklin/Documents/work/hldh/cloud/cloud-base-repo/pom.xml to /Users/Franklin/.m2/repository/com/hldh/cloud/base/1.0-SNAPSHOT/base-1.0-SNAPSHOT.pom
[INFO] 
[INFO] --- maven-deploy-plugin:2.8.2:deploy (default-deploy) @ base ---
Downloading: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/1.0-SNAPSHOT/maven-metadata.xml
Downloaded: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/1.0-SNAPSHOT/maven-metadata.xml (591 B at 2.7 KB/sec)
Uploading: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/1.0-SNAPSHOT/base-1.0-20170107.083838-2.pom
Uploaded: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/1.0-SNAPSHOT/base-1.0-20170107.083838-2.pom (6 KB at 24.7 KB/sec)
Downloading: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/maven-metadata.xml
Downloaded: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/maven-metadata.xml (276 B at 2.8 KB/sec)
Uploading: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/1.0-SNAPSHOT/maven-metadata.xml
Uploaded: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/1.0-SNAPSHOT/maven-metadata.xml (591 B at 3.0 KB/sec)
Uploading: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/maven-metadata.xml
Uploaded: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/maven-metadata.xml (276 B at 1.1 KB/sec)

說明項目已經(jīng)發(fā)布到nexus上了。
這時候在nexus的web頁面上, 就能search到剛剛發(fā)布的項目了:

search.png

上傳第三方j(luò)ar包到nexus

發(fā)布不帶pom文件的獨立jar包:

mvn deploy:deploy-file -DgroupId=<group-id> \
 -DartifactId=<artifact-id> \
 -Dversion=<version> \
 -Dpackaging=<type-of-packaging> \
 -Dfile=<path-to-file> \
 -DrepositoryId=<id-to-map-on-server-section-of-settings.xml> \
 -Durl=<url-of-the-repository-to-deploy>

-DrepositoryId的值即為在setttings.xml里面配置的server id。

默認(rèn)情況下,maven會自動為jar包創(chuàng)建pom文件,如果只想保留獨立jar包,可以使用參數(shù)關(guān)閉這個特性:
-DgeneratePom=false

發(fā)布帶有pom的jar包

mvn deploy:deploy-file -DpomFile=<path-to-pom> \
 -Dfile=<path-to-file> \
 -DrepositoryId=<id-to-map-on-server-section-of-settings.xml> \
 -Durl=<url-of-the-repository-to-deploy>

參考:
https://maven.apache.org/guides/mini/guide-3rd-party-jars-remote.html

使用nexus常見錯誤分析

  1. Request Entity Too Large
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.984 s
[INFO] Finished at: 2017-01-08T15:20:02+08:00
[INFO] Final Memory: 12M/309M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy-file (default-cli) on project standalone-pom: Failed to deploy artifacts: Could not transfer artifact emay:emayclient:jar:4.3.4 from/to nexus (http://nexus.store.com/repository/maven-releases/): Failed to transfer file: http://nexus.store.com/repository/maven-releases/emay/emayclient/4.3.4/emayclient-4.3.4.jar. Return code is: 413, ReasonPhrase: Request Entity Too Large. -> [Help 1]

原因:nexus服務(wù)使用了nginx做反向代理,jar包的大小超過了nginx所允許的范圍。
解決方法:修改nginx.conf配置,將client_max_body_size設(shè)置為一個較大的值:

server { 
  client_max_body_size 10M; 
  listen 80; server_name localhost; 
  location / { proxy_pass http://127.0.0.1:8000/; } 
}
  1. maven編譯時,報錯:
Failure to find org.jfrog.maven.annomojo:maven-plugin-anno:jar:1.4.0 in http://myrepo:80/artifactory/repo
 was cached in the local repository, resolution will not be reattempted until the update interval of MyRepo has elapsed or updates are forced -> [Help 1]

原因:服務(wù)器之前是使用的官方maven庫拉取依賴,本地已經(jīng)存在jar包,配置了nexus倉庫之后,跟之前本地的jar包產(chǎn)生了沖突。
解決方法:刪除~/.m2/repository目錄下對應(yīng)的jar包。 或者干脆從新download一遍所有jar包。

mvn clean install -U

-U表示強制更新所有依賴

  1. 拉取到本地的第三方庫,只有l(wèi)astUpdated文件,卻不見pom和jar文件:
    本地報錯:
[WARNING] The POM for xxx.jar is missing, no dependency information available

我一直以為是nexus有bug,代理maven中央倉庫時出錯。
最后找了好久,發(fā)現(xiàn)原來是這個庫的groupId改了,而且它還刪除了maven中央倉庫的groupId對應(yīng)的包,maven找不到對應(yīng)的pom和jar包,就只會創(chuàng)建lastUpdated文件。
這次更堅信了nexus的代理機(jī)制是很健壯的,出問題一般都是自己本地的問題。

nexus官方文檔:
http://books.sonatype.com/nexus-book/reference3/maven.html#maven-sect-single-group

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容