歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Nexus安裝與配置

Nexus安裝與配置

日期:2017/2/28 13:49:01   编辑:Linux教程

一.安裝nexus前准備
1.先安裝jdk,maven
vi /etc/profile
在末尾添加

export JAVA_HOME=/opt/jdk1.7
export MAVEN_HOME=/opt/maven-3.3.9
export PATH=$JAVA_HOME/bin:$MAVEN_HOME/bin:$PATH

讓配置生效
source /etc/profile
java -version
mvn --help
2.配好dns,能ping通外網(比如www.baidu.com)才能下載索引,中央庫構件
修改dns
vi /etc/resolv.conf
nameserver 61.128.114.133

二.到http://www.sonatype.com/download-oss-sonatype獲取linux包tar.gz的下載連接,假如已下載nexus-2.11.4-01-bundle.tar.gz到/root
三.安裝和配置

/usr/sbin/groupadd nexus
/usr/sbin/useradd -g nexus nexus
cd ~
tar xf nexus-2.11.4-01-bundle.tar.gz
mv nexus-2.11.4-01 /opt/
mv sonatype-work /home/nexus/
chmod 777 /home/nexus/sonatype-work
chown -R nexus:nexus /home/nexus/sonatype-work
vi /opt/nexus-2.11.4-01/conf/nexus.properties
###主要修改nexus庫的位置
nexus-work=/home/nexus/sonatype-work/nexus

cp /opt/nexus-2.11.4-01/bin/nexus /etc/init.d/
vi /etc/init.d/nexus
###主要修改這兩行
NEXUS_HOME="/opt/nexus-2.11.4-01"
RUN_AS_USER=nexus

chown -R nexus:nexus /opt/nexus-2.11.4-01
###添加到自啟動並查看
chkconfig --add nexus
chkconfig --list|grep nexus
service nexus start
###再clone一個終端,並跟蹤日志,從日志都可以看出,啟動到正常訪問,對於機器性能一般都需要1分鐘左右
tail -f /opt/nexus-2.11.4-01/logs/wrapper.log
###回到之前的終端
 ps -ef|grep nexus
 netstat -npl|grep 8081

三.在浏覽器打開http://192.168.1.194:8081/nexus,點右上角的Log In,輸入admin/admin123(nexus默認的設置就已相當完善,但還是有些配置根據自己的情況要修改)
1.點左側導航repositories,就可以右邊列出已有的默認庫(在左側還有查看日志,定義計劃任務等功能,修改密碼的入口在右上角profile)
2.Nexus提供了三種不同的倉庫:
A.代理倉庫.一個代理倉庫是對遠程倉庫的一個代理。默認情況下,Nexus自帶了如下配置的
Apache Snapshots:這個倉庫包含了來自於Apache軟件基金會的快照版本
Codehaus Snapshots:這個倉庫包含了來自於Codehaus的快照版本
Central Maven Repository:這是中央Maven倉庫(發布版本)
B.宿主倉庫.一個宿主倉庫是由Nexus托管的倉庫.Maven自帶了如下配置的宿主倉庫
3rd Party:這個宿主倉庫應該用來存儲在公共Maven倉庫中找不到的第三方依賴。這種依賴的樣例有:你組織使用的,商業的,私有的類庫如Oracle JDBC驅動
Releases:這個宿主倉庫是你組織公布內部發布版本的地方
Snapshots:這個宿主倉庫是你組織發布內部快照版本的地方
C.虛擬倉庫一個虛擬倉庫作為Maven 1的適配器存在。Nexus自帶了一個central-m1虛擬倉庫
3.在列表隨便點一個倉庫
A.點Configuration,可以查看倉庫的ID,名字,類型等信息
B.其中proxy類型才有browse remote選項.以Central庫為例,它也是我最關心的,點Configuration,Remote Storage Location是遠程倉庫的URL(如果網絡原因不能通,就要修改),Download Remote Indexes設置是否下載索引,在外網環境應將默認值False改為True,修改後再保存.
4.Public Repositories的倉庫類型是group,它就是將幾個倉庫分成為一個組,點Ordered Group Repositories可以看到加入此組的倉庫並排好順序,默認順序為Releases,Snapshots,3rd party,Central,這樣的順序是合理的,並且是絕大多數人的需求.如果我們在maven配置使用這個倉庫,它查找構件就是按這順序查找,前三個都找不到時,就會通過代理Central中央倉庫去下載
5.除了虛擬倉庫Central M1 shadow沒有Browse Index,其它倉庫都有Browse Index和Browse Storage,當nexus不能為maven項目提供構件,這兩個選項的查看是比較重要,Browse Index和Browse Storage正如其名,浏覽索引與浏覽存儲.
6.其中,3rd party和Releases還可以在浏覽器添加構件,點Artifact Upload,GAV Definition可以選GAV Parameters,寫好gav,選好packaging,選好Select Artifacts(s) to Upload,最後點底部的Upload Artifact(s)

四.在maven使用nexus
1.部署構件到nexus
A.部署發行版.
pom.xml片段

<distributionManagement>
    ...
    <repository>
        <id>releases</id>
        <name>Internal Releases</name>
        <url>http://192.168.1.194:8081/nexus/content/repositories/releases</url>
    </repository>
    ...
</distributionManagement>

運行mvn deploy
B.部署快照版.

<distributionManagement>
    ...
    <snapshotRepository>
        <id>Snapshots</id>
        <name>Internal Snapshots</name>
        <url>http://192.168.1.194:8081/nexus/content/repositories/snapshots</url>
    </snapshotRepository>
    ...
</distributionManagement>

運行mvn deploy
C.部署第三方構件
mvn deploy:deploy-file -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.3.0 -Dpackaging=jar -Dfile=ojdbc.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty -DrepositoryId=thirdparty

2.一般maven項目的pom片段

    <pluginRepositories>
        <pluginRepository>
            <id>Nexus Public Plugin</id>
            <name>Nexus Public Plugin</name>
            <url>${nexus.url}/content/groups/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

    <distributionManagement>
        <repository>
            <id>Nexus Releases Repository</id>
            <name>Nexus Releases Repository</name>
            <url>${nexus.url}/content/repositories/releases</url>
        </repository>
        <snapshotRepository>
            <id>Nexus Snapshot Repository</id>
            <name>Nexus Snapshot Repository</name>
            <url>${nexus.url}/content/repositories/snapshots</url>
        </snapshotRepository>
    </distributionManagement>

另:對於已有的maven庫,一樣應拷到/home/nexus/sonatype-work/nexus/storage/central,因為牆,還有本身通過網絡下載也沒下載工具下載快,或者內網不能下載,造成下載索引是一件痛苦的事情.下面是一種方法
1.准備工作
a.到http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22org.apache.maven.indexer%22%20AND%20a%3A%22indexer-cli%22下載indexer-cli
b.到http://repo1.maven.org/maven2/.index/下載nexus-maven-repository-index.gz和nexus-maven-repository-index.properties,並驗證一下md5

2.停止nexus,建立索引,拷貝到相應位置,並啟動nexus
service nexus stop
mv /home/nexus/sonatype-work/nexus/indexer/central-ctx /tmp/

假如下載的三個文件在~/index,建立索引需要幾分鐘
cd ~/index
java -jar indexer-cli-5.1.1.jar -u nexus-maven-repository-index.gz -d indexer
mv indexer /home/nexus/sonatype-work/nexus/indexer/central-ctx
chown -R nexus:nexus /home/nexus/sonatype-work/nexus/indexer/central-ctx

service nexus start

Maven使用入門 http://www.linuxidc.com/Linux/2012-11/74354.htm

Ubuntu 下 搭建Nexus Maven私服中央倉庫 http://www.linuxidc.com/Linux/2016-08/133936.htm

Linux下使用Nexus搭建Maven私服詳解 http://www.linuxidc.com/Linux/2016-08/134630.htm

Linux下使用Nexus搭建Maven私服 http://www.linuxidc.com/Linux/2016-08/134617.htm

Linux下安裝配置Nexus http://www.linuxidc.com/Linux/2016-09/135083.htm

Copyright © Linux教程網 All Rights Reserved