1. 程式人生 > 其它 >【Maven】maven私服Nexus安裝以及配置

【Maven】maven私服Nexus安裝以及配置

【Maven】maven私服Nexus安裝以及配置

執行環境:

Centos 7,jdk1.8

一、Nexus服務安裝

1)配置JDK

Nexus執行依賴JDK環境,需求提前在系統中配置jdk。下面 jdk1.8 Linux下載地址:

連結:https://pan.baidu.com/s/17t-S3LDjiFOHFto7tiEmsw 
提取碼:yysm

profile檔案配置:

export JAVA_HOME=/usr/local/src/jdk1.8.0_73
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
2)下載Nexus

Nexus官網地址:https://www.sonatype.com/nexus-repository-oss

選擇需要的下載的版本,

# nexus-3.19.1-01(Windows、Linux、Mac)
連結:https://pan.baidu.com/s/1LPZN_CMh7_SHdT_MnqxRvA 
提取碼:hacg
3)解壓 nexus-3.19.1-01-unix.tar.gz
tar -zxvf nexus-3.19.1-01-unix.tar.gz

解壓後會有兩個檔案,以當前3.19版本為例:

nexus-3.19.1-01 #服務目錄
sonatype-work   #私有庫目錄
4)配置

vim nexus-3.19.1-01/etc/nexus-default.properties

## DO NOT EDIT - CUSTOMIZATIONS BELONG IN $data-dir/etc/nexus.properties
##
# Jetty section
##埠號
application-port=18081 
application-host=0.0.0.0
nexus-args=${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-requestlog.xml
## 私有庫目錄指向,sonatype-work,一般預設不做修改
nexus-context-path=/

# Nexus section
nexus-edition=nexus-pro-edition
nexus-features=\
 nexus-pro-feature

nexus.hazelcast.discovery.isEnabled=true

配置以root使用者啟動,

vim nexus-3.19.1-01/bin/nexus.rc
#run_as_user=""
run_as_user=root
5)啟動
.nexus-3.19.1-01/bin/nexus start #啟動
.nexus-3.19.1-01/bin/nexus stop  #停止
.nexus-3.19.1-01/bin/nexus restart #重啟

二、Nexus 服務的配置

1)訪問服務:http://IP:埠
2)首次登陸,根據提示,查詢admin使用者的密碼,登陸並修改。

cat /data/software/sonatype-work/nexus3/admin.password

3)倉庫說明
(1)預設倉庫說明:
maven-central:maven 中央庫,預設從 https://repo1.maven.org/maven2/ 拉取 jar
maven-releases:私庫發行版 jar,初次安裝請將 Deployment policy 設定為 Allow redeploy
maven-snapshots:私庫快照(除錯版本)jar
maven-public:倉庫分組,把上面三個倉庫組合在一起對外提供服務,在本地 maven 基礎配置 settings.xml 或專案 pom.xml 中使用
(2)倉庫型別說明:
group:這是一個倉庫聚合的概念,使用者倉庫地址選擇 Group 的地址,即可訪問 Group 中配置的,用於方便開發人員自己設定的倉庫。maven-public 就是一個 Group 型別的倉庫,內部設定了多個倉庫,訪問順序取決於配置順序,3.x 預設為 Releases、Snapshots、Central,當然你也可以自己設定。
hosted:私有倉庫,內部專案的釋出倉庫,專門用來儲存我們自己生成的 jar 檔案
snapshots:本地專案的快照倉庫
releases: 本地專案釋出的正式版本
proxy:代理型別,從遠端中央倉庫中尋找資料的倉庫(可以點選對應的倉庫的 Configuration 頁簽下 Remote Storage 屬性的值即被代理的遠端倉庫的路徑),如可配置阿里雲 maven 倉庫
central:中央倉庫
4)新增阿里雲服務

【Create repository】->【 maven2(proxy)】

5)編輯 maven-public

編輯maven-public,並將剛才新增的aliyun-repository的優先順序調高到第一

三、Maven使用私服配置

1)設定 settings.xml全域性共享
<mirrors>
	  <mirror>
          <id>nexus</id>
          <name>nexus maven</name>
		  <mirrorOf>*</mirrorOf>
          <url>http://nexus.local:18081/repository/maven-public/</url>          
      </mirror>
</mirrors>
2)通過 pom.xml 檔案配置,專案獨享模式
<repositories>
    <repository>
        <id>maven-nexus</id>
        <name>maven-nexus</name>
        <url>http://nexus.local:18081/repository/maven-public/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

Maven 配置使用私服(釋出依賴)

1)設定 settings.xml
 <servers>
	<server>
		<id>nexus-releases</id>
		<username>admin</username>
		<password>password</password>
	</server>

	<server>
		<id>nexus-snapshots</id>
		<username>admin</username>
		<password>admin</password>
	</server>
  </servers>
2)設定 pom.xml 檔案中加入 distributionManagement 節點:
	<distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Release Repository</name>
            <url>http://nexus.local:18081/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://nexus.local:18081/repository/maven-snapshots</url>
        </snapshotRepository>
    </distributionManagement>