Android 搭建私有maven仓库及上传项目

参考链接

前言

在日常工作及项目迭代过程中,一定有不少同学自己写了一些框架性的东西,或者一些好用的工具,总之就是能复用的代码。然而也有不少同学为了复用这些代码不得不复制粘贴到不同项目中,这样相同的功能出现了多份代码,在后期的维护过程中带来极度不便,更新一次SDK要口口传送多次且无法按版本迭代和降级。为了解决这一系列问题,我们不妨尝试将这些代码部署到maven仓库,一处编写多处使用。

Nexus 介绍

Sonatype Nexus 是一个常见的搭建本地私有仓库的工具,详情进入官网查看(建议使用科学上网)。
https://www.sonatype.com/download-oss-sonatype

搭建私有Nexus仓库

下载Nexus安装包

Mac版下载更快
linux下载地址

解压 Nexus 文件在这里插入图片描述

启动 Nexus 服务

//启动
nexus-3.30.1-01/bin/nexus start
//关闭
nexus-3.30.1-01/bin/nexus stop 

控制台输出如下:
在这里插入图片描述
到此 Nexus 服务已经启动,浏览器ip:8081即可访问 Nexus 私服。

初始账户:admin
初始密码:看登录的提示

http://localhost:8081

创建宿主仓库

在这里插入图片描述
在这里插入图片描述
仓库类型:

  • hosted(宿主仓库):用来部署自己,第三方或者公共仓库的构件
  • proxy(代理仓库):代理远程仓库
  • virtual(虚拟仓库):默认提供了一个 Central M1虚拟仓库 用来将maven 2适配为maven 1
  • group(仓库组):统一管理多个仓库
    在这里插入图片描述
    Disable Redeploy: 不允许构件重新部署
    Allow Redeploy: 允许构件重新部署
    Read Only: 禁止构件部署到仓库中

查看仓库地址

在这里插入图片描述

上传库到Maven仓库

  1. 首先我们创建一个新的AndroidStudio 项目,然后新建一个module,选择Android Library
  2. 配置新建gradle.properties文件,定义通用属性

# Maven仓库的URL
MAVEN_REPO_RELEASE_URL=http://localhost:8081/repository/Library/
# 对应maven的GroupId的值
GROUP=Library
# 登录nexus ossde的用户名
NEXUS_USERNAME=admin
#登录nexus oss的密码
NEXUS_PASSWORD=admin
# groupid
GROUP_ID=com.android.Library
# type
TYPE=aar
# description
DESCRIPTION= 测试Library

3.修改module对应的build.gradle文件,添加以下配置

apply plugin: 'com.android.library'
apply plugin: 'maven'


uploadArchives {
    configuration = configurations.archives
    repositories {
        mavenDeployer {
            repository(url: MAVEN_REPO_RELEASE_URL) {
                authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
            }
            pom.project {
                version '1.0.4'
                artifactId 'myLibrary'
                groupId GROUP_ID
                packaging TYPE
                description DESCRIPTION
            }
        }
    }
}

4.点击uploadArchives进行编译上传
在这里插入图片描述
或者执行命令

./gradlew uploadArchives

在这里插入图片描述

使用 library

在根目录 build.gradle 加入

buildscript {
    repositories {
        google()
        jcenter()
        // 配置私有仓库地址
        maven {
            url 'http://localhost:8081/repository/Library/'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.1'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
         // 配置私有仓库地址
        maven {
            url 'http://localhost:8081/repository/Library/'
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

在依赖此 library 项目中引用

implementation 'com.android.Library:myLibrary:1.0.4'

401

Received status code 401 from server: Unauthorized
在这里插入图片描述

nexus.properties 在这里插入图片描述

# Jetty section
# application-port=8081 
# application-host=0.0.0.0
# nexus-args=${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-requestlog.xml
# nexus-context-path=/

# Nexus section
# nexus-edition=nexus-pro-edition
# nexus-features=\
#  nexus-pro-feature

# nexus.hazelcast.discovery.isEnabled=true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值