Sonatype Nexus 學(xué)習(xí)筆記

Nexus 有多個(gè)產(chǎn)品活烙,比如一種安全掃描工具遣鼓,或者谷歌的平板電腦/手機(jī)。

這里加上限定詞 Sonatype宫补,表示這個(gè)Nexus 是二進(jìn)制包管理工具曾我,業(yè)界跟它類似的工具是 Artifactory 。

社區(qū)版是 Nexus repository OSS

Installing and Starting Nexus Repository Manager 3

Ubuntu 20 環(huán)境上測(cè)試贫贝,已經(jīng)安裝 openjdk-8 (當(dāng)前不支持 java11 或者其它版本)

下面是官網(wǎng)的安裝步驟蛉谜,寫的很詳細(xì),就直接抄了客燕。

  1. Create an installation directory in your desired location.
  2. Download the most recent repository manager for your operating system.
  3. If the file is downloaded to a location outside the installation directory, move it there.
  4. Unpack the .tar.gz or .zip file in its new location. Both an application (i.e. nexus-<version>) and data directory (i.e. ../sonatype-work/nexus3) are created after extraction.
  5. Go to the application directory which contains the repository manager file you need to start up.
  6. In the application directory, run the startup script launching the repository manager:
    • Linux: ./bin/nexus run (front end method, for testing)
    • ./bin/nexus start (backend method)
  7. Wait until the log shows the message “Started Sonatype Nexus.”
  8. Open your browser and type http://localhost:8081/ in your URL field.
  9. From the user interface click Sign In, which generates a modal to enter your credentials.
  10. Navigate to ../sonatype-work/nexus3/ in your terminal.
  11. Locate the admin.password file.
  12. Copy the string from the file to the password field, and sign in.
  13. Complete the step-by-step setup modal to update your password and set Anonymous Access defaults upon logging in.

基本結(jié)構(gòu)

Nexus 安裝后發(fā)現(xiàn)內(nèi)置的 maven repos也搓,不用修改涵紊,直接能用。

它有三種 type:proxy颤练,group,hosted 代表著用什么方式獲取代碼嗦玖。跟 Artifactory 對(duì)比:

Nexus Artifactory
proxy remote
group virtual
hosted local

Format 表示支持哪種二進(jìn)制文件,包括(maven2, docker, yum ...)

image-20230406142348673.png

Proxying Maven

  1. 修改 settings.xml 比如 ~/.m2/settings.xml.

    <settings>
        <servers>
            <server>
                <id>nexus</id>
                <username>admin</username>
                <password>admin123</password>
            </server>
        </servers>
    <mirrors>
      <mirror>
      <!--This sends everything else to /public -->
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <url>http://localhost:8081/repository/maven-public/</url>
      </mirror>
    </mirrors>
     
    </settings>
    
  1. 創(chuàng)建 POM file (pom.xml) with the values below:

    <project>
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.example</groupId>
      <artifactId>nexus-proxy</artifactId>
      <version>1.0-SNAPSHOT</version>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.10</version>
        </dependency>
      </dependencies>
    </project>
    
  1. Run the Maven build with the command mvn package.

    $ mvn package
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ----------------------< com.example:nexus-proxy >-----------------------
    [INFO] Building nexus-proxy 1.0-SNAPSHOT
    [INFO]   from pom.xml
    [INFO] --------------------------------[ jar ]---------------------------------
    Downloading from nexus: http://localhost:8081/repository/maven-public/org/apache/maven/plugins/maven-resources-plugin/3.3.0/maven-resources-plugin-3.3.0.pom
    Downloaded from nexus: http://localhost:8081/repository/maven-public/org/apache/maven/plugins/maven-resources-plugin/3.3.0/maven-resources-plugin-3.3.0.pom (8.5 kB at 6.0 kB/s)
    Downloading from nexus: http://localhost:8081/repository/maven-public/org/apache/maven/plugins/maven-plugins/36/maven-plugins-36.pom
    Downloaded from nexus: http://localhost:8081/repository/maven-public/org/apache/maven/plugins/maven-plugins/36/maven-plugins-36.pom (9.9 kB at 22 kB/s)
    ....
    

    You can see downloading from private repo.

  2. 網(wǎng)頁確認(rèn)

image-20230406144301281.png
image-20230406144329477.png

Deploy to Nexus

修改 POM file ,添加下面的內(nèi)容

<project>
....
  <distributionManagement>
    <repository>
        <id>nexus</id>
        <name>maven-releases</name>
        <url>http://localhost:8081/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
        <id>nexus</id>
        <name>maven-snapshots</name>
        <url>http://localhost:8081/repository/maven-snapshots/</url>
    </snapshotRepository>
   </distributionManagement>
</project>

測(cè)試上傳

$ mvn clean deploy
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.example:nexus-proxy >-----------------------
[INFO] Building nexus-proxy 1.0-SNAPSHOT
[INFO]   from pom.xml
....
Uploading to nexus: http://localhost:8081/repository/maven-snapshots/com/example/nexus-proxy/1.0-SNAPSHOT/maven-metadata.xml
Uploaded to nexus: http://localhost:8081/repository/maven-snapshots/com/example/nexus-proxy/1.0-SNAPSHOT/maven-metadata.xml (766 B at 25 kB/s)
Uploading to nexus: http://localhost:8081/repository/maven-snapshots/com/example/nexus-proxy/maven-metadata.xml
Uploaded to nexus: http://localhost:8081/repository/maven-snapshots/com/example/nexus-proxy/maven-metadata.xml (280 B at 10 kB/s)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.832 s
[INFO] Finished at: 2023-04-06T05:39:58+02:00
[INFO] ------------------------------------------------------------------------

從日志看,已經(jīng)上傳了娱局,到網(wǎng)頁上再次確認(rèn)。

image-20230406144852493.png

以上任斋,功能上 Maven 和 Nexus 結(jié)合的基本讀寫已經(jīng)測(cè)試完畢耻涛。

用戶權(quán)限管理

使用 Role-Based Access Controls 來管理權(quán)限,Artifactory 7.X 之后也使用 RBAC澈蟆。
基本操作思路就是創(chuàng)建 role 卓研, 分配權(quán)限, 關(guān)聯(lián)用戶或組寥闪。

假如一個(gè)新組 apollo 磨淌,提交文件到 dev-apollo 來舉例。

創(chuàng)建repo

image-20230406151124681.png
image-20230406151226480.png
image-20230406151305309.png

Creates a new role named team-apollo.

image-20230406151522015.png
  1. The admin adds the following browse, read, and write privileges for that repository to the team-apollo role:
    • nx-repository-view-maven2-dev-apollo-add
    • nx-repository-view-maven2-dev-apollo-read
    • nx-repository-view-maven2-dev-apollo-browse
image-20230406151641719.png

Create local user and assign role

開發(fā)環(huán)境應(yīng)該配置 LDAP,然后分配 LDAP 用戶到 role 喂击。 這里使用本地賬戶來測(cè)試淤翔。

image-20230406152901212.png

網(wǎng)頁驗(yàn)證

用新建賬戶登錄后只能看到一個(gè)repo (期待的結(jié)果)佩谷。

image-20230406153022720.png

Maven 驗(yàn)證

change settings.xml for local user authentication

    <servers>
        <server>
            <id>apollo</id>
            <username>testuser</username>
            <password>testuser</password>
        </server>
        ....
    </servers>

change POM file for distribution.

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>my-artifact</artifactId>
  <version>1.0</version>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
    </dependency>
  </dependencies>
  <distributionManagement>
    <repository>
        <id>apollo</id>
        <name>maven-releases</name>
        <url>http://localhost:8081/repository/dev-apollo/</url>
    </repository>
   </distributionManagement>
</project>

Test deploy

$ mvn deploy
....
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:3.1.0:deploy (default-deploy) on project my-artifact: Failed to deploy artifacts: Could not transfer artifact com.example:my-artifact:pom:1.0 from/to apollo (http://localhost:8081/repository/dev-apollo/): status code: 403, reason phrase: Forbidden (403) -> [Help 1]

報(bào)錯(cuò) Forbidden ,說明權(quán)限不夠裁奇÷竽欤回到之前配置 role 的頁面,添加 nx-repository-view-maven2-dev-apollo-* 免胃,重試,成功了躺涝。

$ mvn deploy
....
[INFO] --- deploy:3.1.0:deploy (default-deploy) @ my-artifact ---
Uploading to apollo: http://localhost:8081/repository/dev-apollo/com/example/my-artifact/1.0/my-artifact-1.0.pom
Uploaded to apollo: http://localhost:8081/repository/dev-apollo/com/example/my-artifact/1.0/my-artifact-1.0.pom (538 B at 6.3 kB/s)
Uploading to apollo: http://localhost:8081/repository/dev-apollo/com/example/my-artifact/1.0/my-artifact-1.0.jar
Uploaded to apollo: http://localhost:8081/repository/dev-apollo/com/example/my-artifact/1.0/my-artifact-1.0.jar (1.3 kB at 45 kB/s)
Downloading from apollo: http://localhost:8081/repository/dev-apollo/com/example/my-artifact/maven-metadata.xml
Uploading to apollo: http://localhost:8081/repository/dev-apollo/com/example/my-artifact/maven-metadata.xml
Uploaded to apollo: http://localhost:8081/repository/dev-apollo/com/example/my-artifact/maven-metadata.xml (298 B at 12 kB/s)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

在網(wǎng)頁驗(yàn)證坚嗜,也成功诗充。

image-20230406155844225.png

refs:

Official document: https://help.sonatype.com/repomanager3

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市银室,隨后出現(xiàn)的幾起案子励翼,更是在濱河造成了極大的恐慌,老刑警劉巖抓狭,帶你破解...
    沈念sama閱讀 217,542評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件造烁,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡苗桂,警方通過查閱死者的電腦和手機(jī)告组,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來便锨,“玉大人,你說我怎么就攤上這事姚建≈ㄑ常” “怎么了?”我有些...
    開封第一講書人閱讀 163,912評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵贩虾,是天一觀的道長(zhǎng)沥阱。 經(jīng)常有香客問我,道長(zhǎng)考杉,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,449評(píng)論 1 293
  • 正文 為了忘掉前任咽袜,我火速辦了婚禮询刹,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘凹联。我一直安慰自己哆档,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評(píng)論 6 392
  • 文/花漫 我一把揭開白布澳淑。 她就那樣靜靜地躺著,像睡著了一般杠巡。 火紅的嫁衣襯著肌膚如雪雇寇。 梳的紋絲不亂的頭發(fā)上绑改,一...
    開封第一講書人閱讀 51,370評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音出革,去河邊找鬼。 笑死骂束,一個(gè)胖子當(dāng)著我的面吹牛成箫,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播蹬昌,決...
    沈念sama閱讀 40,193評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼皂贩,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了明刷?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,074評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤愚争,失蹤者是張志新(化名)和其女友劉穎轰枝,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體狸膏,經(jīng)...
    沈念sama閱讀 45,505評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡添怔,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評(píng)論 3 335
  • 正文 我和宋清朗相戀三年广料,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片韧衣。...
    茶點(diǎn)故事閱讀 39,841評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖畅铭,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情假残,我是刑警寧澤炉擅,帶...
    沈念sama閱讀 35,569評(píng)論 5 345
  • 正文 年R本政府宣布谍失,位于F島的核電站,受9級(jí)特大地震影響快鱼,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜抹竹,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評(píng)論 3 328
  • 文/蒙蒙 一柒莉、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧兢孝,春花似錦、人聲如沸跨蟹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽痢艺。三九已至,卻和暖如春堤舒,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背舌缤。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留陵吸,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,962評(píng)論 2 370
  • 正文 我出身青樓澳厢,卻偏偏與公主長(zhǎng)得像旨指,于是被迫代替她去往敵國(guó)和親喳整。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評(píng)論 2 354