Introduction to the

After completing an open source library, we usually upload the AAR package to JCenter for easy distribution. Upload depends on a personally maintained plugin android-Maven-gradle-plugin. Update Android Studio to 3.6.1, So the default Android gradle plugin upgrade to com. The Android. View the build: gradle: after 3.6.1 Com. Making. Dcendents: android – maven – gradle – plugin: 2.1 again across, and found that the original author has given pit, and recommend https://github.com/sky-uk/gradle-maven-plugin

However, this script directly references the remote script, which is unstable and inconsistent with national conditions. Existing scripts that use my many open source libraries vary greatly. Therefore, I made great improvements on the basis of the modified project, making it more convenient to use:

use

  1. Create the following files in the project root directory:

gradle-mavenizer.gradle:

apply plugin: 'maven-publish'

//utils
def isAndroidProject() {
    def plugins = project.getPlugins()
    return plugins.hasPlugin('com.android.application') || plugins.hasPlugin('com.android.library')}def getStringProperty(String propertyName) {
    return project.hasProperty(propertyName) ? project.getProperty(propertyName) : ""
}

def getBooleanProperty(String propertyName) {
    return project.hasProperty(propertyName) ? project.getProperty(propertyName) : false
}

def getArrayProperty(String propertyName) {
    return project.hasProperty(propertyName) ? project.getProperty(propertyName) : []
}

//pom manager
def decoratePom(pom) {
    pom.name = getStringProperty("mavProjectName")
    pom.description = getStringProperty("mavLibraryDescription")
    pom.url = getStringProperty("mavSiteUrl")

    pom.licenses {
        getArrayProperty("mavLibraryLicenses").each { licenseName, licenseUrl ->
            license {
                name = licenseName
                url = licenseUrl
            }
        }
    }
    pom.developers {
        getArrayProperty("mavDevelopers").each { developerId, developerName ->
            developer {
                id = developerId
                name = developerName
            }
        }
    }
    pom.scm {
        connection = getStringProperty("mavGitUrl")
        developerConnection = getStringProperty("mavGitUrl")
        url = getStringProperty("mavSiteUrl")
    }

    addDependencies(pom)
}

def addDependencies(pom) {
    pom.withXml {
        final dependenciesNode = asNode().appendNode('dependencies')
        ext.addDependency = { Dependency dep, String scope ->
            if (dep.group == null || dep.version == null || dep.name == null || dep.name == "unspecified")
                return // ignore invalid dependencies

            if (dep.artifacts.size() > 0) {
                dep.artifacts.each { art ->
                    addDependencyNode(dependenciesNode, dep, scope, art.classifier, art.extension)
                }
            } else {
                addDependencyNode(dependenciesNode, dep, scope, null.null)
            }
        }
        manageConfigurations(configurations)
    }
}

def addDependencyNode(dependenciesNode, dep, scope, classifier, extension) {
    final dependencyNode = dependenciesNode.appendNode('dependency')
    dependencyNode.appendNode('groupId', dep.group)
    dependencyNode.appendNode('artifactId', dep.name)
    dependencyNode.appendNode('version', dep.version)

    if(classifier ! =null) {
        dependencyNode.appendNode('classifier', classifier)
    }

    if(extension ! =null) {
        dependencyNode.appendNode('type', extension)
    }

    dependencyNode.appendNode('scope', scope)

    if(! dep.transitive) {// If this dependency is transitive, we should force exclude all its dependencies them from the POM
        final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion')
        exclusionNode.appendNode('artifactId'.The '*')
        exclusionNode.appendNode('groupId'.The '*')}else if(! dep.properties.excludeRules.empty) {// Otherwise add specified exclude rules
        final exclusionsNode = dependencyNode.appendNode('exclusions')
        dep.properties.excludeRules.each { ExcludeRule rule ->
            final exclusionNode = exclusionsNode.appendNode('exclusion')
            exclusionNode.appendNode('artifactId', rule.module ? :The '*')
            exclusionNode.appendNode('groupId', rule.group ? :The '*')}}}def manageConfigurations(configurations) {
    configurations.compile.getDependencies().each { dep -> addDependency(dep, "compile") }
    configurations.api.getDependencies().each { dep -> addDependency(dep, "compile") }
    configurations.implementation.getDependencies().each { dep -> addDependency(dep, "runtime") }
    configurations.testImplementation.getDependencies().each { dep -> addDependency(dep, "test") }
    configurations.testCompile.getDependencies().each { dep -> addDependency(dep, "test")}if(! isAndroidProject()) { configurations.runtime.getDependencies().each { dep -> addDependency(dep,"runtime") }
        configurations.testRuntime.getDependencies().each { dep -> addDependency(dep, "test")}}}//javadoc handler
if (isAndroidProject()) {
    task androidJavadocs(type: Javadoc) {
        title = "${getStringProperty("mavProjectName")} ${project.version} API"
        description "Generates Javadoc"
        source = android.sourceSets.main.java.srcDirs
        classpath += files(android.bootClasspath)

        android.libraryVariants.all { variant ->
            if (variant.name == 'release') {
                owner.classpath += variant.javaCompileProvider.get().classpath
            }
        }

        exclude '**/R.html'.'**/R.*.html'.'**/index.html'.'**/*.kt'

        options {
            windowTitle("${getStringProperty("mavProjectName")} ${project.version} Reference")
            locale = 'en_US'
            encoding = 'UTF-8'
            charSet = 'UTF-8'
            links("http://docs.oracle.com/javase/7/docs/api/")
            linksOffline("http://d.android.com/reference"."${android.sdkDirectory}/docs/reference")
            setMemberLevel(JavadocMemberLevel.PUBLIC)
        }
    }

    task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
        classifier = 'javadoc'
        from androidJavadocs.destinationDir
    }

    task androidSourcesJar(type: Jar) {
        classifier = 'sources'
        from android.sourceSets.main.java.srcDirs
    }
} else {
    task sourcesJar(type: Jar) {
        from sourceSets.main.allJava
        archiveClassifier = 'sources'
    }

    task javadocJar(type: Jar) {
        from javadoc
        archiveClassifier = 'javadoc'}}//publish
project.afterEvaluate {
    publishing {
        publications {
            mavenPublish(MavenPublication) {
                if (isAndroidProject()) {
                    artifact bundleReleaseAar
                    artifact androidJavadocsJar
                    artifact androidSourcesJar
                } else {
                    artifact jar
                    artifact sourcesJar
                    artifact javadocJar
                }
                
                decoratePom(pom)
            }
        }
        
        repositories {
            def isToRemoteRepo = getBooleanProperty("mavPublishToRemoteRepo")
            def isToInternalRepo = getBooleanProperty("mavPublishToInternalRepo")
            def isToMavenLocal = getBooleanProperty("mavPublishToMavenLocal")

            if (isToRemoteRepo) {
                maven {
                    credentials {
                        username getStringProperty("mavRemoteRepoUser")
                        password getStringProperty("mavRemoteRepoPassword")
                    }
                    url = getStringProperty("mavRepoRemoteUrl")}}if (isToInternalRepo) {
                maven {
                    url = getStringProperty("mavRepoInternalUrl")}}if(isToMavenLocal || (! isToInternalRepo && ! isToRemoteRepo)) { mavenLocal() } } } }Copy the code

deployBintray.gradle:

apply plugin: 'com.jfrog.bintray'

if(! Bintray_user) {// The bintary account information needs to be configured in ~/.gradle.propertiesprint("add your bintray_user & bintray_key first!!")
    return
}

def properties = new Properties()
properties.load(new FileInputStream(rootProject.file('project.properties')))

project.ext {
    mavGitUrl =  properties.getProperty("project.gitUrl")
    mavProjectName = properties.getProperty("project.name")
    mavLibraryLicenses = ["Apache - 2.0":'http://www.apache.org/licenses/LICENSE-2.0.txt']
    mavLibraryDescription = properties.getProperty("project.desc")
    mavPublishToMavenLocal = true
    mavSiteUrl = properties.getProperty("project.siteUrl")
}


bintray {
    user = "${bintray_user}"
    key = "${bintray_key}"
    publications = ['mavenPublish']
    configurations = ['archives']
    override = true
    pkg {
        repo = properties.getProperty("project.repo")
        name = properties.getProperty("project.name")
        websiteUrl = properties.getProperty("project.siteUrl")
        vcsUrl = properties.getProperty("project.gitUrl")
        description = properties.getProperty("project.desc")
        publicDownloadNumbers = true
        licenses = ['the Apache 2.0']
    }
}

apply from: rootProject.file('gradle-mavenizer.gradle')
Copy the code

Project.properties :(configuration information)

# suppress inspection "UnusedProperty" for whole file
#projectProject. Repo = Bintray registered maven repository name, The default value is Maven project.name= project name project.desc= description project.groupId= XXX project Project. GitUrl = XXX license. Name = Apache 2.0 license.url=https\://opensource.org/licenses/apache2.0.php developer. Id = XXX developer.name=xxx developer.email=xxxCopy the code
  1. In the user directory ~ /. Gradle/gradle properties configuration information as follows:
Bintray_key = Token obtained from the Bintray console Bintray_user = Bintray user nameCopy the code
  1. Add the following information to build. Gradle of the root project to set groups and versions for each library submodule
subprojects {
    if ("lib" == it.name) {
        group = 'com.example.xxx'// Publish to bintray with groupId id version ="1.0.0"// Release to bintray version number}}Copy the code
  1. Build.graldet adds plug-ins
        classpath 'com. Jfrog. Bintray. Gradle: gradle bintray - plugin: 1.8.4'

Copy the code
  1. Reference at the bottom of the library module build.gradle:
apply from: '.. /deployBintray.gradle'
Copy the code
  1. The last executionbintrayUploadTask packages library to upload JCenter

Example project: github.com/hotstu/chip…