wenmin92
1/12/2018 - 4:58 AM

上传Android库到Bintray,之后通过Bintray同步到JCenter

上传Android库到Bintray,之后通过Bintray同步到JCenter

apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

version = libraryVersion
group = publishedGroupId

install {
    repositories.mavenInstaller {
        // This generates POM.xml with proper parameters
        pom {
            project {
                packaging 'aar'
                artifactId artifact
                groupId publishedGroupId
                
                // Add your description here
                name libraryName
                description libraryDescription
                url siteUrl

                // Set your license
                licenses {
                    license {
                        name licenseName
                        url licenseUrl
                    }
                }
                developers {
                    developer {
                        id developerId
                        name developerName
                        email developerEmail
                    }
                }
                scm {
                    connection gitUrl
                    developerConnection gitUrl
                    url siteUrl
                }
            }
        }
    }
}

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

artifacts {
    archives sourcesJar
}

bintray {
    user = project.'bintray.user'
    key = project.'bintray.apikey'
    configurations = ['archives']
    override = true
    pkg {
        repo = bintrayRepo
        name = bintrayName
        desc = libraryDescription
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = allLicenses
        publish = true
        version {
            name = this.version
            desc = "${this.version}"
            released = new Date()
            vcsTag = this.version
        }
    }
}
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.bintray'

version libraryVersion
group publishedGroupId

// Create the pom configuration:
def pomConfig = {
    licenses {
        license {
            name licenseName
            url licenseUrl
        }
    }
    developers {
        developer {
            id developerId
            name developerName
            email developerEmail
        }
    }
    scm {
        developerConnection gitUrl
        url siteUrl
    }
}

publishing {
    publications {
        MyPublication(MavenPublication) {
            artifact sourcesJar
            artifact "$buildDir/outputs/aar/$project.name-release.aar"
            groupId publishedGroupId
            artifactId artifact
            version libraryVersion
            pom.withXml {
                def root = asNode()
                root.appendNode('name', libraryName)
                root.appendNode('description', libraryDescription)
                root.appendNode('url', siteUrl)
                root.children().last() + pomConfig
                def dependenciesNode = root.appendNode('dependencies')

                // Iterate over the implementation dependencies (we don't want the test ones), adding a <dependency> node for each
                configurations.implementation.allDependencies.each {
                    // Ensure dependencies such as fileTree are not included.
                    if (it.name != 'unspecified') {
                        def dependencyNode = dependenciesNode.appendNode('dependency')
                        dependencyNode.appendNode('groupId', it.group)
                        dependencyNode.appendNode('artifactId', it.name)
                        dependencyNode.appendNode('version', it.version)
                    }
                }
            }
        }
    }
}

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

artifacts {
    archives sourcesJar
}

bintray {
    user = project.'bintray.user'
    key = project.'bintray.apikey'
    // configurations = ['archives']
    publications = ['MyPublication']
    override = true
    pkg {
        repo = bintrayRepo
        name = bintrayName
        desc = libraryDescription
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = allLicenses
        publish = true
        version {
            name = this.version
            desc = "${this.version}"
            released = new Date()
            vcsTag = this.version
        }
    }
}
ext {
    bintrayRepo = 'library' // 你的仓库的名称
    bintrayName = 'bt-printer' // bintray 库的名称

    publishedGroupId = 'com.chinawutong'
    libraryName = 'BTPrinter'
    artifact = 'bt-printer'

    libraryDescription = '封装蓝牙打印小票功能'

    siteUrl = 'https://github.com/wenmin92/BTPrinter'
    gitUrl = 'https://github.com/wenmin92/BTPrinter.git'

    libraryVersion = '1.0.0'

    developerId = 'wenmin92'
    developerName = 'Changzhu Zhao'
    developerEmail = 'wenmin92@gmail.com'

    licenseName = 'The Apache Software License, Version 2.0'
    licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
    allLicenses = ["Apache-2.0"]
}

上传bintray操作步骤

  1. bintray上的操作
    1. 先注册bintray账号
    2. 新建一个仓库
  2. 本地配置
    1. 在项目根目录下的build.gradle文件中,添加dependencies:
      classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
      classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.0'
      
    2. 在库的build.gradle文件中,添加ext,根据自己的需要修改这个配置:
      ext {
          bintrayRepo = 'library'
          bintrayName = 'bt-printer'
      
          publishedGroupId = 'com.chinawutong'
          libraryName = 'BTPrinter'
          artifact = 'bt-printer'
      
          libraryDescription = '封装蓝牙打印小票功能'
      
          siteUrl = 'https://github.com/wenmin92/BTPrinter'
          gitUrl = 'https://github.com/wenmin92/BTPrinter.git'
      
          libraryVersion = '1.0.0'
      
          developerId = 'wenmin92'
          developerName = 'Changzhu Zhao'
          developerEmail = 'wenmin92@gmail.com'
      
          licenseName = 'The Apache Software License, Version 2.0'
          licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
          allLicenses = ["Apache-2.0"]
      }
      
    3. 在库的build.gradle文件最后,添加apply:
      apply from: 'https://gist.githubusercontent.com/wenmin92/3583757c2d3377aba7deeb81d28e7eef/raw/8ac8cdfc1b924925d2217b8a050c4810029ad340/maven-publish-new.gradle'
      
    4. 将自己在bintray上的用户名和apikey放到gradle.properties下(可以是全局的,免去每次都要添加):
      bintray.user=wenmin92
      bintray.apikey=****************************************
      
  3. 配置完成,打包上传。
    1. 依次执行buildgeneratePomFileForMyPublicationPublicationbintrayUpload这3个任务即可完成上传。
    2. 在bintray上同步到JCenter。