美文网首页
Android私有Maven仓库搭建及arr打包上传

Android私有Maven仓库搭建及arr打包上传

作者: zii4914 | 来源:发表于2020-10-10 17:33 被阅读0次

环境

系统:windows10
JDK:Oracle JDK 1.8
仓库软件:Nexus Repository Manager OSS 3.x

一、Nexus Repository Manager OSS 3.x 安装及配置

1.下载

官网: https://www.sonatype.com/
下载地址: https://www.sonatype.com/nexus-repository-oss
官方文档: https://help.sonatype.com/repomanager3
下载的时候选择Nexus Repository OSS,带Pro的是收费版。不需要登录,输入任意邮箱就可以下载了。

2.安装

下载后是个压缩包,软件是绿色版,不用安装,直接解压到指定目录就可以了。但是Windows用户需注意目录路径不能含有中文,空格等字符。官方文档还说不要放到Program Files,因为会有注册问题,另外注意文件夹权限等,总之,怕麻烦别放C盘吧。
我下载的压缩包有两个文件夹,一个是nexus-3.28.0-01,另外一个是sonatype-work。不同版本可能不一样。

Nexus Repository Manager should not be installed in the Program Files directory to avoid problems with Windows file registry virtualization. If you plan to run the repository manager as a specific user you can install it into the AppData\Local directory of that users home directory. Otherwise simply use e.g., C:\nexus or something similar, ensuring that the user running the application has full access.

3.配置

打开D:\SonatypeNexus\nexus-3.28.0-01\etc\nexus-default.properties。注意不是sonatype-work下的etc

# Jetty 访问端口,默认是8081
application-port=8081
# 本机地址
application-host=0.0.0.0

4.启动服务

4.1直接启动

使用管理员权限运行cmd,进入Nexusbin目录下执行nexus.exe /run

C:\Windows\system32>D:
D:\>cd D:\SonatypeNexus\nexus-3.28.0-01\bin
D:\SonatypeNexus\nexus-3.28.0-01\bin>nexus.exe /run

第一次启动比较慢,等到打印出来下面的内容就启动成功了。

-------------------------------------------------

Started Sonatype Nexus OSS 3.12.0-01

-------------------------------------------------

这个启动成功之后,服务就启动了,这个时候就可以访问控制台了 http://localhost:8081/
关闭cmd窗口可以停止服务。

4.2安装成windows服务

进入nexusbin目录,执行nexus.exe /install <optional-service-name>

D:\SonatypeNexus\nexus-3.28.0-01\bin>nexus.exe /install nexus
Installed service 'nexus'.

其他服务操作指令

#启动
nexus.exe /start <optional-service-name>
#停止
nexus.exe /stop <optional-service-name>
#卸载
nexus.exe /uninstall <optional-service-name>

5.使用

打开http://localhost:8081/
点击右上角Sign in,输入账号密码,管理员账号是admin,密码是存储在本地一个文件中,首次登录,在登录窗口有提示密码文件位置,用文本查看工具打开就可以看到密码,修改了管理员密码后,该文件就会消失。

登录

点击齿轮状按钮,可进入配置页面,进入Repository-Repositories:


image.png

Repository的type属性有:proxy,hosted,group三种。

proxy:即你可以设置代理,设置了代理之后,在你的nexus中找不到的依赖就会去配置的代理的地址中找;
hosted:你可以上传你自己的项目到这里面;
group:它可以包含前面两个,是一个聚合体。一般用来给客户一个访问nexus的统一地址。

简单的说,就是你可以上传私有的项目到hosted,以及配置proxy以获取第三方的依赖(比如可以配置中央仓库的地址)。前面两个都弄好了之后,在通过group聚合给客户提供统一的访问地址。

至于format,因为本文讲的的 Maven Repository ,所以请选择maven2;

你还可以添加一个国内的私有maven库作为代理,避免中央库无法连接,譬如阿里云:
http://maven.aliyun.com/nexus/content/groups/public/

系统默认就有以上几个Repository。点击maven-public 确保已经将 maven-central,maven-releases以及maven-snapshots都包含在里面,如果你自己有自定义库,请确保也在里面。

image.png

maven-releases : 默认配置只能上传 release版本的项目
maven-snapshots: 默认配置只能上传 snapshots版本的项目
nuget开头的库: .net使用的库,java不用管

如有特殊要求,可以自己创建一个Version policy 为Mixed的Repository。

以上配置就能满足一般需求了。

  • 添加用户
    找到 Security > Users , 点击 Create local user 添加新用户.
    image.png

二、AndroidStudio上传arr

1. 打包上传

修改需要打包的module下的的build.gradle

//引入maven插件
apply plugin: 'maven'

def RELEASE_REPOSITORY_URL = "http://localhost:8081/repository/zii-host/"
def SNAPSHOT_REPOSITORY_URL = "http://localhost:8081/repository/zii-host/"
def REPOSITORY_RUL = RELEASE_REPOSITORY_URL;
def NEXUS_USERNAME = "zii4914"
def NEXUS_PASSWORD = "----"

def GROUP_ID="com.github.zii4914"
def VERSION_NAME="v1.1.0"
def ARTIFACT_ID="mylib"

uploadArchives {
    repositories.mavenDeployer {

        repository(url: REPOSITORY_RUL) {
            authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
        }
        // 快照仓库地址(可选)
        snapshotRepository(url: SNAPSHOT_REPOSITORY_URL) {
            authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
        }

        pom.project {
            name  ARTIFACT_ID// artifact名称
            version VERSION_NAME // artifact版本
            artifactId ARTIFACT_ID // artifact id
            groupId GROUP_ID // artifact所属Group Id
            packaging 'aar' // 文件格式
            description 'mylib package v' + VERSION_NAME // 描述
        }
    }
}

代码编译之后,在侧边Gradle窗口找到该模块下的upload任务,执行uploadArchives任务,会把module打包成arr,并且上传到仓库中。
上传成功后可以在仓库中看到相关信息

image.png

2. 引用依赖

//项目根目录下`build.gradle`
allprojects {
    repositories {
        google()
        jcenter()
        maven {
            //我在maven-public里面加入了自己新建的zii-host,所以可以从这里找到我的包
            url "http://localhost:8081/repository/maven-public/"
        }
    }
}

//在需要的模块中,添加依赖
 implementation 'com.github.zii4914:mylib:v1.1.0'

参考

CSDN > Nexus Repository Manager OSS 3.x 安装配置
简书 > Maven私服Nexus 3.x搭建

相关文章

网友评论

      本文标题:Android私有Maven仓库搭建及arr打包上传

      本文链接:https://www.haomeiwen.com/subject/jgljpktx.html