Nexus 上传,引用 和坑(android studio)
使用nexus目的
对我而言,基本是为了解决私有库问题。
可以编写很多aar包上传上去进行依赖。避免本地代码过多问题。
Nexus启动
Nexus安装略过,网上教程很多。在mac下就是解压,然后cd到bin目录下,输入 ./nexus start 启动成功。默认地址为http://127.0.0.1:8081/,然后第一次登录会告知密码位置,复制过来后更改即可。
如何上传文件
在Nexus最右边有一个Upload,默认有两个库 maven_releases,nuget-hosted。我们点击maven_releases

File
上传文件路径文件路径,
Extension
文件类型 比如jar ,apk
groupId
定义了项目属于哪个组,最常见就是项目com.xxx.xxx
artifacted
定义了当前maven项目在组中唯一的ID,比如你开发了个库叫photo-picker。这里就写 photo-picker
Version
当前版本,SNAPSHOT意为快照,说明该项目还处于开发中,是不稳定的版本。
案例
androidx.constraintlayout:constraintlayout:2.0.1
groupId是androidx.constraintlayout,artifacted是constraintlayout,Version 2.0.1
点击Upload

点击view it now 就到你上传完成的文件那里了。
再点击一下


分别是上传文件,md5值 sha1值等。当然这些值就是你上传文件的对应的md5值和sha1值
android studio怎么上传aar包?
新建一个model,编写自己需要的代码,在该modle的bulid.gradle中,添加如下
apply plugin: 'maven'//添加maven依赖
uploadArchives {
repositories {
mavenDeployer {//开发者
//url 这里填写自己私服maven地址
repository(url: 'http://127.0.0.1:8081/repository/maven-snapshots/') {
//nexus登录默认用户名和密码
authentication(userName: "admin", password: "123456")
}
pom.groupId = "com.sd.jining"// 唯一标识(通常为模块包名,也可以任意)
pom.artifactId = "photo_x" // 项目名称(通常为类库模块名称,也可以任意)
pom.version = "1.0.23-SNAPSHOT" // 版本号
pom.packaging = "aar"//文件类型
}
}
}
Rebuild一下, 此刻右边应该显示出了这个uploadArchives,

有时候不显示task任务,

可以如图设置

点击uploadArchives, 应该能上传成功。
注意事项(我遇到的坑)
-
Nexus 私服有三种仓库类型:Hosted、Proxy和Virtual,另外还有一个 group (仓库组)用于对多个仓库进行组合。部署的时候只能部署到 Hosted 类型的宿主仓库中,如果是其他类型就会出现这个 400 错误。若是出现这个错误,只需修改 POM 文件中的部署仓库到对应的宿主仓库即可解决此问题。
-
repository和pom相关参数 一定要放在 mavenDeployer 中 不然就会提示路径错误 或者你发现自己传了个项目上去,而不是自己的aar库。
-
如果上传的库是SNAPSHOT ,version 后面一定要是-SNAPSHOT 不然就会报错。
-
发布了一个版本再发布就出异常。下面的设置请选中 Allow redeploy

上传成功 ,去自己对应的库里找一下看,search 中可以快速搜索


库的使用
项目build.gradle 设置
buildscript 和 allprojects- repositories 添加maven 相关,
buildscript {
ext.kotlin_version = "1.5.10"
repositories {
google()
//添加maven , 这里要添加用户名密码 和对应的url地址 ,url 地址获取看下面图
maven {
credentials {
username = 'admin'
password = '123456'
}
url "http://127.0.0.1:8081/repository/maven-snapshots/"
}
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
//添加maven , 这里要添加用户名密码 和对应的url地址
maven {
credentials {
username = 'admin'
password = '123456'
}
url "http://127.0.0.1:8081/repository/maven-snapshots/"
}
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}

在app下build.gradle 引用
这里对应的就是之前库 pom.groupId 和 pom.artifactId 和 pom.version
格式为 implementation groupId :artifactId:version 如下
implementation "com.sd.jining:photo_x:1.0.23-SNAPSHOT"
然后就可以欢快的使用自己打好的库了。