一、 安装配置 Nexus

1.1 下载 nexus

https://www.sonatype.com/download-oss-sonatype

1.2 解压

tar -zxf nexus-3.5.2-01-unix.tar.gz

1.3 进入 bin 目录启动:

./nexus run &

出现如下界面启动成功
-------------------------------------------------
Started Sonatype Nexus OSS 3.5.2-01
-------------------------------------------------

1.4 访问

http://10.211.55.7:8081/ 可以登录

mvn私有仓库搭建 maven私有仓库配置_jetty

默认端口号:8081
默认账号:admin
默认密码:admin123

1.5 配置修改

1.5.1 修改运行 nexus3 所使用的用户

[root@bigdata1 bin]#vi nexus.rc
run_as_user=”root”

1.5.2 改 nexus3 启动所使用的 jdk 版本

[root@bigdata1 bin]#vi nexus
INSTALL4J_JAVA_HOME_OVERRIDE=/data/program/software/java8

1.5.3 修改 nexus3 默认端口

[root@bigdata1 etc]# vi nexus-default.properties
 application-port=8282

1.5.4 修改 nexus3 数据以及相关日志的存储位置

[root@bigdata1 etc]# vi nexus.vmoptions
 -XX:LogFile=./sonatype-work/nexus3/log/jvm.log
 -Dkaraf.data=./sonatype-work/nexus3
 -Djava.io.tmpdir=./sonatype-work/nexus3/tmp

二、 修改 settings.xml 配置,使用 nexus 私有库

<?xml version="1.0" encoding="UTF-8"?>
<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>${user.home}/.m2/repository</localRepository>
<interactiveMode>true</interactiveMode>
<offline>false</offline>
<pluginGroups>
<pluginGroup>org.mortbay.jetty</pluginGroup>
<pluginGroup>org.jenkins-ci.tools</pluginGroup>
</pluginGroups>
<servers>
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>

这 是 Server 的 ID( 不 是 登 录 进 来 的 user) , 与 Maven 想 要 连 接 上 的 repository/mirror 中 的 id 元 素 相 匹 配 。
username,password:这两个元素成对出现,表示连接这个 server 需要验证 username 和 password。

在 nexus 中,默认管理员用户名为 admin,密码为 admin123。这里使用两个服务器配置,分别对应 release 和 snapshot。

<mirrors>
<mirror>
<id>nexus-releases</id>
<mirrorOf>*</mirrorOf>
<url>http://10.211.55.7:8081/repository/maven-public/</url>
</mirror>
<mirror>
<id>nexus-snapshots</id>
<mirrorOf>*</mirrorOf>
<url>http://10.211.55.7:8081/repository/maven-snapshots/</url>
</mirror>
</mirrors>

id,name:唯一的镜像标识和用户友好的镜像名称。id 被用来区分 mirror 元素,并且当连接时候被用来获得相应的证书。
mirrorOf:镜像所包含的仓库的 Id。例如,指向 Maven central 仓库的镜像(http://repo1.maven.org/maven2/),设置这个元素为 central。更多的高级映射例如repo1,repo2 或者*,!inhouse 都是可以的。没必要一定和 mirror 的 id 相匹配。

在这里mirrorOf 项当然应该使用*,以表明是所有仓库都会被镜像到指定的地址。
url:镜像基本的 URL,构建系统将使用这个 URL 来连接仓库。

这里应该添 nexus 仓库的地址,地址可以在 nexus 仓库页面中找到。

<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus-releases</id>
<url>http://nexus-releases</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>nexus-snapshots</id>
<url>http://nexus-snapshots</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus-releases</id>
<url>http://nexus-releases</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
<pluginRepository>
<id>nexus-snapshots</id>
<url>http://nexus-snapshots</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>

profile 项代表 maven 的基本配置。按照 maven 的一贯尿性,很多 xml 的配置项都会有一个配置项的复数形式作为父节点,以保证该配置项可以配置多个。

在 profiles 项中,当然也可以配置多个 profile,不过在这里配一个就够了。

下面介绍 profile 项的各个子节点。
id:用来确定该 profile 的唯一标识。
repositories/repository:用以规定依赖包仓库的相关信息。

在下属节点中,id 就不用多说了;URL 是指仓库地址,这里使用伪造的地址,否则即使设置了 mirror,maven 也有可能会直接从中央仓库下载包;releases 和 snapshots 放在一块说吧,这两个节点下属的 enable 节点用以规定对应的依赖包是否对当前策略有效,假如将 snapshot 的 enable 项设为 disable,则不会下载 snapshot 包。

<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>

用以规定当前启用的配置,将对应 profile 的 ID 加入到这一项即可使 profile 生效。

三、 上传 jar 到 nexus

3.1 第一种方式

mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId=dubbo -Dversion=2.8.4 -Dpackaging=jar
-Dfile=/Users/zhangyong/Documents/software/dubbo-2.8.4.jar -Durl=http://10.211.55.7:8081/repository/maven-releases/ -
DrepositoryId=nexus-releases

# DrepositoryId 和 settings.xml 里配置的 id 一样

3.2 第二种方式

代码的 pom.xml 中直接接入

<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>maven-releases</name>
<url>http://10.211.55.7:8081/repository/maven-releases/</url>
</repository>
</distributionManagement>

 

mvn deploy