nexus3.x maven私庫安裝教程 (親測!)

前言

書接上文?enn…

嘛~ ,上文:nexus 3.x下載 3.18.1(maven 私服)

上面是教如何下載nexus的

嘛,開始吧,有啥闊以聊的我後面再放吧(づ ̄ 3 ̄)づ

在這裏插入圖片描述


一、環境準備

1、一臺服務器 :我的是Centos7雲服務器(可雲,可本地,只不過重點在有無外網)
2、jdk:jdk1.8(老牌)
3、maven:maven3.6.2 (都不陌生吧,版本看着來)
4、nexus:nexus3.18.1 (上面有攻略)


二、nexus環境配置

(1)解壓

tar -xvf nexus-3.18.1-01-unix.tar.gz

./nexus-3.18.1-01 程序home路徑
./sonatype-work 工作目錄,包含緩存信息,日誌,上傳到私服的相關包的信息

在這裏插入圖片描述
(2)配置環境變量

nexus的環境變量最好要配置(關於後面自啓動的配置),至於jdk,maven這些這裏就不過多贅述了

vim /etc/profile 
#和NEXUS_HOME
export NEXUS_HOME=/home/nexus/nexus-3.18.1-01
export PATH=$NEXUS_HOME/bin;
#完成以後使更改後的文件立即生效
source /etc/profile
#檢驗配置是否成功
nexus

在這裏插入圖片描述
(3)啓動服務

使用./nexus run 就是直接放到主線程裏跑

在這裏我們最好就試一試主線程能不能跑通,跑不通的話請看下面的 問題一

使用./nexus start 就是後臺運行

如果主線程跑的沒問題的話,就可以直接起了

(4)設置自啓服務

/etc/rc.local 文件最底下加入行 nexus start

三、瞭解nexus

(1)登錄

默認端口號爲:ip:8081,如果有需要可以到/nexus/sonatype-work/nexus3/etc/nexus.properties ,進行修改端口即可

在這裏插入圖片描述

首次登錄的話是需要到它提示的文件裏找初始密碼的

在這裏插入圖片描述

(2)基本倉庫

  • maven-releases (Version policy=Release)默認只允許上傳不帶SNAPSHOT版本尾綴的包,默認部署策略是Disable redeploy
    不允許重複上傳相同版本號信息的jar,避免包版本更新以後使用方無法獲取到最新的包。
  • maven-snapshots (Versionpolicy=Snapshot)只允許上傳帶SNAPSHOT版本尾綴的包,默認部署策略是Allow
  • redeploy,允許重複上傳相同版本號信息的jar,每次上傳的時候會在jar的版本號上面增加時間後綴信息。 maven-central
    中央倉庫的拷貝,如果環境可以訪問中央倉庫,則可以獲取到相關的包,否則沒用
  • maven-public
    倉庫組,不是實際個一個倉庫地址,只是將現有的組合到一次,可以通過它看到所屬組內全部倉庫的jar信息

在這裏插入圖片描述

我得說一下,我這裏並不管其他角色和權限倉庫之類的,不過如有需要,可以參考後面的博客。


四、上傳naxus

(1)項目打包

setting.xml需要配置:

<servers>
		<server>
			<id>maven-snapshots</id>
			<username>賬號名</username>
			<password>賬號密碼</password>
		</server>
		<server>
			<id>maven-releases</id>
			<username>賬號名</username>
			<password>賬號密碼</password>
		</server>
	</servers>

在這裏插入圖片描述

pom.xml需要配置:

<!--    發佈項目的時候有用 deploy-->
    <distributionManagement>
        <snapshotRepository>
            <id>maven-snapshots</id>
            <url>http://www.javawwl.com:9081/repository/maven-snapshots/</url>
        </snapshotRepository>
        <repository>
            <id>maven-releases</id>
            <url>http://www.javawwl.com:9081/repository/maven-releases/</url>
        </repository>
    </distributionManagement>

就可以直接上傳了,記得要配置好maven

在這裏插入圖片描述

在這裏插入圖片描述

(2)window本地maven倉庫批量上傳

在本地maven倉庫下新建一個 mavenimport.sh
在這裏插入圖片描述

文件內容:

#!/bin/bash
# copy and run this script to the root of the repository directory containing files
# this script attempts to exclude uploading itself explicitly so the script name is important
# Get command line params

while getopts ":r:u:p:" opt; do
	case $opt in
		r) REPO_URL="$OPTARG"
		;;
		u) USERNAME="$OPTARG"
		;;
		p) PASSWORD="$OPTARG"
		;;
	esac
done

find . -type f -not -path './mavenimport\.sh*' -not -path '*/\.*' -not -path '*/\^archetype\-catalog\.xml*' -not -path '*/\^maven\-metadata\-local*\.xml' -not -path '*/\^maven\-metadata\-deployment*\.xml' | sed "s|^\./||" | xargs -I '{}' curl -u "$USERNAME:$PASSWORD" -X PUT -v -T {} ${REPO_URL}/{} ;

打開此文件的cmd,運行命令:

mavenimport.sh -u 用戶名 -p 密碼 -r http://ip:端口/repository/maven-releases/

在這裏插入圖片描述

五、使用nexus

就像之前一樣配置阿里的鏡像,配置自己的鏡像就行了。不過也有兩種方式:

(1)第一種: 設置鏡像倉庫
setting.xml內

 <!-- 鏡像倉庫,將releases snapshots thirdparty的jar同步到一起-->
	<mirrors>
		<mirror>
			<!--This sends everything else to /public -->
			<id>maven-releases</id>
			<name> Maven Hundusn Yuntai Mirror(zjrc)</name>
			<mirrorOf>*</mirrorOf>
			<url>http://www.javawwl.com:9081/repository/maven-releases/</url>
		</mirror>   
	</mirrors>

(2)第二種: 配置全局pom.xml倉庫地址
setting.xml內

<profiles>
	<profile>  
	    <id>jdk-1.8</id>  
	    <activation>  
			<activeByDefault>true</activeByDefault>  
			<jdk>1.8</jdk>  
		</activation>  
		<properties>  
			<maven.compiler.source>1.8</maven.compiler.source>  
			<maven.compiler.target>1.8</maven.compiler.target>  
			<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>  
		</properties>  
	</profile> 

   <!-- maven開發庫 -->
   <profile>
       <id>dev</id>
          <repositories>

			<repository>
                 <id>maven-releases</id>
                 <url>http://www.javawwl.com:9081/repository/maven-releases/</url>
                 <releases>
                    <enabled>true</enabled>
					<updatePolicy>always</updatePolicy> 
                 </releases>
                 <snapshots>
                    <enabled>false</enabled>
                 </snapshots>
              </repository>
			  <repository>
                 <id>maven-snapshots</id>
				 <url>http://www.javawwl.com:9081/repository/maven-snapshots/</url>
                 <releases>
                    <enabled>false</enabled>
                 </releases>
                 <snapshots>
                    <enabled>true</enabled>
					<updatePolicy>always</updatePolicy> 
                 </snapshots>
              </repository>
			</repositories>
        </profile>
  </profiles>
	<activeProfiles>
        <activeProfile>dev</activeProfile>
    </activeProfiles>

(3)講解一下

(3)參考setting.xml (大佬的)

<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!--
 | This is the configuration file for Maven. It can be specified at two levels:
 |
 |  1. User Level. This settings.xml file provides configuration for a single user, 
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -s /path/to/user/settings.xml
 |
 |  2. Global Level. This settings.xml file provides configuration for all Maven
 |                 users on a machine (assuming they're all using the same Maven
 |                 installation). It's normally provided in 
 |                 ${maven.home}/conf/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -gs /path/to/global/settings.xml
 |
 | The sections in this sample file are intended to give you a running start at
 | getting the most out of your Maven installation. Where appropriate, the default
 | values (values used when the setting is not specified) are provided.
 |
 |-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
		  <!-- 
  <localRepository>/home/maven/repository</localRepository> 
  -->
  <localRepository>D:\AboutWork\maven\repository</localRepository>
  
  <!--Maven是否需要和用戶交互以獲得輸入。如果Maven需要和用戶交互以獲得輸入,則設置成true,反之則應爲false。默認爲true。 -->  
  <interactiveMode>true</interactiveMode>  
  
   <!--Maven是否需要使用plugin-registry.xml文件來管理插件版本。  -->  
   <!--如果設置爲true,則在{user.home}/.m2下需要有一個plugin-registry.xml來對plugin的版本進行管理  -->  
   <!--默認爲false。 -->  
   <usePluginRegistry>false</usePluginRegistry> 
   
   <!--表示Maven是否需要在離線模式下運行。如果構建系統需要在離線模式下運行,則爲true,默認爲false。  -->  
   <!--當由於網絡設置原因或者安全因素,構建服務器不能連接遠程倉庫的時候,該配置就十分有用。  -->  
   <offline>false</offline>  
   
   <!--當插件的組織Id(groupId)沒有顯式提供時,供搜尋插件組織Id(groupId)的列表。  -->  
   <!--該元素包含一個pluginGroup元素列表,每個子元素包含了一個組織Id(groupId)。  -->  
   <!--當我們使用某個插件,並且沒有在命令行爲其提供組織Id(groupId)的時候,Maven就會使用該列表。  -->  
   <!--默認情況下該列表包含了org.apache.maven.plugins和 org.codehaus.mojo -->  
   <pluginGroups>
		<pluginGroup>org.mortbay.jetty</pluginGroup>
		<pluginGroup>org.codehaus.cargo</pluginGroup>
		<pluginGroup>com.hundsun.scm.maven.plugins</pluginGroup> 
		<pluginGroup>org.apache.maven.plugins</pluginGroup> 
		<pluginGroup>org.codehaus.mojo</pluginGroup>
   </pluginGroups>


  <!--用來配置不同的代理,多代理profiles可以應對筆記本或移動設備的工作環境:通過簡單的設置profile id就可以很容易的更換整個代理配置。  -->  
    <proxies>  
            
        <!--代理元素包含配置代理時需要的信息
        <proxy>  
                
            代理的唯一定義符,用來區分不同的代理元素。 
            <id>myproxy</id>  
                
            該代理是否是激活的那個。true則激活代理。當我們聲明瞭一組代理,而某個時候只需要激活一個代理的時候,該元素就可以派上用處。
            <active>true</active>  
                
            代理的協議。 協議://主機名:端口,分隔成離散的元素以方便配置。
            <protocol>http://…</protocol>  
                
            代理的主機名。協議://主機名:端口,分隔成離散的元素以方便配置。
            <host>proxy.somewhere.com</host>  
                
            代理的端口。協議://主機名:端口,分隔成離散的元素以方便配置。 
            <port>8080</port>  
                
            代理的用戶名,用戶名和密碼錶示代理服務器認證的登錄名和密碼。 
            <username>proxyuser</username>  
                
           代理的密碼,用戶名和密碼錶示代理服務器認證的登錄名和密碼。
            <password>somepassword</password>  
                
            不該被代理的主機名列表。該列表的分隔符由代理服務器指定;例子中使用了豎線分隔符,使用逗號分隔也很常見。
            <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>  
                
        </proxy>  
           -->
    </proxies>  

<!-- 	<server> -->
<!-- 		<id>nexus</id> -->
<!-- 		<username>readOnly</username> -->
<!-- 		<password>hscmreadonly</password> -->
<!-- 	</server> -->
 <!-- Another sample, using keys to authenticate.
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
    
	<servers>
		<server>
			<id>maven-snapshots</id>
			<username>username</username>
			<password>password</password>
		</server>
		<server>
			<id>maven-releases</id>
			<username>username</username>
			<password>password</password>
		</server>
	</servers>
    <!-- maven 默認的中央倉庫 -->
    <!--
	<repositories>
		<repository>
			<id> central</id>
			<name> Maven Repository Switchboard</name>
			<layout> default</layout>
			<url> http://repo1.maven.org/maven2</url>
			<snapshots>
				<enabled> false</enabled>
			</snapshots>
		</repository>
	</repositories> 
    -->
   
   <!-- 鏡像倉庫,將releases snapshots thirdparty的jar同步到一起-->
	<mirrors>
		<mirror>
			<!--This sends everything else to /public -->
			<id>public</id>
			<name> Maven Hundusn Yuntai Mirror(zjrc)</name>
			<mirrorOf>*</mirrorOf>
			<url>http://nexusIp:8081/repository/maven-public/</url>
		</mirror>   
	</mirrors>
	

    
  <!-- profiles
   | This is a list of profiles which can be activated in a variety of ways, and which can modify
   | the build process. Profiles provided in the settings.xml are intended to provide local machine-
   | specific paths and repository locations which allow the build to work in the local environment.
   |
   | For example, if you have an integration testing plugin - like cactus - that needs to know where
   | your Tomcat instance is installed, you can provide a variable here such that the variable is 
   | dereferenced during the build process to configure the cactus plugin.
   |
   | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
   | section of this document (settings.xml) - will be discussed later. Another way essentially
   | relies on the detection of a system property, either matching a particular value for the property,
   | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a 
   | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
   | Finally, the list of active profiles can be specified directly from the command line.
   |
   | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
   |       repositories, plugin repositories, and free-form properties to be used as configuration
   |       variables for plugins in the POM.
   |
   |-->
  <profiles>
	<profile>  
	    <id>jdk-1.8</id>  
	    <activation>  
			<activeByDefault>true</activeByDefault>  
			<jdk>1.8</jdk>  
		</activation>  
		<properties>  
			<maven.compiler.source>1.8</maven.compiler.source>  
			<maven.compiler.target>1.8</maven.compiler.target>  
			<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>  
		</properties>  
	</profile> 

   <!-- maven開發庫 -->
   <profile>
       <id>dev</id>
          <repositories>

			<repository>
                 <id>maven-releases</id>
                 <url>http://nexusIp:8081/repository/maven-releases/</url>
                 <releases>
                    <enabled>true</enabled>
					<updatePolicy>always</updatePolicy> 
                 </releases>
                 <snapshots>
                    <enabled>false</enabled>
                 </snapshots>
              </repository>
			  <repository>
                 <id>maven-snapshots</id>
				 <url>http://nexusIp:8081/repository/maven-snapshots/</url>
                 <releases>
                    <enabled>false</enabled>
                 </releases>
                 <snapshots>
                    <enabled>true</enabled>
					<updatePolicy>always</updatePolicy> 
                 </snapshots>
              </repository>
			</repositories>
        </profile>
  </profiles>
	<activeProfiles>
        <activeProfile>dev</activeProfile>
    </activeProfiles>
  <!-- activeProfiles
   | List of profiles that are active for all builds.
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  -->
</settings>


然後就可以用了,
在這裏插入圖片描述

六、問題

1、問題一

nexus起不來。。。
我就是遇到過這個問題滴。
就是所謂交換區不夠:

下面是我解決之後的圖片,在沒解決之前還全都是0。
在這裏插入圖片描述

按照步驟來就行

執行命令 free 查看內存是不是還有 最主要的是 看有沒有交換空間 swap (這很重要)如果沒有交換空間 或者交換空間比較小 要先安裝交換空間 或者增大空間

(1)、創建swapfile:
root權限下,創建swapfile

dd  if=/dev/zero  of=swapfile  bs=1024  count=10000000  

(有時會遇到dd命令不識別 可能是你安裝過一次了 沒事 先把swapfile刪除就ok了)

(2)、將swapfile設置爲swap空間

mkswap swapfile

(3)、啓用交換空間,這個操作有點類似於mount操作(個人理解):

 swapon  swapfile 

(刪除交換空間 swapoff swapfile)
至此增加交換空間的操作結束了,可以使用free命令查看swap空間大小是否發生變化;

後言

如果還有其他需求的,比我這個詳細的多: Linux Nexus Repository Manager OSS 3.18.1-01 搭建指南

畢竟我這個只是簡單的教你如何上傳依賴到私庫,又如何使用罷了。

不過話說(○` 3′○),像這種東西感覺只適合那些大公司來使用(畢竟小公司也不會寫那種自研的源碼)
又或者是嫌下載依賴的速度還是不過快的人。

公司內部如果裝了一個私庫的話,把當前正在開發的項目所需依賴全放進去,那麼如果有新成員來了,連接下載依賴肯定就快很多了。。。。

我的雲服務器只是最辣雞的一種(一核兩G)
寬帶只有1M,enn…但是感覺下載一下2,3M的依賴包還是沒多大問題滴。

我看了下5M寬帶的服務器,哇(っ °Д °;)っ,感覺一定很快吧。

在最後再附上嬌小的服務器照:
服務器:“awsl” 。
在這裏插入圖片描述

有其他了解私庫的用途的小夥伴也可以跟我講一下咯,謝謝

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章