上传 Android aar 到 nexus 上

在新版本的Gradle 中,提供了publish这个操作,简化了整个上传的流程,详细 API在这个文档下,比如如果我需要上传 jar 到 maven上,只需要这样写

group = 'org.example'
version = '1.0'

publishing {
    publications {
        myLibrary(MavenPublication) {
            from components.java
        }
    }

    repositories {
        maven {
            name = 'myRepo'
            url = "file://${buildDir}/repo"
        }
    }
}

不过这只是Java的, Android的压根没用,所以我又找到一个插件来适配安卓的

首先添加插件

classpath 'digital.wup:android-maven-publish:3.6.2'

然后直接在加入对应代码

build.gradle

apply plugin: 'digital.wup.android-maven-publish'
//主要是区分逻辑,本地打的都是有 DEBUG 标志的包体
def reposUrl = "http://maven.xxx.com"
group = 'com.netease.cc'
publishing {
    publications {
        android.libraryVariants.all { variant ->
                from components.findByName("android${variant.name.capitalize()}")
                artifactId "你的aar名字"
                version "版本名”
            }
        }
    }

    repositories {
        maven {
            name reposName
            url reposUrl
            credentials {
                username = rootProject.properties['nexusUsername']
                password = rootProject.properties['nexusPassword']
            }
        }

    }
}

记得需要再 gradle.properies 中加入 nexusUsername=xxxxnexusPassword=password 不然会验证不过

在 gradle task 中,应该可以看到publishxxxxx名字的任务,点击相印的任务就能直接上传

更复杂的实现

因为项目中需要实现开发只能上传DEBUG包,RELEASE 包体必须要是构建系统完成,所以我做了个比较复杂的改版

/**
 * sdk上传代码仓库的相关信息
 */
//如果是打包机会有这个环境变量
def isPacker = Packer.isPacker(project)
//def isPacker = true
group = 'com.netease.cc'

apply plugin: 'digital.wup.android-maven-publish'
//主要是区分逻辑,本地打的都是有 DEBUG 标志的包体
def versionSuffix = isPacker ? "" : "-DEBUG"
def reposName = isPacker ? "ReleaseRepos" : "DebugRepos"
def reposUrl = "http://xxxx.xxxx.com/nexus/content/repositories/${isPacker ? "releases" : "debug"}"
publishing {
    publications {
        android.libraryVariants.all { variant ->

            def nameLowerCase = variant.name.toLowerCase()
            //只创建 release 构建的 push
            if (!nameLowerCase.endsWith("release")) {
                return
            }
            def nameLowerCaseWithBuildType = nameLowerCase.replace('release', '')
            def artifactIdSuffix = "-${nameLowerCaseWithBuildType}"
            "${nameLowerCaseWithBuildType}"(MavenPublication) {
                from components.findByName("android${variant.name.capitalize()}")
                artifactId "ccgroomsdk${artifactIdSuffix}"
                version gradle.ext.sdkMavenVersion + versionSuffix
            }
        }
    }

    repositories {
        maven {
            name reposName
            url reposUrl
            credentials {
                username = rootProject.properties['nexusUsername']
                password = rootProject.properties['nexusPassword']
            }
        }

    }
}

这个就不多讲了,日后自己看吧