gradle task plugin 开发

https://docs.gradle.org/current/userguide/custom_plugins.html
https://docs.gradle.org/current/userguide/custom_tasks.html
http://www.cnblogs.com/davenkin/p/gradle-learning-9.html
http://www.cnblogs.com/davenkin/p/gradle-learning-10.html

gradle task plugin 开发_第1张图片
image.png

task 开发

使用第三种 Standalone project

使用工具


gradle task plugin 开发_第2张图片
image.png
gradle task plugin 开发_第3张图片
image.png
gradle task plugin 开发_第4张图片
image.png
package com.xin.pulgin.updata

import org.gradle.api.Plugin
import org.gradle.api.Project

class BmobPublishPlugin implements Plugin {
    @Override
    void apply(Project project) {

        project.extensions.create("bmobPublishConfig", ApkDistExtension)
        project.task("bmobPublish", type: UpLoadTask) {
            /*运行在配置阶段 在此添加dependsOn是找不到的*/
            group 'publish'
        }
        /*https://discuss.gradle.org/t/how-to-define-a-preprocessing-task-for-android-build/6147*/
        project.afterEvaluate {
            /*build*/
            try {
                /*可简化成   project.bmobPublishConfig.release*/
                if (project.extensions['bmobPublishConfig'].release) {
                    def release = project.tasks.getByName("assembleRelease")
                    project.bmobPublish.dependsOn release
                } else {
                    def debug = project.tasks.getByName("assembleDebug")
                    project.bmobPublish.dependsOn debug
                }
            } catch (Exception e) {
                println e.message
            }
        }
    }

}

package com.xin.pulgin.updata

import com.alibaba.fastjson.JSONObject
import com.xin.pulgin.updata.apk.ApkInformation
import com.xin.pulgin.updata.apk.ApkUtils
import com.xin.pulgin.updata.http.HttpDo
import com.xin.pulgin.updata.http.HttpListener
import com.xin.pulgin.updata.local.BmobKey
import com.xin.pulgin.updata.utils.Log
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.tasks.TaskAction
import rx.Observable
import rx.Subscriber

class UpLoadTask extends DefaultTask {
    ApkDistExtension config;

    //    TaskAction 的方法 都会执行
    @TaskAction
    void doAction() {
        if (!check(project)) {
            return
        }
        project.assembleDebug.execute();
        config = getProject().extensions['bmobPublishConfig']
       
    }
    /**
     * 检查配置是否正确
     */
    private boolean check(Project project) {
        /*检查是否正确的配置了*/
        try {
            def object = project.extensions['bmobPublishConfig']
            if (object.isEmpty()) {
                throw new Exception();
            }
        } catch (Exception e) {
            Log.log e.message
            Log.log '''
                    请先配置
                     bmobPublishConfig {
                            appId "***"
                            restFulId "***"
                            log "应用日志 可选"
                            filePath "apk应用路径 可选"
                        }'''
            return false
        }
        /*检查是否是android应用*/
        try {
            def android = project.extensions['android']
        } catch (Exception e) {
            Log.log e.message
            Log.log '''
                  Must apply com.android.application  first!
              '''
            return false
        }

        return true;
    }
   


}

apply plugin: 'groovy'
apply plugin: 'maven'
group 'f4'
version '1.0-SNAPSHOT'

apply plugin: 'groovy'
apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
     compile gradleApi()  //手动添加的
    compile 'org.codehaus.groovy:groovy-all:2.3.11'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}
uploadArchives {
    repositories.mavenDeployer {
        repository(url: 'file:../lib')
    }

注:

uploadArchives {
    repositories.mavenDeployer {
        repository(url: 'file:../lib')
    }

file:..当前文件为上一层目录

测试类

package com.xin.test

import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.testfixtures.ProjectBuilder
import org.junit.Test

class apkTest2 {
    @Test
    public void projectTest() {
        def string = 'src\\test\\project'
        def project = new ProjectBuilder().withProjectDir(new File(string)).build()
        project.apply plugin: 'bmobPublish'
        project.tasks.getByPath("bmobPublish").execute();

    }

    @Test
    public void projectTest2() {
        Project project = ProjectBuilder.builder().build()
        project.apply plugin: 'bmobPublish'
        project.buildscript {
            repositories {
                google()
                jcenter()
                mavenCentral()
            }
            dependencies {
                classpath 'com.android.tools.build:gradle:3.0.0'
            }
        }
        project.apply plugin: 'bmobPublish'
        project.apply plugin: 'com.android.application'
        project.tasks.getByPath("bmobPublish").execute();

    }

    @Test
    public void projectTest3() {
        Project project = ProjectBuilder.builder().build()
        project.task("aaa", type: DefaultTask) << {
            println "aaa"
        }
        project.task("bbb", type: DefaultTask) << {
            println "bbbb"
        }
        project.tasks.getByName("aaa").execute();
        project.tasks.getByName("bbb").execute();

        project.task("bmobPublish") {
            group 'publish'
            if (false) {
                dependsOn project.aaa
                dependsOn project.tasks.getByName("bbb")
            }
        } << {
            println "bmobPublish"
        }
        project.tasks.getByName("bmobPublish").execute();
    }
}

发布到bintray

https://www.iteblog.com/archives/1807.html
https://linroid.com/2015/03/13/upload-android-library-to-moven-central/
手把手教你将Android项目开源到JCenter两种方式以及挖坑和填坑(一)
bintray.gradle 文件 打包成jar的,上面文章的是aar,注册发布可参考上面

/*https://www.jianshu.com/p/02ffb5f46902*/
apply plugin: 'com.jfrog.bintray'

def siteUrl = 'https://github.com/mogujie'      // 项目的主页
def gitUrl = 'https://github.com/mogujie'   // Git仓库的url
group = "com.xin"  // Maven Group ID for the artifact,一般填你唯一的包名
version = "1.0"

install {
    repositories.mavenInstaller {
        // This generates POM.xml with proper parameters
        pom {
            project {
                packaging 'jar'
                // Add your description here
                name 'BmobPublishPlugin'    //项目描述
                url siteUrl
                // Set your license
                licenses {
                    license {
                        name 'The MIT License (MIT)'
                        url 'https://opensource.org/licenses/MIT'
                    }
                }
                developers {
                    developer {
                        id 'boyue'      //填写的一些基本信息
                        name 'boyue'
                        email '[email protected]'
                    }
                }
                scm {
                    connection gitUrl
                    developerConnection gitUrl
                    url siteUrl
                }
            }
        }
    }
}
task sourcesJar(type: Jar) {
    from sourceSets.main.java.srcDirs
    classifier = 'sources'
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}
artifacts {
    archives javadocJar
    archives sourcesJar
}

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" //此为创建的仓库名
        name = "PublishApkBmob" //发布到JCenter上的项目名字
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = ["MIT"]
        publish = true
    }
}

你可能感兴趣的:(gradle task plugin 开发)