maven私有仓nexus搭建

关于nexus

nexus,全名sonatype nexus,是用于搭建私有maven仓库的工具,可对java/android进行模块化、组件化、插件化等进行依赖管理,有企业版和免费版。我们使用免费版即可,本文仓库搭建环境为阿里云centOS7.需注意nexus仓库的物理配置建议为2核4G内存,最好至少是2核2G,不能可能无法启动。

maven及jdk依赖

nexus依赖maven及jdk。下载方式略过,maven需保证mvn命令可用,另外需定义好JAVA_HOME环境变量。
[vim /etc/profile]

1
2
3
4
5
6
7
8
#Java
export JAVA_HOME=/path_toJdk/jdk1.8.0_141
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin

#maven
export M2_HOME=/path_toMavel/apache-maven-3.6.3
export PATH=$PATH:$JAVA_HOME/bin:$M2_HOME/bin

nexus下载

nexus提供mac、windows以及unix版的binary下载,centos选择unix的即可。截止到目前有2.x与3.x版,3.x可点击链接前往下载.同样先配置下环境变量
[vim /etc/profile]

1
2
export NEXUS_HOME=/path_toNexus/nexus-3.41.1-01
export PATH=$PATH:$NEXUS_HOME/bin

nexus配置

nexus若提示找不到jdk,可手动修改路径
[vim /NEXUS_HOME/bin/nexus]

1
INSTALL4J_JAVA_HOME_OVERRIDE=/path_toJdk/jdk1.8.0_141

nexus启动

nexus start
默认启动端口为8081,需避免端口占用。浏览器访问后,点击登录,用户名admin,密码默认存储在nexus同级目录sonatype-work下,根据弹窗提醒前往拷贝,首次登录后修改密码即可。拷贝setting/respository下的maven-releases和maven-sanshots url地址备用

启动时间较长,可能30s以上

安卓示例

  1. 配置probject build.gradle,buildscript和allprobjects下添加私有的maven仓库nexus地址
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    buildscript{
    repositories{
    google()
    mavenCentral()
    maven{
    url:"http://svinvy.com:8081/repository/maven-releases"
    }
    }
    dependencies:{}
    }
    allprojects{
    repositories{
    google()
    mavenCentral()
    maven{
    url:"http://svinvy.com:8081/repository/maven-releases"
    }
    }
    }
  2. 配置需上传的module的build.gradle,添加上传配置uploadAchirves。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    apply plugin:'maven'
    uploadArchives:{
    configuration=configurations.archives
    repositories:{
    mavenDeployer{
    repository(url:MAVEN_REPO_REPLEASE_URL){
    authentication:(userName:NEXUS_USERNAME,passowrd:NEXUS_PASSWORD)
    }
    pom.project{
    version "1.0.0"
    artifactId 'ASPlugin'
    goupId GROUP_ID
    packaging TYPE
    description DESCRIPTION
    }
    }
    }
    }

    MAVEN_REPO_RELEASE_URL(nexus的maven-releases url),NEXUS_USERNAME/NEXUS_PASSWORD(nexus用户名/密码),GROUP_ID,TYPE,DESCRIPTION等需在gradle.properties下提前配置好。

  3. 执行./gradlew uploadAchirves命令发布module.
  4. 在需要使用添加依赖的appication/module的build.gradle下添加api依赖即可。

配置SSL证书

使用nginx反向代理8081端口即可。需要注意的是3.x和2.x的nexus版本不同,3.x的nexus地址不以nexus开头。因此配置location时需rewrite将相应path前缀移除。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
location ^~/nexus {
rewrite ^/nexus(.*)$ $1 break;#移除nexus前缀
proxy_pass http://127.0.0.1:8081/nexus;

sendfile off;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;#forward为https请求
proxy_max_temp_file_size 0;

# This is the maximum upload size
client_max_body_size 20m;
client_body_buffer_size 128k;

proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;

proxy_temp_file_write_size 64k;

# Required for new HTTP-based CLI
proxy_http_version 1.1;
proxy_request_buffering off;
proxy_buffering off; # Required for HTTP-based CLI to work over SSL
}