這里有關jcenter的注冊以及創建jcenter倉庫,在我的另一篇上傳jcenter已經寫過。
第一步:用Android Studio建立一個簡單的插件
1、新建一個項目pluginlibrary(名字隨意),再新建一個module(library),類型選擇Android Library
2、將src/main下的目錄全部刪除,創建一個新的目錄groovy,接着根據module的包名創建目錄,比如我的module包名是com.plugin.gradle.library,
那么就按照這個目錄創建。之后在該目錄下創建一個以groovy后綴名的文件TestPlugin.groovy,作用是打印出一行字,目的是檢驗插件是否能運行。
package com.plugin.gradle.library import org.gradle.api.Plugin import org.gradle.api.Project class TestPlugin implements Plugin<Project> { @Override void apply(Project project) { println "mack success haha" } }
項目結構和代碼如下:
3、在main目錄下創建resources目錄,在這個目錄下創建META-INF/gradle-plugins, 在目錄里面我們再創建名為com.test.plugin.properties的文件,
文件名library就是以后給別人引用的插件名(插件名相當於 其他項目引入的builde apply plugin: 'com.test.plugin')。
在該文件添加代碼:implementation-class=com.plugin.gradle.library.TestPlugin

第二步:配置gradle(重要)
1、先配置好主項目的build.gradle,這里需要引入兩個依賴

classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.5'
2、配置module:library里面的gradle,因為我們最終上傳的是這個插件,所以重要的信息都包含在里面。

apply plugin:'groovy' apply plugin:'maven' apply plugin:'com.jfrog.bintray' apply plugin:'maven-publish' //另外一個上傳包到 jcenter 的插件 //apply plugin: 'com.novoda.bintray-release'//添加 dependencies { implementation gradleApi() implementation localGroovy() implementation 'com.android.tools.build:gradle:3.0.0' implementation "org.aspectj:aspectjtools:1.8.9" implementation "org.aspectj:aspectjrt:1.8.9" implementation 'com.android.tools.build:transform-api:1.5.0' } repositories { mavenCentral() } //打包到本地 //group = 'com.huodonghezi.aspectj.Plugin' //version = '1.1.0' //uploadArchives { // repositories { // mavenDeployer { // repository(url: uri('../repo')) // } // } //} group ='com.test.plugin.android' // 組名 jcenter中的倉庫路徑 version ='1.0.0' // 版本 //// 應用插件 apply from:'bintray.gradle'
2、配置module:里面創建上傳jcenter的bintray.gradle,所有重要的信息都包含在里面。

applyplugin: 'com.jfrog.bintray'
applyplugin: 'maven-publish'
def baseUrl = '項目git路徑'
def siteUrl = baseUrl
def gitUrl = "${baseUrl}/ hzAspectJ"
def issueUrl = "${gitUrl}/issues"
install {
repositories {
mavenInstaller {
// This generates POM.xml with proper paramters
pom.project {
//添加項目描述
name 'Gradle Plugin for Android'
url siteUrl
//設置開源證書信息
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
//添加開發者信息
developers {
developer {
name 'jcenter 的用戶名'
email ' jcenter 登錄郵箱'
}
}
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl
}
}
}
}
}
//配置上傳Bintray相關信息
bintray {
user = '用戶名'
key = ' jcenter 的個人 api key'
configurations = ['archives']
pkg {
repo = 'welljan' // 上傳到中央倉庫的名稱
name = 'hzAspectJ' // 上傳到jcenter 的項目名稱
desc = 'test aop' // 項目描述
websiteUrl = siteUrl
issueTrackerUrl = issueUrl
vcsUrl = gitUrl
labels = ['gradle', 'plugin']
licenses = ['Apache-2.0']
publish = true
}
}
第三步:上傳插件
我們到底怎么上傳這個插件呢,其實有兩種方法,但本質上是一樣的,可以使用工具,或者使用命令行。1、使用gradle快捷工具

第一步,在右上角側邊欄找到Gradle,點擊打開,在目錄中展開pluginlibrary,雙擊install運行

第二步如圖展開publishing,雙擊bintrayUpload運行。

打開Messages,當你看到BUILD SUCCESSFUL,就代表已經上傳成功啦。