今天临时开会讨论要把不经常用的,独立的第三方库上传到本地nexus上。
首先说明下搭建本地maven库的优势:
经过一上午的努力 通过gradle达成目标,这里做一下分享:
SNAPSHOT_REPOSITORY_URL=http://xxx.xxx.xxxx.xxx:8080/nexus/..../repositories/snapshots/
#前面配置的releases仓库地址
RELEASE_REPOSITORY_URL=http://xxx.xxx.xxxx.xxx:8080/nexus/c...../repositories/releases/
其中xxx代表你本地的服务器url
nexus要有两种地址一个是 snapshots一个是releases
NEXUS_USERNAME=admin
NEXUS_PASSWORD=pwd
其中 admin和pwd代表你nexus服务器的账号和密码
在apply plugin: 'com.android.library'下添加如下内容:
apply plugin: 'maven'
def isReleaseBuild() {
return VERSION_NAME.contains("SNAPSHOT") == false
}
def getRepositoryUsername() {
return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : ""
}
def getRepositoryPassword() {
return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : ""
}
afterEvaluate { project ->
uploadArchives {
repositories {
mavenDeployer {
pom.groupId = "你的lib库的包名"
pom.artifactId = "你lib库的项目名"
pom.version = "版本号"
repository(url: RELEASE_REPOSITORY_URL) {
authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
}
snapshotRepository(url: SNAPSHOT_REPOSITORY_URL) {
authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
}
}
}
}
task androidJavadocs(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
classifier = 'javadoc'
from androidJavadocs.destinationDir
}
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.sourceFiles
}
artifacts {
archives androidSourcesJar
// archives androidJavadocsJar
}
}
由于你在gradle.properites设置过RELEASE_REPOSITORY_URL、SNAPSHOT_REPOSITORY_URL、NEXUS_USERNAME和NEXUS_PASSWORD的值,所以这里可以直接引用。
Paste_Image.png
1 url一定要对,我之前url出了一些问题。会报如下错误: Failed to transfer file http:///xxxx. Return code is: 400" 出现上面的问题一般是如下问题导致的 1 url不对 2 证书不对 3 没有权限去上传到nexus库 4 该用户上传这个库是没有权限的 5 这个release版本号已经上传过了 在 stackoverflow有相关答案
至此 你的库已经上传到nexus上了 那么如果进行依赖那? 首先在你的整体项目的build.gradle的里面添加如下代码即可:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
mavenCentral()
jcenter()
flatDir {
dirs 'libs'
}
maven{
url 'http://xxx.xxx.xxx.xx:8080/nexus/....public/'
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
http://xxx.xxx.xxx.xx:8080/nexus/....public/ 表示你本地nexus的服务器地址 然后在你具体项目里面去依赖就可以了 然后gradle一下就好了。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有