1. 程式人生 > >Nexus私服搭建及其核心功能

Nexus私服搭建及其核心功能

1、私服的使用場景

   1)、公司不能連線公網,可以用一個私服務來統一連線
   2)、公司內部jar 元件的共享

2、nexus下載與安裝

   1)、下載nexus(nexus下載
   2)、解壓並設定環境變數

#解壓
tar -zxvf nexus-2.14.5-02-bundle.tar.gz
#在環境變數當中設定啟動使用者
vim /etc/profile
#新增profile檔案。安全起見不建議使用root使用者,如果使用其它使用者需要加相應許可權
export RUN_AS_USER=root

   3)、進入nexus安裝目錄啟動/停止nexus

#啟動
./bin/nexus start
#停止
./bin/nexus stop

  在瀏覽器輸入IP和埠號啟動後如下圖所示:
啟動
   4)、nexus.conf配置檔案:進入nexus安裝目錄內的conf資料夾,裡面有一個nexus.properties檔案,檔案內容大致如下:

#埠號
application-port=8081
#限定使用的IP
application-host=0.0.0.0
#應用程式目錄
nexus-webapp=${bundleBasedir}/nexus
#context目錄
nexus-webapp-context-path=/nexus
# Nexus倉庫地址
nexus-work=${bundleBasedir}/../sonatype-work/nexus
runtime=${bundleBasedir}/nexus/WEB-INF
storage.diskCache.bufferSize=4096

  5)、我們點選頁面右上角的Log In以管理員的身份登入,使用者名稱和密碼分別預設是admin和admin123
登入
  6)、點選左側選單的Repositories進入頁面,nexus的倉庫如下圖:
在這裡插入圖片描述
  1、3rd party:第三方倉庫;
  2、Apache Snapshots:apache 快照倉庫;
  3、Central: maven 中央倉庫;
  4、Central M1 shadow:M1版本的倉庫,現在用的很少;
  5、Releases:私有釋出版本倉庫;
  6、Snapshots:私有 快照版本倉庫;
  7、Public Repositories:不是一個倉庫,但是通過Public Repositories的地址可以訪問其它除了Apache Snapshots之外的任意一個倉庫。因為在Public Repositories下的Configuration做了配置,如下圖所示:
在這裡插入圖片描述


  7)、我們jar包的下載順序是本地倉庫,本地倉庫沒有去nexus私服,nexus私服沒有才會去遠端倉庫下載,遠端倉庫預設是在Central倉庫的https://repo1.maven.org/maven2/下載,這個地址下載很慢,因為它的伺服器是在國外的,我們可以在這裡改成國內的一些中央倉庫。可以改成阿里雲的地址http://maven.aliyun.com/nexus/content/groups/public/然後點選save即可。如下圖所示:
在這裡插入圖片描述
在這裡插入圖片描述

3、本地專案連線nexus私服

  我們可以直接用Public Repositories的地址在pom.xml檔案中配置。如下圖所示:
在這裡插入圖片描述
1、配置pom.xml檔案(作用範圍僅限該專案)。配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>demo-parent</groupId>
    <artifactId>demo-parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>demo-server</module>
        <module>demo-client</module>
    </modules>

    <repositories>
        <repository>
            <id>nexus-repository</id>
            <name>nexus repository</name>
            <url>http://192.168.43.181:8081/nexus/content/groups/public/</url>
        </repository>
    </repositories>
    <!--依賴fastjson1.2.47版本-->
    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>
    </dependencies>
</project>

2、配置maven的settings.xml檔案(作用於所有用到該配置的專案)。配置如下:

<mirror>        
	<id>nexus-aliyun</id>
	<mirrorOf>*</mirrorOf>
	<name>Nexus aliyun</name>
	<url>http://192.168.43.181:8081/nexus/content/groups/public/</url>
</mirror> 

執行編譯
  我們執行compile命令可以從列印的日誌看出來是從我的nexus私服下載的fastjson包。

4、將專案釋出到nexus

  1、在pom.xml中配置如下:

<!--
	將專案釋出到nexus私服配置
	url:釋出到nexus倉庫的地址
-->
    <distributionManagement>
        <repository>
            <uniqueVersion>false</uniqueVersion>
            <id>releases-respository</id>
            <name>releases-respository</name>
            <url>http://192.168.43.181:8081/nexus/content/repositories/releases/</url>
            <layout>default</layout>
        </repository>

        <snapshotRepository>
            <id>snapshot-respository</id>
            <name>snapshot-respository</name>
            <url>http://192.168.43.181:8081/nexus/content/repositories/snapshots/</url>
        </snapshotRepository>
</distributionManagement>

  2、在setting.xls配置如下:

<mirror>
    	<id>nexus-aliyun</id>
        <mirrorOf>*</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://192.168.43.181:8081/nexus/content/groups/public/</url>
</mirror>


	<server>
		<id>releases-respository</id>
		<username>deployment</username>
		<password>deployment123</password>
    </server>
	<server>
      	<id>snapshot-respository</id>
      	<username>deployment</username>
      	<password>deployment123</password>
    </server>

  3、執行deploy命令,成功如下圖所示:
在這裡插入圖片描述
在這裡插入圖片描述