在 Gradle 不工作的情况下将 aar 文件发布到 Maven Central [英] Publish an aar file to Maven Central with Gradle not working

查看:28
本文介绍了在 Gradle 不工作的情况下将 aar 文件发布到 Maven Central的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,让我们重复我遵循的所有步骤,以设法使用 Gradle 将 aar 文件发布到 Maven Central"(我主要遵循这个 指南),只是为了确定...

Ok, let's repeat all the steps I followed to manage to "Publish an aar file to Maven Central with Gradle" (I mainly followed this guide), just to be sure...

1) 我使用Android Studio"并且我有这个简单的 android lib,我想在 maven 上使用它:https://github.com/danielemaddaluno/Android-Update-Checker

1) I use "Android Studio" and I have this simple android lib that I would like to be available on maven: https://github.com/danielemaddaluno/Android-Update-Checker

2) 在 UpdateCheckerLib 文件夹中,我有上面提到的 lib 代码.并在此文件夹的 build.gradle 中应用 apply plugin: 'com.android.library' 我在模块目录的 build/outputs/aar/目录中得到一个 .aar 作为输出

2) In the UpdateCheckerLib folder I have the lib code abovementioned. And applying in the build.gradle of this folder apply plugin: 'com.android.library' i got as output an .aar in the build/outputs/aar/ directory of the module's directory

3) 我的第一步是找到一个批准的存储库.我决定使用 Sonatype OSS 存储库.在这里我注册了一个项目,使用 groupid com.github.danielemaddaluno

3) My first step was to find an approved repository. I decided to use the Sonatype OSS Repository. Here I registered a project opening a new Issue (Create --> Create Issue --> Community Support - Open Source Project Repository Hosting --> New Project) with groupid com.github.danielemaddaluno

4) 所以我在我的项目的根目录中添加了一个文件:maven_push.gradle:

4) So I added in the root of my project a file: maven_push.gradle:

apply plugin: 'maven'
apply plugin: 'signing'

def sonatypeRepositoryUrl
if (isReleaseBuild()) {
    println 'RELEASE BUILD'
    sonatypeRepositoryUrl = hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL
            : "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
} else {
    println 'DEBUG BUILD'
    sonatypeRepositoryUrl = hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL
            : "https://oss.sonatype.org/content/repositories/snapshots/"
}

def getRepositoryUsername() {
    return hasProperty('nexusUsername') ? nexusUsername : ""
}

def getRepositoryPassword() {
    return hasProperty('nexusPassword') ? nexusPassword : ""
}

afterEvaluate { project ->
    uploadArchives {
        repositories {
            mavenDeployer {
                beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

                pom.artifactId = POM_ARTIFACT_ID

                repository(url: sonatypeRepositoryUrl) {
                    authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
                }

                pom.project {
                    name POM_NAME
                    packaging POM_PACKAGING
                    description POM_DESCRIPTION
                    url POM_URL

                    scm {
                        url POM_SCM_URL
                        connection POM_SCM_CONNECTION
                        developerConnection POM_SCM_DEV_CONNECTION
                    }

                    licenses {
                        license {
                            name POM_LICENCE_NAME
                            url POM_LICENCE_URL
                            distribution POM_LICENCE_DIST
                        }
                    }

                    developers {
                        developer {
                            id POM_DEVELOPER_ID
                            name POM_DEVELOPER_NAME
                        }
                    }
                }
            }
        }
    }

    signing {
        required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") }
        sign configurations.archives
    }

    task androidJavadocs(type: Javadoc) {
        source = android.sourceSets.main.java.sourceFiles
    }

    task androidJavadocsJar(type: Jar) {
        classifier = 'javadoc'
        //basename = artifact_id
        from androidJavadocs.destinationDir
    }

    task androidSourcesJar(type: Jar) {
        classifier = 'sources'
        //basename = artifact_id
        from android.sourceSets.main.java.sourceFiles
    }

    artifacts {
        //archives packageReleaseJar
        archives androidSourcesJar
        archives androidJavadocsJar
    }
}

6) 我在位于根目录的文件 gradle.properties 中添加了以下几行:

6) I added in the file gradle.properties located in the root the following lines:

VERSION_NAME=1.0.1-SNAPSHOT
VERSION_CODE=2
GROUP=com.github.danielemaddaluno

POM_DESCRIPTION=Android Update Checker
POM_URL=https://github.com/danielemaddaluno/Android-Update-Checker
POM_SCM_URL=https://github.com/danielemaddaluno/Android-Update-Checker
POM_SCM_CONNECTION=scm:git@github.com:danielemaddaluno/Android-Update-Checker.git
POM_SCM_DEV_CONNECTION=scm:git@github.com:danielemaddaluno/Android-Update-Checker.git
POM_LICENCE_NAME=The Apache Software License, Version 2.0
POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
POM_LICENCE_DIST=repo
POM_DEVELOPER_ID=danielemaddaluno
POM_DEVELOPER_NAME=Daniele Maddaluno

7) 在根目录中,我修改了 build.gradle:

7) Inside the root I modified the build.gradle from this:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

为此:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
    }
}

def isReleaseBuild() {
    return version.contains("SNAPSHOT") == false
}

allprojects {
    version = VERSION_NAME
    group = GROUP

    repositories {
        mavenCentral()
    }
}

apply plugin: 'android-reporting'

8) 对于我想上传到中央的每个模块或应用程序,我都读到过,我应该:

8) I read that for each module or application I want to upload to central, I should:

  • 提供一个 gradle.propeties
  • 修改 build.gradle 在最后添加以下行:申请自:'../maven_push.gradle'

所以在 UpdateCheckerLib 文件夹中我:

So in the UpdateCheckerLib folder I:

  • 添加了 gradle.properties:

  • Added a gradle.properties:

POM_NAME=Android Update Checker
POM_ARTIFACT_ID=androidupdatechecker
POM_PACKAGING=aar

  • 修改 build.gradle 在文件底部添加以下行:apply from: '../maven_push.gradle'

    9) 为了给我的文物签名,我做了:

    9) In order to sign my artifacts I did:

    gpg --gen-key
    gpg --list-keys  --> get my PubKeyId...
    gpg --keyserver hkp://pool.sks-keyservers.net --send-keys PubKeyId
    

    10) 我在 ~/.gradle/gradle.properties 路径中添加了一个文件,内容如下(为了获取我使用的密钥 gpg --list-secret-keys):

    10) I added a file to ~/.gradle/gradle.properties path with a content like this (to get the secret key I used gpg --list-secret-keys):

    signing.keyId=xxxxxxx
    signing.password=YourPublicKeyPassword
    signing.secretKeyRingFile=~/.gnupg/secring.gpg
    
    nexusUsername=YourSonatypeJiraUsername
    nexusPassword=YourSonatypeJiraPassword
    

    11) sudo apt-get install gradle 在终端中,因为Andoid Studio"终端无法识别 gradle...

    11) sudo apt-get install gradle in the terminal because "Andoid Studio" teminal didn't recognize gradle...

    12) 最后gradle uploadArchives

    13) 我收到这个错误:

    13) I got this error:

    FAILURE: Build failed with an exception.
    
    * Where: 
    Build file '/home/madx/Documents/Workspace/Android-Update-Checker/UpdateCheckerLib/build.gradle' line: 1
    
    * What went wrong:
    A problem occurred evaluating project ':UpdateCheckerLib'.
    > Could not create plugin of type 'LibraryPlugin'.
    

    可能只是由于 gradle/gradle 插件问题,但我想分享所有过程,以防万一它对其他人有帮助!

    Probably it is simply due a gradle/gradle plugin problem, but I wanted to share all the procedure, just in case it could be helpful for someone else!

    提前致谢!

    非常感谢 JBaruch 和他的回答!所以我试图发布到 jCenter 而不是 Maven Central,因为事实上 jcenter() 是 mavenCentral() 的超集.好的,让我们从我的 github 库 Android-Update-Checker 重新开始.我试图遵循他的一些提示,但我仍然被卡住了......我还将为 jcenter 发布编写我的步骤(希望对某人有用).也许我错过了什么......

    A great thanks goes to JBaruch and his anwer! So I'm trying to publish to jCenter instead of Maven Central, as the matter of fact that jcenter() is a superset of mavenCentral(). Ok let's start again from my github library Android-Update-Checker. I tried to follow some of his tips but I'm still stuck... I'm going to write my steps also for the jcenter publishing (hoping that could be useful to someone). Maybe I'm missing something...

    1) 使用 username 注册到 Bintray:danielemaddaluno

    1) Registered to Bintray with username: danielemaddaluno

    2) 启用上传内容的自动签名:
    来自 Bintray 个人资料网址 --> GPG 签名 --> 复制粘贴您的公钥/私钥.你可以在文件public_key_sender.asc/private_key_sender.asc中分别找到这两个如果您执行以下代码(gpg 中的 -a--armor 选项用于生成 ASCII-armored 密钥对):

    2) Enabling the automatically signing of the uploaded content:
    from Bintray profile url --> GPG Signing --> copy paste your public/private keys. You can find respectively these two in files public_key_sender.asc/private_key_sender.asc if you execute the following code (the -a or --armor option in gpgis used to generate ASCII-armored key pair):

        gpg --gen-key
        gpg -a --export daniele.maddaluno@gmail.com > public_key_sender.asc
        gpg -a --export-secret-key daniele.maddaluno@gmail.com > private_key_sender.asc
    

    2.1) 在同一个 网页 中,您可以从以下位置配置自动签名:存储库 --> Maven --> 勾选GPG 签名自动上传文件" --> 更新

    2.1) In the same web page you can configure the auto-signing from: Repositories --> Maven --> Check the "GPG Sign uploaded files automatically" --> Update

    3) 在同一个网页中,您可以找到您的Bintray API Key代码>(复制以备后用)

    3) In the same web page you can find your Bintray API Key (copy it for a later use)

    4) 在同一个网页中,您可以配置您的Sonatype OSS User代码>(在问题的上一部分中我已经创建了一个用户和一个问题)

    4) In the same web page you can configure your Sonatype OSS User (in the previous section of the question I already created a user and a issue)

    5) 我将这两行添加到根目录中的 build.gradle

    5) I added these two lines to the build.gradle in the root

    classpath 'com.github.dcendents:android-maven-plugin:1.2'
    classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.1"
    

    这样我自己在根目录中的 build.gradle 看起来像:

    So that my own build.gradle in the root looks like:

    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:1.0.0'
            classpath 'com.github.dcendents:android-maven-plugin:1.2'
            classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.1"
        }
    }
    
    allprojects {
        repositories {
            jcenter()
        }
    }
    

    6) 我修改了位于我的项目文件夹中的 build.gradle,它看起来像:

    6) I modified my build.gradle located inside my project folder, and it looks like:

    apply plugin: 'com.android.library'
    apply plugin: 'com.github.dcendents.android-maven'
    apply plugin: "com.jfrog.bintray"
    
    // This is the library version used when deploying the artifact
    version = "1.0.0"
    
    android {
        compileSdkVersion 21
        buildToolsVersion "21.1.2"
    
        defaultConfig {
            //applicationId "com.madx.updatechecker.lib"
            minSdkVersion 8
            targetSdkVersion 21
            versionCode 1
            versionName "1.0.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'org.jsoup:jsoup:1.8.1'
    }
    
    
    def siteUrl = 'https://github.com/danielemaddaluno/Android-Update-Checker'      // Homepage URL of the library
    def gitUrl = 'https://github.com/danielemaddaluno/Android-Update-Checker.git'   // Git repository URL
    group = "com.github.danielemaddaluno.androidupdatechecker"                      // Maven Group ID for the artifact
    
    
    install {
        repositories.mavenInstaller {
            // This generates POM.xml with proper parameters
            pom {
                project {
                    packaging 'aar'
    
                    // Add your description here
                    name 'The project aims to provide a reusable instrument to check asynchronously if exists any newer released update of your app on the Store.'
                    url siteUrl
    
                    // Set your license
                    licenses {
                        license {
                            name 'The Apache Software License, Version 2.0'
                            url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                        }
                    }
                    developers {
                        developer {
                            id 'danielemaddaluno'
                            name 'Daniele Maddaluno'
                            email 'daniele.maddaluno@gmail.com'
                        }
                    }
                    scm {
                        connection gitUrl
                        developerConnection gitUrl
                        url siteUrl
    
                    }
                }
            }
        }
    }
    
    task sourcesJar(type: Jar) {
        from android.sourceSets.main.java.srcDirs
        classifier = 'sources'
    }
    
    task javadoc(type: Javadoc) {
        source = android.sourceSets.main.java.srcDirs
        classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    }
    
    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 = "androidupdatechecker"
            websiteUrl = siteUrl
            vcsUrl = gitUrl
            licenses = ["Apache-2.0"]
            publish = true
        }
    }
    

    7) 我在根 local.properties 文件中添加了以下几行:

    7) I added to the root local.properties file the following lines:

    bintray.user=<your bintray username>
    bintray.apikey=<your bintray API key>
    

    8) 将Android Studio"实际使用的默认 gradle 2.2.1 添加到我的路径中,例如:

    8) Added to my PATH the default gradle 2.2.1 actually used by "Android Studio", for example:

    PATH=$PATH:/etc/android-studio/gradle/gradle-2.2.1/bin
    

    9) 打开Android Studio"终端并执行:

    9) Open "Android Studio" terminal and execute:

    gradle bintrayUpload
    

    10) 从 Bintray --> 我最近的包 --> androidupdatechecker(这里只有在执行前一点 9 之后) --> 添加到 Jcenter --> 勾选框 --> Group Id = "com.github.danielemaddaluno.androidupdatechecker".

    10) From Bintray --> My Recent Packages --> androidupdatechecker (this is here only after the execution of the previous point 9 ) --> Add to Jcenter --> Check the box --> Group Id = "com.github.danielemaddaluno.androidupdatechecker".

    11) 最后我尝试遵循:Bintray --> 我最近的包 --> androidupdatechecker --> Maven Central --> Sync 但我在页面右侧的同步状态"栏中收到此错误:

    11) Finally I tryed to follow: Bintray --> My Recent Packages --> androidupdatechecker --> Maven Central --> Sync but I got this error in the "Sync Status" bar on the right of the page:

    Last Synced: Never
    Last Sync Status: Validation Failed
    Last Sync Errors: 
    Missing Signature: 
    '/com/github/danielemaddaluno/androidupdatechecker/UpdateCheckerLib/1.0.0/UpdateCheckerLib-1.0.0-javadoc.jar.asc' 
    does not exist for 'UpdateCheckerLib-1.0.0-javadoc.jar'. 
    Missing Signature: 
    '/com/github/danielemaddaluno/androidupdatechecker/UpdateCheckerLib/1.0.0/UpdateCheckerLib-1.0.0.aar.asc' 
    does not exist for 'UpdateCheckerLib-1.0.0.aar'. 
    Missing Signature: 
    '/com/github/danielemaddaluno/androidupdatechecker/UpdateCheckerLib/1.0.0/UpdateCheckerLib-1.0.0-sources.jar.asc' 
    does not exist for 'UpdateCheckerLib-1.0.0-sources.jar'. 
    Missing Signature: 
    '/com/github/danielemaddaluno/androidupdatechecker/UpdateCheckerLib/1.0.0/UpdateCheckerLib-1.0.0.pom.asc' 
    does not exist for 'UpdateCheckerLib-1.0.0.pom'. 
    Invalid POM: /com/github/danielemaddaluno/androidupdatechecker/UpdateCheckerLib/1.0.0/UpdateCheckerLib-1.0.0.pom: 
    Project description missing Dropping existing partial staging repository.
    

    推荐答案

    您可以改为将您的 aar 发布到 JCenter.

    You can publish your aar to JCenter instead.

    1. 这是 Android Studio 中的默认设置.
    2. 您可以从那里以更简单的方式同步到 Central.
    3. 它是所以在那里发布要容易得多.
    4. 您可以使用oss.jfrog.组织快照.

    这篇关于在 Gradle 不工作的情况下将 aar 文件发布到 Maven Central的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆