如何上传自定义gradle插件和自定义library库到JCenter仓库

前言

  • 在上篇文章《本地项目上传到Github并用Github Desktop管理》就提到过,本来是打算同时讲下自定义gradle插件和自定义library库是如何上传到JCenter的,但是由于操作基本是一样的,所以在这篇文章,先讲讲如何写自定义gradle插件,以及如何将自定义gradle插件上传到JCenter仓库

1. 自定义gradle插件的实现

  1. 写gradle自定义插件之前,建议大家先阅读一下Gradle核心思想这篇文章:Gradle核心思想,先对Gradle有个比较全面的了解。

  2. 接下来!我们直入正题:如何写自定义插件。因为需要把自定义gradle插件上传到JCenter,那么需要用到ItenlliJ IDEA来开发,我的是macbook,在这里给大家提供一个比较好玩的Mac资源网站,直接搜索idea关键字就可以搜索到IntelliJ IDEA。当然,这里有收费版和免费版,土豪可以使用收费版,我使用的是免费版,支持新建gradle的groovy项目。
    在这里插入图片描述

  3. ItenlliJ IDEA新建一个Groovy工程,我的工程名叫CustomPluginShare:
    在这里插入图片描述

  4. 然后配置build.gradle:

    apply plugin: 'groovy'
    dependencies {
    	compile gradleApi()
    	compile localGroovy()
    }
    
  5. build工程,会在External Libraries下面生成一些jar包:
    在这里插入图片描述

  6. 这样我们就可以使用groovy语法和Gradle的api了,创建自定义插件在src/main/groovy/目录中创建一个包,我的是com.ben.plugins,在com.ben.plugins创建一个groovy文件:src/main/groovy/com/ben/plugins/CustomPlugin.groovy

    package com.ben.plugins
    import org.gradle.api.Plugin
    import org.gradle.api.Project
    class CustomPlugin implements Plugin<Project> {
    
    	@Override
    	void apply(Project project) {
        	project.task("CustomPluginTask") {
            	doLast {
                	println("自定义插件:CustomPluginTask")
            	}
        	}
    	}
    }
    
  7. 配置properties文件,这样插件才能被Gradle识别,如何配置呢?就是在生成的JAR文件中提供一个属性文件,这个属性文件名要与插件id相匹配。在resources中创建src/main/resources/META-INF/gradle-plugins/com.ben.plugins.customplugin.properties,这个属性文件的名称实际就是插件的id。
    在这里插入图片描述

  8. 将properties文件的内容改为:

    #implementation-class就是自定义插件的名称;
    implementation-class=com.ben.plugins.CustomPlugin
    #这个properties属性文件名称:com.ben.plugins.customplugin,就是自定义插件的id;
    #其他项目引用次插件的时候用得上,例如:apply plugin: 'com.ben.plugins.customplugin'

2. 上传自定义gradle插件到JCenter

  1. 首先需要申请一个Bintray账号。
    地址:https://bintray.com/signup/oss (需要科学上网)
    
  2. 接下来的操作参照AndroidStudio上传自己的项目到Bintray jCenter远程仓库!
  3. 下面把自己的build.gradle配置贴出来:
    buildscript {
    	repositories {
        	jcenter()
    	}
    	dependencies {
     	   classpath 'com.novoda:bintray-release:0.9.2'
    	}
    }
    
    plugins {
    	id 'groovy'
    }
    
    apply plugin: 'com.novoda.bintray-release'
    
    publish {
    	userOrg = 'bendeng' //bintray账户下某个组织id
    	groupId = 'com.ben.plugins' //maven仓库下库的包名,一般为模块包名
    	artifactId = 'CustomPluginShare' //项目名称
    	publishVersion = '1.0.0' //版本号
    	desc = 'first custom plugin' //项目介绍,可以不写
    	website = '' //项目主页,可以不写
    }
    
    group 'com.ben.plugins'
    version '1.0.0-SNAPSHOT'
    
    repositories {
    	mavenCentral()
    }
    
    dependencies {
    	compile gradleApi()
    	compile localGroovy()
    }
    
    //apply plugin: 'maven'
    //group = 'com.ben.plugins'
    //version = '1.0.0'
    //uploadArchives {
    //    repositories {
    //        mavenDeployer {
    //            // 将生成的插件上传到项目的平级目录repo下
    //            repository(url: uri('../repo'))
    //        }
    //    }
    //}
    
  4. 如果Binary上传时,报错了Failed to send a message: The version control 1.0.0 returns 404.,下面这个链接完美解决了这个问题:android Failed to send a message: The version control x.x.x returns 404.

3. 上传自定义library库到JCenter

  1. 这里直接贴出以为博友的文章,写得很详细:AndroidStudio上传自己的项目到Bintray jCenter远程仓库!

4. 在另一个项目中使用插件

  1. 新建一个Groovy项目,我取名为Projet,build.gradle的代码如下:
    buildscript {
    	repositories {
        	maven {
            	// 本地库
    	//            url uri('../repo')
         	   // maven库
        	    url "https://dl.bintray.com/bendeng/maven/"
        	}
    	}
    	dependencies {
       	 	// 集成自定义插件  groupId:project:version
       		classpath 'com.ben.plugins:CustomPluginShare:1.0.0'
    	}
    }
    
    plugins {
    	id 'groovy'
    }
    // apply:引用自定义插件
    apply plugin: 'com.ben.plugins.customplugin'
    
    group 'com.ben.project'
    version '1.0.0-SNAPSHOT'
    
    repositories {
    	mavenCentral()
    }
    
    dependencies {
    	compile 'org.codehaus.groovy:groovy-all:2.5.12'
    	testCompile group: 'junit', name: 'junit', version: '4.12'
    }
    
    
  2. Terminal中输入./gradlew CustomPluginTask来执行CustomPluginTask任务,打印了下面的日志,说明大功告成。
    BenDengdeMBP:Project bendeng$ ./gradlew CustomPluginTask
    Starting a Gradle Daemon (subsequent builds will be faster)
    
    > Task :CustomPluginTask
    自定义插件:CustomPluginTask
    
    Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
    Use '--warning-mode all' to show the individual deprecation warnings.
    See https://docs.gradle.org/6.7/userguide/command_line_interface.html#sec:command_line_warnings
    
    BUILD SUCCESSFUL in 7s
    1 actionable task: 1 executed
    

最后贴出项目的github地址:https://github.com/dengweijun/CustomPluginShare

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值