android本地lib通过gradle上传到本地nexus上

今天临时开会讨论要把不经常用的,独立的第三方库上传到本地nexus上。

首先说明下搭建本地maven库的优势:

  • 1 第三方库代码不是任何人都可以进行编辑的
  • 2 很多库在服务器的maven上会让本地的project结构更简单

经过一上午的努力 通过gradle达成目标,这里做一下分享:

第一步在本地gradle.properites设置公共字段:

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服务器的账号和密码

第二步 找到在本地需要上传到nexus的lib库内部的* build.gradle *文件。

在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的值,所以这里可以直接引用。

第三步 选择gradle你的项目,你可以在选在Build-->Make Project或者直接点击Android Studio上面AVDManager右侧的圆形按钮。均可以gradle你的项目。

第四步 gradle你的项目后会在gradle窗口下多出一个upload方法如图

Paste_Image.png

第五步 双击这个uploadArchives,如果输出successfull,就表示成功,就会上传到你nexus服务器。

我这里遇到的坑,在这里说下:

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的里面添加如下代码即可:

是你的整体项目的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一下就好了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容