自建maven私服(linux版)


玩一下自己的maven私服(bg)--linux版

說明

1. maven私服只是一個中轉, 不用太在意
2. 本人公司有用到過, 想自己嘗試下
3. 順便記錄一下

環境

* 服務器: linux —— centOS7 64bit    
* 軟件: nexus ——  nexus-3.21.1-01-unix.tar.gz

下載

nexus下載地址

​ 懶人操作直接下載nexus-3.21.1-01-unix.tar.gz

安裝

  • 解壓nexus到指定目錄

    tar -zxvf nexus-3.21.1-01-unix.tar.gz -C /usr/local/

  • 命令cd /usr/local/進到nexus的安裝目錄, 重命nexus的解壓目錄

    mv nexus-3.21.1-01 nexus3.21

  • 命令: vim /etc/profile配置nexus的環境變量

    # nexus set 
    export NEXUS_HOME=/usr/local/nexus3.21
    export PATH=$PATH:NEXUS_HOME/bin
    
  • 命令: :wq 保存修改后, 生效配置

    source /etc/profile

  • 啟動nexus

    // 進入nexus的bin目錄
    cd /usr/local/nexus3.21/bin/
    // 啟動nexus
    ./nexus start  
    
    [root@linux_maven bin]# ./nexus start
    WARNING: ************************************************************
    WARNING: Detected execution as "root" user.  This is NOT recommended!
    WARNING: ************************************************************
    Starting nexus
    
    // 直接./nexus 會顯示出此命令后面可以接的參數, 如:
    [root@linux_maven bin]# ./nexus 
    WARNING: ************************************************************
    WARNING: Detected execution as "root" user.  This is NOT recommended!
    WARNING: ************************************************************
    Usage: ./nexus {start|stop|run|run-redirect|status|restart|force-reload}
    
    // 注意: 官網說的啟動方式是  
    ./nexus run
    
    // 但這種方式會打印很多日志信息, 同時會持續性占用當前的終端界面, 如果想繼續使用終端輸入linux命令, 則需要新開一個界面
    // 而使用
    ./nexus start
    // 方式啟動, 則不會顯示日志信息, 同時可以繼續使用當前的linux終端界面, 不需要重新打開新的界面
    
  • 使用命令: ps -ef | grep nexus 可以確認服務是否啟動

  • 在網頁查看安裝的結果http://192.168.1.126:8081/(這是本人的服務器, 將IP改成你自己的就可以了)

    // 無法訪問的原因
    // 1.你的服務器自身無法訪問
    // 2.你的服務器的防火牆的限制(簡單粗暴處理: 關閉防火牆)
    
    // 查看防火牆的狀態 systemctl status firewalld; 如果是active, 則表示開啟中.
    [root@linux_maven bin]# systemctl status firewalld
    ● firewalld.service - firewalld - dynamic firewall daemon
       Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
       Active: active (running) since 四 2020-03-05 15:37:09 CST; 4h 49min ago
         Docs: man:firewalld(1)
     Main PID: 797 (firewalld)
       CGroup: /system.slice/firewalld.service
               └─797 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
    
    
    // 關閉防火牆 systemctl stop firewalld
    [root@linux_maven bin]# systemctl stop firewalld
    
    // 再查看防火牆的狀態  systemctl status firewalld; inactive表示已經關閉
    ● firewalld.service - firewalld - dynamic firewall daemon
       Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
       Active: inactive (dead) since 四 2020-03-05 20:27:24 CST; 3s ago
         Docs: man:firewalld(1)
      Process: 797 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
     Main PID: 797 (code=exited, status=0/SUCCESS)
    
    
  • nexus網頁登錄

    // 本人的提示的路徑是: /usr/local/sonatype-work/nexus3/admin.password
    // 所以直接使用以下命令: 
    cat /usr/local/sonatype-work/nexus3/admin.password
    18a4f5b5-589a-4614-904d-15e718129fa4
    
    // 將查到的密碼, 直接復制到密碼框即可
    
  • 初始登錄, nexus需要重置密碼

    彈出一個對話框設置是否允許匿名瀏覽、下載倉庫中的包:(建議勾選)

  • 之后, 就可以愉快地玩耍了^^.

服務器配置

  • 修改nexus的訪問端口

    cd /usr/local/nexus3.21/etc
    vim nexus-default.properties
    
    // 默認的端口信息為: 
    application-port=8081 
    application-host=0.0.0.0
    
  • 修改后, 重啟

    [root@linux_maven bin]# cd /usr/local/nexus3.21/bin
    [root@linux_maven bin]# ./nexus restart
    WARNING: ************************************************************
    WARNING: Detected execution as "root" user.  This is NOT recommended!
    WARNING: ************************************************************
    Shutting down nexus
    Stopped.
    Restarting nexus
    

    重啟有點慢, 要等個一兩分鍾

  • 使用命令:ps -ef | grep nexus, 以及: netstat -nltp 可以查看是否已經重啟成功

  • 添加第三方 maven 倉庫的代理配置到 nexus

    以管理員賬號 admin 登陸 nexus,打開 Repositories 配置界面,
    默認情況下 nexus 僅配置了 maven 中心倉庫的代理 maven-central

    maven-public 為所有代理倉庫的自定義匯集,用於控制 nexus 按特定順序分別從配置的第三方倉庫中下載依賴包。

    幾個可能比較好的第三方maven倉庫代理配置:

    具體怎么配置, 請看這

本地maven配置

  • 設置本地maven的settings.xml, 正常情況下配置文件在~/.m2/settings.xml

    <settings>
    
    	<!-- server 這個是配置向maven私服上傳jar包用的 -->
    	<!-- 
    		開始還不明白為什么需要兩個賬號配置, 后來考慮到賬號安全才明白
    
    		releases -- 這個賬號的配置是給管理員用的, 一般是項目經理, 或運維
    					只有這些人才有發布正式版本的權限
    					正常情況下, 開發人員的電腦里是沒有這個賬號配置的
    
    		那開發人員怎么配置?
    
    		snapshots -- 這個賬號配置就是給開發人員用的, 主要上傳一些開發中的, 非穩定版本jar包
    
    		這樣, 通過兩個server配置, 就可以做到開發版本和正式版本一次配置, 分開提交
    	-->
      <servers>
        <server>
          <!-- 正式版本提交的賬號配置 -->
          <!-- 非項目經理/運維, 不需要配置管理員的賬號 -->
          <id>release</id> 
          <username>admin</username> 
          <password>admin123</password>
        </server> 
        <server>
          <!-- 開發(快照)版本提交的賬號配置 -->
          <!-- 如果沒有相關賬號的, 需要管理員去私服進行配置 -->
          <id>snapshots</id> 
          <username>deployment</username> 
          <password>deployment123</password>
        </server>
      </servers>
    
    	<mirrors>
    		<mirror>
    			<!--This sends everything else to /public -->
    			<id>nexus</id>
    			<mirrorOf>*</mirrorOf>
    			<!-- 自己maven私服public倉庫, 集成其它幾個鏡像倉庫 -->
    			<url>http://192.168.1.126: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>https://repo1.maven.org/maven2/</url>
    					<releases><enabled>true</enabled></releases>
    					<snapshots><enabled>true</enabled></snapshots>
    				</repository>
    			</repositories>
    			<pluginRepositories>
    				<pluginRepository>
    					<id>central</id>
    					<!-- 中央倉庫的地址 -->
    					<url>https://repo1.maven.org/maven2/</url>
    					<releases><enabled>true</enabled></releases>
    					<snapshots><enabled>true</enabled></snapshots>
    				</pluginRepository>
    			</pluginRepositories>
    		</profile>
    	</profiles>
    
    	<activeProfiles>
        <!--make the profile active all the time -->
        <!-- 使profile的配置生效 -->
        <activeProfile>nexus</activeProfile>
    	</activeProfiles>
    </settings>
    

詳情請看《Proxying Maven and npm Components》

項目配置

​ 項目的pom.xml文件需要配置如何上傳jar包到私服

<distributionManagement>
    <!-- 設置此模塊打好的包放到私發布服地址 -->
    <repository>
        <!-- 此id要與settings.xml里的server賬號'正式'配置的id一致 -->
        <id>release</id>
        <url>http://192.168.1.126:8081/repository/maven-releases/</url>
    </repository>

    <!-- 設置此模塊打好的包放到私服快照地址 -->
    <snapshotRepository>
    		<!-- 此id要與settings.xml里的server賬號'開發/快照'配置的id一致 -->
        <id>snapshots</id>
        <url>http://192.168.1.126:8081/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>

​ 進入項目的根目錄, 使用命令: mvn deploy 即可將項目打包, 並上傳到maven私服


參考來源

Linux 安裝Nexus

解決: Incorrect username or password

Sonatype Nexus 私服的安裝(Windows 篇)

Nexus Repository Manager OSS 3.x 安裝配置

Nexus Repository Manager - Proxying Maven and npm Quick Start

學習Maven之Maven Surefire Plugin(JUnit篇)


免責聲明!

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



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