搭建maven私庫

私服介紹

私服是指私有服務器,是架設在局域網的一種特殊的遠程倉庫,目的是代理遠程倉庫及部署第三方構建。有了私服之後,當 Maven 需要下載構件時,直接請求私服,私服上存在則下載到本地倉庫;否則,私服請求外部的遠程倉庫,將構件下載到私服,再提供給本地倉庫下載。
搭建maven私庫

Nexus的安裝

下載地址:https://www.sonatype.com/download-oss-sonatype
搭建maven私庫
搭建maven私庫

然後下載相應的版本,我們這裏使用的centos所以下載
Nexus Repository Manager OSS 3.x - Unix請添加鏈接描述

提前在服務器上安裝jdk環境(參考:安裝配置java http://blog.51cto.com/4534309/2155016

[root@bogon ~]# java -version
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

建議在linux新建用戶nexus(名字隨便)

[root@bogon ~]# useradd nexus
[root@bogon ~]# passwd nexus
(centos7 默認創建組)
[root@bogon ~]# su - nexus
[nexus@bogon nexus]$mkdir nexus
[nexus@bogon ~]$ rz -y 
[nexus@bogon ~]$ ls -lrt
total 119236
-rw-r--r--. 1 nexus nexus 122093706 Jan 13 22:06 nexus-3.15.0-01-unix.tar.gz
[nexus@bogon ~]$ tar -zxvf nexus-3.15.0-01-unix.tar.gz -C ./nexus && cd nexus
[nexus@bogon nexus]$ vim nexus-3.15.0-01/etc/nexus-default.properties (修改端口號,這裏面修改的是12306)
[nexus@bogon bin]$ cat nexus.vmoptions 
-Xms1200M
-Xmx1200M
-XX:MaxDirectMemorySize=2G
-XX:+UnlockDiagnosticVMOptions
-XX:+UnsyncloadClass
-XX:+LogVMOutput
-XX:LogFile=../sonatype-work/nexus3/log/jvm.log
-XX:-OmitStackTraceInFastThrow
-Djava.net.preferIPv4Stack=true
-Dkaraf.home=.
-Dkaraf.base=.
-Dkaraf.etc=etc/karaf
-Djava.util.logging.config.file=etc/karaf/java.util.logging.properties
-Dkaraf.data=../sonatype-work/nexus3
-Djava.io.tmpdir=../sonatype-work/nexus3/tmp
-Dkaraf.startLocalConsole=false
[nexus@bogon bin]$ pwd
/home/nexus/nexus/nexus-3.15.0-01/bin
爲了訪問12306端口,這裏簡單粗暴

關閉firewalld
關閉防火牆:
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall開機啓動

查看防火牆
firewall-cmd --state

關閉selinux
getenforce 查看狀態
關閉 selinux  /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced. 激活
#     permissive - SELinux prints warnings instead of enforcing. 打印警告沒有激活
#     disabled - No SELinux policy is loaded.沒有激活
SELINUX=enforcing修改disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
啓動nexus
[nexus@bogon bin]$ ./nexus  start
Starting nexus

啓動成功後用瀏覽器登錄http://ip:12306
我這裏是:http://172.16.30.199:12306/

[nexus@bogon bin]$ lsof -i:12306
COMMAND    PID  USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
java    115095 nexus  734u  IPv4 1793119      0t0  TCP *:12306 (LISTEN)
java    115095 nexus  738u  IPv4 1793121      0t0  TCP bogon:12306->bogon:56961 (ESTABLISHED)
java    115095 nexus  739u  IPv4 1793122      0t0  TCP bogon:12306->bogon:56962 (ESTABLISHED)
java    115095 nexus  740u  IPv4 1793123      0t0  TCP bogon:12306->bogon:56964 (ESTABLISHED)
java    115095 nexus  741u  IPv4 1793125      0t0  TCP bogon:12306->bogon:56965 (ESTABLISHED)
java    115095 nexus  742u  IPv4 1793126      0t0  TCP bogon:12306->bogon:56966 (ESTABLISHED)
java    115095 nexus  743u  IPv4 1793127      0t0  TCP bogon:12306->bogon:56967 (ESTABLISHED)

登錄默認用戶名admin,默認密碼admin123


創建blob存儲
搭建maven私庫
開始創建私庫
Repositories-->create repositories-->maven2(proxy)填寫信息
create repositories-->maven2(hosted)填寫信息
create repositories-->maven2(group)填寫信息


本地配置setting.xml

  <servers>
    <!-- server
     | Specifies the authentication information to use when connecting to a particular server, identified by
     | a unique name within the system (referred to by the 'id' attribute below).
     |
     | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
     |       used together.
     |
    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
    -->
    <server>
      <id>CFCC</id>
      <username>admin</username>
      <password>123456</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> 
   <mirrors>
        <mirror>
        <id>CFCC</id>
        <mirrorOf>*</mirrorOf>
        <name>CFCC</name>
        <url>http://172.16.30.199:12306/repository/CFCC-public</url>
    </mirror>

  </mirrors>

查看一下:
搭建maven私庫
搭建maven私庫

注意下面幾點說明:
1.component name的一些說明:
1)maven-central:maven中央庫,默認從https://repo1.maven.org/maven2/ 拉取jar
2)maven-releases:私庫發行版jar
3)maven-snapshots:私庫快照(調試版本)jar
4)maven-public:倉庫分組,把上面三個倉庫組合在一起對外提供服務,在本地maven基礎配置settings.xml中使用。
2.Nexus默認的倉庫類型有以下四種:
1)group(倉庫組類型):又叫組倉庫,用於方便開發人員自己設定的倉庫;
2)hosted(宿主類型):內部項目的發佈倉庫(內部開發人員,發佈上去存放的倉庫);
3)proxy(代理類型):從遠程中央倉庫中尋找數據的倉庫(可以點擊對應的倉庫的Configuration頁簽下Remote Storage Location屬性的值即被代理的遠程倉庫的路徑);
4)virtual(虛擬類型):虛擬倉庫(這個基本用不到,重點關注上面三個倉庫的使用);
3.Policy(策略):表示該倉庫爲發佈(Release)版本倉庫還是快照(Snapshot)版本倉庫;
4.Public Repositories下的倉庫
1)3rd party: 無法從公共倉庫獲得的第三方發佈版本的構件倉庫,即第三方依賴的倉庫,這個數據通常是由內部人員自行下載之後發佈上去;
2)Apache Snapshots: 用了代理ApacheMaven倉庫快照版本的構件倉庫
3)Central: 用來代理maven中央倉庫中發佈版本構件的倉庫
4)Central M1 shadow: 用於提供中央倉庫中M1格式的發佈版本的構件鏡像倉庫
5)Codehaus Snapshots: 用來代理CodehausMaven 倉庫的快照版本構件的倉庫
6)Releases: 內部的模塊中release模塊的發佈倉庫,用來部署管理內部的發佈版本構件的宿主類型倉庫;release是發佈版本;
7)Snapshots:發佈內部的SNAPSHOT模塊的倉庫,用來部署管理內部的快照版本構件的宿主類型倉庫;snapshots是快照版本,也就是不穩定版本
所以自定義構建的倉庫組代理倉庫的順序爲:Releases,Snapshots,3rd party,Central。也可以使用oschina放到Central前面,下載包會更快。
5.Nexus默認的端口是8081,可以在etc/nexus-default.properties配置中修改。
6.Nexus默認的用戶名密碼是admin/admin123
7.當遇到奇怪問題時,重啓nexus,重啓後web界面要1分鐘左右後才能訪問。
8.Nexus的工作目錄是sonatype-work(路徑一般在nexus同級目錄下)

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