Nexus安装与配置

一.安装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

四.在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

猜你喜欢

转载自www.linuxidc.com/Linux/2016-09/135084.htm