android nexus私服的使用

先唠叨几句啊,由于公司私服已经搭好了我就不费那劲琢磨搭建私服的事了,直接开撸上传lib。下图是我放组件库的地方,本来想一个module拉出一个项目来维护,后来想想实在是麻烦,同时维护多个库就要down多个库下来,于是就统一管理了。当然统一维护module多了自然会影响编译的速度,可以在settings.gradle 里将不编译的module注释掉就可以了。现在就有图片选择、表情、输入框、星星、视频录制、裁切等七七八八个库,至于后面组件库多了可能会分类按类型拉出几个项目来管理,要不版本控制也是个麻烦事。目前就是升级一个module在git上打个tag标签。大家有啥好的建议欢迎留言哈。

发布Library到私服

我们就以functionbar这个组件库为例吧

配置nexus账户信息

因为Nexus相关参数是固定的,包含仓库地址、用户名和密码,从安全性考虑我们把这些参数写到gradle的Global配置中,目录是C:\Users(用户名).gradle\gradle.properties

NEXUS_USERNAME=username
NEXUS_PASSWORD=password
NEXUS_REPOSITORY_URL=http://xxx/nexus/repository/maven-releases/

配置pom参数

项目根目录下的gradle.properties中添加如下pom参数

//依赖库名称
POM_NAME=functionbar
//版本号
POM_VERSION=1.0.1
//类别
POM_ARTIFACTID=utils
//组id
POM_GROUPID=com.app
//打包类型
POM_PACKAGING=aar

引入gradle脚本

在library的build.gradle文件末尾加上如下引用,当然这个文件需要创建放到项目根路径,源码在脚本解读

apply from: '../nexus_upload.gradle'

这个nexus_upload.gradle脚本包含生成java-source和java-doc,如果注释不完整可以注释掉脚本里的androidJavadocsJar调用,避免影响上传。

发布

双击右侧gradle task中的uploadArchives,编译并上传library

等待一会出现Success字样,证明已经上传成功

最后我们去Nexus上验证下,bingo!上传成功。

脚本解读

//依赖maven插件
apply plugin: 'maven'

task androidJavadocs(type: Javadoc) {
    options.encoding = "utf-8"
    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.srcDirs
}

artifacts {
    archives androidSourcesJar
    //如果项目javadoc不全会报错,可以注释掉
    archives androidJavadocsJar
}

uploadArchives {
    repositories {
        mavenDeployer {
            //仓库地址
            repository(url: NEXUS_REPOSITORY_URL) {
                //私服账户信息
                authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
            }
            //pom必需的参数
            pom.project {
                name POM_NAME
                version POM_VERSION
                artifactId POM_ARTIFACTID
                groupId POM_GROUPID
                packaging POM_PACKAGING
            }
        }
    }
}

使用私服上的Library

首先,要在项目的build.gradle里面声明私服的地址

allprojects {
    repositories {
        jcenter()
        mavenCentral()
        maven { url NEXUS_REPOSITORY_URL}
    }
}

然后就是我们最熟悉的在module的build.gradle文件中添加依赖,注意 groupId后面是:依赖库名称后面要加:和版本号,我就犯过引用是groupId后面写. 的错误。

compile 'com.app:functionbar:1.0.1

发布Plugin(插件)到私服

发布步骤是和library一样的只不过需要提一点,脚本信息需要做一些删减,只用到以下这些。

apply plugin: 'maven'
uploadArchives {
    repositories {
        mavenDeployer {
            //仓库地址
            repository(url: NEXUS_REPOSITORY_URL) {
                //私服账户信息
                authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
            }
            //pom必需的参数
            pom.project {
                name POM_NAME
                version POM_VERSION
                artifactId POM_ARTIFACTID
                groupId POM_GROUPID
                packaging POM_PACKAGING
            }
        }
    }
}

使用私服上的Plugin

首先,要在项目的build.gradle里面配置classpath引用,就拿经典的小刀举例吧

buildscript {

    repositories {
        jcenter()
        maven { url  NEXUS_REPOSITORY_URL}
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.0'
        classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1'
        ...
    }
}

然后在module中build.gradle引用插件plugin

apply plugin: 'com.android.application'
apply plugin: 'com.jakewharton.butterknife'

你可能感兴趣的:(android技术)