首页 > 技术文章 > 【Maven】maven私服Nexus安装以及配置

wsx2019 2022-03-13 16:58 原文

【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>

推荐阅读