Android Gradle plugin 开发并上传到JCenter

Posted 小向往

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Gradle plugin 开发并上传到JCenter相关的知识,希望对你有一定的参考价值。

注册 bintray 账号

可以参考之前的博客 传送

创建插件项目

可以先创建Module->android Library,然后删掉多余的文件和文件夹。 MyPlugin 必须是 groovy 格式的文件:MyPlugin.groovy

1. 目录结构

2. 项目根目录 buidle.gradle 配置
buildscript 
    dependencies 
        ...
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
    
3. 插件 Module build.gradle 文件
apply plugin: 'groovy'
apply plugin: 'maven'

//发布 Jcenter
apply from: 'bintray.gradle'
//本地
apply from: 'maven.gradle'
4. maven.gradle 文件
apply plugin: 'maven'

//发布在本地,../plugin 文件夹
uploadArchives 
    repositories.mavenDeployer 
        repository(url: uri('../plugin'))
        pom.groupId = 'com.totcy.android'
        pom.artifactId = 'gradle-plugin-test'
        pom.version = '1.0.0'
    
5. bintray.gradle 文件
apply plugin: 'com.jfrog.bintray'
apply plugin: 'maven-publish'
apply plugin: 'java'
apply plugin: 'signing'
apply plugin: 'maven'

//使用: classpath 'com.totcy.android:gradle-plugin-test:1.0.0'
group = "com.totcy.android"
version = "1.0.0"
project.archivesBaseName = "gradle-plugin-test"

task javadocJar(type: Jar, dependsOn: javadoc) 
    classifier = 'javadoc'
    from javadoc.destinationDir


task sourceJar(type: Jar) 
    classifier = 'sources'
    from sourceSets.main.allSource


ext 
    pomFilePath = "$project.buildDir.absolutePath/tmp/pom.xml"
    pomFile = file(pomFilePath)


configurations 
    pom


artifacts 
    archives jar
    archives sourceJar
    archives javadocJar
    if (pomFile.exists()) 
        pom pomFile
    


task signJars(type: Sign, dependsOn: [jar, javadocJar, sourceJar]) 
    sign configurations.archives


task signPom(type: Sign) 
    sign configurations.pom


if (project.ext.pomFile.exists()) 
    task preparePublication(dependsOn: [signJars, signPom])
 else 
    task preparePublication(dependsOn: signJars)


def getSignatureFiles = 
    def allFiles = project.tasks.signJars.signatureFiles.collectit
    def signedSources = allFiles.find it.name.contains('-sources') 
    def signedJavadoc = allFiles.find it.name.contains('-javadoc') 
    def signedJar = (allFiles - [signedSources, signJars])[0]
    return [
            [archive: signedSources, classifier: 'sources', extension: 'jar.asc'],
            [archive: signedJavadoc, classifier: 'javadoc', extension: 'jar.asc'],
            [archive: signedJar, classifier: 'null', extension: 'jar.asc']
    ]


def getPomSignature = 
    return project.tasks.signPom.signatureFiles.collect it [0]


def siteUrl = 'https://github.com/792814781/gradle-plugin-test'
def gitUrl = 'https://github.com/792814781/gradle-plugin-test.git'
def issueUrl = 'https://github.com/792814781/gradle-plugin-test/issues'

install 
    repositories.mavenInstaller 
// generates POM.xml with proper parameters
        pom 
            project 
                packaging 'jar'
                name 'The Android Plugin for Totcy.'
                url siteUrl

                licenses 
                    license 
                        name 'The Apache Software License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    
                
                developers 
                    developer 
                        id 'bintray用户名账户'
                        name '用户名'
                        email '邮箱'
                    
                

                scm 
                    connection gitUrl
                    developerConnection gitUrl
                    url siteUrl
                
            
        
    


Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())

bintray 
    user = properties.getProperty("bintray.user")
    key = properties.getProperty("bintray.apikey")
    configurations = ['archives']
    pkg 
        repo = "maven" //bintray 上面 Package名字
        name = "gradle-plugin-test" //bintray 上面项目名
        desc = 'The Android Plugin for Totcy.'
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        issueTrackerUrl = issueUrl
        licenses = ["Apache-2.0"]
        labels = 'Groovy'
        publicDownloadNumbers = true
        publish = true
    

bintray.userbintray.apikey 属于账户私密信息,定义在 local.properties,这两个信息都可以在 bintray 个人信息里面找到

6. 执行
  1. ./gralew install
  2. ./gralew bintrayUpload
    执行成功就可以在 bintray 个人仓库里面看到了,最后记得 点击 Add to Jcenter 提交审核。

使用

使用的项目根目录 build.gradle

buildscript 
     repositories 
        ...
        //发布在本地文件夹
        maven  url "../plugin" 
    
    dependencies 
        ...
        classpath 'com.totcy.android:gradle-plugin-test:1.0.0'
    


allprojects 
    repositories 
        ...
        //审核之前添加仓库地址即可,通过后可以删除
        maven  url "https://dl.bintray.com/intotcy/maven" 
    

使用的项目 Module gradle.build

apply plugin: 'gradle-plugin'

以上是关于Android Gradle plugin 开发并上传到JCenter的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio Gradle Plugin开发入门指南

Android Studio Gradle Plugin开发入门指南

Gradle之Android Gradle Plugin 主要 Task 分析

android gradle plugin开发 入门 搭建 小白级入门

android gradle plugin开发 入门 搭建 小白级入门

Android Gradle 插件Gradle 自定义 Plugin 插件 ② ( buildSrc 目录中实现 Gradle 插件 | 实现 Gradle 插件代码 | 模块引入插件并编译 )