Gradle發布項目到 maven 之gradle-bintray-plugin(2)


上傳的方式有兩種,第一種是通過 bintray 官方出的插件

bintray/gradle-bintray-plugin

第二種是一個國外組織開源的插件

novoda/bintray-release

 

這里講bintray/gradle-bintray-plugin

   1.在項目根目錄下的 build.gradle 添加插件依賴

 

dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3' classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
    }

在項目根目錄下的 gradle.properties 中配置我們的開源庫信息

PROJ_NAME=test-lib
PROJ_GROUP=com.test.lib
PROJ_ARTIFACTID=test-lib
PROJ_VERSION=0.1.1
PROJ_WEBSITEURL=https://github.com/youproject_url
PROJ_ISSUETRACKERURL=https://github.com/youproject_url/issues
PROJ_VCSURL=https://github.com/youproject_url/xxxx.git
PROJ_DESCRIPTION=project description

DEVELOPER_ID=xxx_id
DEVELOPER_NAME=you accout name
DEVELOPER_EMAIL=you email@gmail.com
2.在項目根目錄下的 local.properties 中填寫我們的 userAPI key,這里的內容是完全可以放在 gradle.properties 中的,但是通常我們的開源庫都會發到 Github 的公共倉庫中,如果這樣的話那我們的 API key 就會暴露給其他人,那當然是不行的,所以我們就在 git 忽略的 local.properties 中配置我們的 API key。
BINTRAY_USER=werbhelius
BINTRAY_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

3.在我們開源庫的目錄下,新建一個 bintray.gradle 文件,用於上傳開源庫以及配置發布的文件內容包括源碼,文檔以及 AAR。

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



group = PROJ_GROUP
version = PROJ_VERSION
project.archivesBaseName = PROJ_ARTIFACTID



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

task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += configurations.compile
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

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

javadoc {
    options{
        encoding "UTF-8"
        charSet 'UTF-8'
        author true
        version true
        links "https://androiddoc.qiniudn.com/reference/"
        title "$PROJ_NAME $PROJ_VERSION"
    }
}

artifacts {
    archives sourcesJar
    archives javadocJar
}

install {
    repositories.mavenInstaller {
        pom.project {
            name PROJ_NAME
            description PROJ_DESCRIPTION //project description
            url PROJ_WEBSITEURL //project weiste url

            packaging 'aar'
            groupId PROJ_GROUP  //group
            artifactId PROJ_ARTIFACTID //artifactId
            version PROJ_VERSION  //version

            licenses {
                license {
                    name 'The Apache Software License, Version 2.0'
                    url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    distribution 'repo'
                }
            }
            scm {
                connection PROJ_VCSURL
                url PROJ_WEBSITEURL

            }
            developers {
                developer {
                    id DEVELOPER_ID
                    name DEVELOPER_NAME
                    email DEVELOPER_EMAIL
                }
            }
        }
    }
}

bintray {

    Properties properties = new Properties()
    properties.load(project.rootProject.file('local.properties').newDataInputStream())
    user = properties.getProperty('BINTRAY_USER')
    key = properties.getProperty('BINTRAY_KEY')
    configurations = ['archives']

    dryRun = false
    publish = true

    pkg {
        repo = 'maven'
        name = PROJ_NAME
        licenses = ['Apache-2.0']
        vcsUrl = PROJ_VCSURL
        websiteUrl = PROJ_WEBSITEURL
        issueTrackerUrl = PROJ_ISSUETRACKERURL
        publicDownloadNumbers = true
        version {
            name = PROJ_VERSION
            desc = PROJ_DESCRIPTION
            vcsTag = PROJ_VERSION

            gpg {
                sign = true
            }
        }
    }
}

4.在我們開源庫中的 build.gradle 文件中引入 bintary.gradle ,注意引入的命令需要寫在最后一行,不然會報錯。

 

apply plugin: 'com.android.library'
android {
    compileSdkVersion 28
    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
}
apply from: './bintray.gradle'

5.當上述的配置全部完成之后,我們可以上傳我們的開源庫了,打開 Terminal,執行 ./gradlew install,執行完成后,打開 build 目錄你會發現生成了 aar 包、javadoc 文檔、sources 源碼以及上傳 maven 需要的 pom 配置文件。

生成上述文件后,繼續在 Terminal 中執行 ./gradlew bintrayUpload 提示成功之后,我們的開源庫就發布成功啦。

 也可以直接通過

點擊之后顯示成功

 

發布成功之后,打開之前 bintray 網頁,你會發現在之前我們創建的 maven 倉庫中,已經有我們剛剛發布的庫了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM