常用gradle脚本整理

一:发布项目

1.发布项目到本地&nexus私有仓库

apply plugin: 'maven'
//test
println 'upload_maven running'
if (!project.hasProperty("POM_ARTIFACT_ID") || !project.hasProperty("POM_GROUP")) {
    //throw Exception(">>>>> 组件ArtifactID/GroupId必须设置 <<<<<");
    println('POM_ARTIFACT_ID or POM_GROUP missed.')
    return
}
if (!project.hasProperty("POM_VERSION")) {
    //throw Exception(">>>>> Version必须设置 <<<<<");
    println('POM_VERSION missed.')
    return
}
if(!rootProject.ext.has("USER_NAME") || !rootProject.ext.has("USER_PASS")) {
    println('please check rootProject.ext.USER_NAME and rootProject.ext.USER_PASS')
    return
}
String releaseRepoUrl = project.hasProperty("RELEASE_REPO_URL") ? RELEASE_REPO_URL : 'http://nexus.xxx.net/repository/android-release/'
String snapshotRepoUrl = project.hasProperty("SNAPSHOT_REPO_URL") ? SNAPSHOT_REPO_URL : 'http://nexus.xxx.net/repository/android-snapshots/'
def userName = rootProject.ext.USER_NAME
def userPassword = rootProject.ext.USER_PASS
def pom_artifact_id = POM_ARTIFACT_ID
def pom_group = POM_GROUP
def pom_version = POM_VERSION
def pom_description = project.hasProperty("POM_DESCRIPTION") ? POM_DESCRIPTION : "$pom_group:$pom_artifact_id:$pom_version"
def pom_packaging = project.hasProperty("POM_PACKAGING") ? POM_PACKAGING : 'aar'
def pom_name = project.hasProperty("POM_NAME") ? POM_NAME : pom_artifact_id

uploadArchives {
    //发布到本地仓库
    if (project.hasProperty("LOCAL") && LOCAL.toBoolean()) {
        releaseRepoUrl = uri(releaseRepoUrl)
        snapshotRepoUrl = uri(snapshotRepoUrl)
    }

    repositories.mavenDeployer {
        // 会自动根据传入的version判断是release模式还是snapshot模式,这是gradle的默认行为
        repository(url: releaseRepoUrl) {
            authentication(userName: userName, password: userPassword)
        }
        snapshotRepository(url: snapshotRepoUrl) {
            authentication(userName: userName, password: userPassword)
        }

        pom.groupId = pom_group //主项目的groupId,这里不能用group变量
        pom.artifactId = pom_artifact_id
        pom.version = pom_version

        pom.project {
            name pom_name
            description pom_description
            packaging pom_packaging
        }
    }
}

task cleanDir(type: Delete) {
    delete buildDir
}

task androidJavadocs(type: Javadoc) {
    // 设置源码所在的位置
    if (android != null && android.sourceSets != null && android.sourceSets.main != null && android.sourceSets.main.java != null) {
        source = android.sourceSets.main.java.sourceFiles
    }
    //source = android.sourceSets.main.java.srcDirs
    //classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

// 生成javadoc.jar, frameworkAnnotation中需要用
//task androidJavadocsJar(type: Jar) {
    // 指定文档名称
//    classifier = 'javadoc'
//    from androidJavadocs.destinationDir
//}

// 生成sources.jar
task androidSourcesJar(type: Jar) {
    if (android != null && android.sourceSets != null && android.sourceSets.main != null && android.sourceSets.main.java != null) {
        classifier = 'sources'
        from android.sourceSets.main.java.sourceFiles
    }
}

//是否是开发版本
def isDevOrBetaBuild(String version) {
    if (version == null || version.isEmpty()) {
        throw Exception(">>>>>>>>>>>>>>编译失败,POM_VERSION_TYPE = " + POM_VERSION_TYPE + " ,请确认gradle.properties文件中 POM_VERSION_TYPE 参数填写正确,POM版本必须为(DEV、BETA、RELEASE)中的一种类型<<<<<<<<<<");
    }
    return version.endsWith("-SNAPSHOT") //version.contains("dev") || version.contains("beta")
}

artifacts {
   // if (isDevOrBetaBuild(pom_version)) {
        archives androidSourcesJar
        //archives androidJavadocsJar
   // }
}
uploadArchives.mustRunAfter 'cleanDir'

2.发布到jCenter

//官方插件
plugins {
    //gradle 自定义插件 省掉建resources目录和properties等步骤,gradlePlugin闭包内配置完成
    id 'java-gradle-plugin'
    //发布插件到gradle官方插件库,成为官方插件可以使用plugins闭包一步配置,引用插件
    id 'com.gradle.plugin-publish' version '0.10.1'
    //发布到jCenter仓库,bintray闭包配置
    id "com.jfrog.bintray" version "1.8.4"
}
//第三方插件
apply plugin: 'maven'
apply plugin: 'java'
apply plugin: 'groovy'
//配置
ext {
    versionId = '1.1.0'
    groupId = 'com.cpdroid'
    name = 'fat-aar'
    website = 'https://github.com/cpdroid/fat-aar'
    vcsUrl = 'https://github.com/cpdroid/fat-aar.git'
    tags = ['fat-aar', 'aar', 'fat', 'android', 'fataar']
}

version = ext.versionId
group = ext.groupId

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
//jcenter
bintray {
    user = properties.getProperty('bintrayUser')
    key = properties.getProperty('bintrayApiKey')
    configurations = ['archives']
    pkg {
        repo = 'gradle'
        name = project.ext.name
        licenses = ['MIT']
        vcsUrl = project.ext.vcsUrl
        labels = project.ext.tags
        version {
            name = project.ext.versionId
        }
    }
}
//自定义插件需要
gradlePlugin {
    plugins {
        fatAarPlugin {
            id = "${project.ext.groupId}.${project.ext.name}"
            displayName = 'Fat Aar Plugin'
            description = 'A plugin to merge dependencies(aar/jar) into aar file'
            implementationClass = "${project.ext.groupId}.fat_aar.FatAarPlugin"
        }
    }
}
//发布插件到gradle官方
pluginBundle {
    website = project.ext.website
    vcsUrl = project.ext.vcsUrl
    tags = project.ext.tags
    mavenCoordinates {
        groupId = project.ext.groupId
        artifactId = project.ext.name
        version = project.ext.versionId
    }

}
//发布到本地
uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: uri('/Users/admin/AndroidStudioProjects/fat-aar/maven/repo/fatAar'))
        }
    }
}
//仓库地址
repositories {
    jcenter()
    google()
}

dependencies {
    implementation 'com.android.tools.build:gradle:3.2.1'
}

你可能感兴趣的:(常用gradle脚本整理)