uploadArchives构建时

uploadArchives构建时

本文介绍了Android Gradle uploadArchives构建时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将uploadArhives发布到Maven存储库.aar.

I have my uploadArhives to Maven repository .aar publishing.

但是我必须一直从控制台运行gradlew uploadArhives,如何编写代码以使其在每个版本或发行版本中都可以调用?

But I have to run gradlew uploadArhives from the console all the time, how to code to make it invoke with every build or with release build?

uploadArchives {
    repositories {
        mavenDeployer {
            def credentials = [
                    userName: NEXUS_USERNAME,
                    password: NEXUS_PASSWORD
            ]
            repository(url: MAVEN_REPO_URL, authentication: credentials)
            pom.artifactId = 'aaa'
            pom.version = version
            pom.packaging = 'aar'
            pom.groupId = 'bbb'

        }
    }
}

编辑:

我认为,我们可以定义函数:

As I think, we can define function:

def uploadToMaven = {
    uploadArchives
}

但是如何在每次构建时执行它?

But how to execute it with every build?

推荐答案

我有一个复杂的项目,其中包含许多模块和一个主要应用程序.我在其中两个模块上添加了"uploadArchives"(因为是android库).这样,我可以在Maven上发布库,只需从主应用程序运行任务uploadArchives,或使用gradle并将此任务称为"uploadArchives"即可.

I have a complex project with many modules and one main application.I added "uploadArchives" on two of these modules (because are android libraries). In this way I can publish my libraries on Maven simply running the task uploadArchives from my main application, or using gradle and calling this task "uploadArchives".

您可以在您要发布的库的build.gradle的"build.finalizedBy(uploadArchives)"中使用它.

You can use this in your build.gradle (of the library that you want to publish) "build.finalizedBy(uploadArchives)".

例如:

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 23
        versionCode 2
        versionName "2.0"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }


    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    lintOptions {
        abortOnError false
    }


}


build.finalizedBy(uploadArchives)

task wrapper(type: Wrapper) {
    gradleVersion = "2.8"
}

dependencies {
    compile project(':spfshared')
    compile 'com.google.code.gson:gson:2.4'
}

//task for Sonatype Nexus OSS
uploadArchives {
    repositories {
        mavenDeployer {
            repository(
                    url: "${nexusUrl}/content/repositories/releases") {
                authentication(userName: nexusUsername, password: nexusPassword)
            }
            snapshotRepository(
                    url: "${nexusUrl}/content/repositories/snapshots") {
                authentication(userName: nexusUsername, password: nexusPassword)
            }

            pom.version = "2.0.0.1"
            pom.artifactId = "spflib"
            pom.groupId = "it.polimi.spf"
        }
    }
}

每次构建后,uploadArchives将启动.

After every build, uploadArchives will starts.

我尝试了此解决方案,并且有效.

I tried this solution and it works.

我还尝试了一些使用"build.dependsOn myTaskName"的解决方案,但均未成功.如果您愿意,可以尝试,但是在我的AndroidStudio上,第一个解决方案有效.

I also tried some solutions with "build.dependsOn myTaskName" without success. If you want you can try, but on my AndroidStudio, the first solution it works.

PS:我使用命令"gradlew -q build"测试了我的解决方案,并且还从Android Studio的主要模块(这是我的主要应用程序)中专门运行了"build"任务.

PS: I tested my solution using the command "gradlew -q build" and also running specifically the task "build" from my main module in Android Studio (that it's my main application).

如果要在每个发行版中调用"uploadArchives",只需用发行任务替换"build".

If you want to call "uploadArchives" at every release, simply replace "build" with the release task.

更新:我还尝试了以下代码行:

Update:I tried also with these codelines:

defaultTasks 'uploadArchives'

clean.finalizedBy(uploadArchives)
assembleDebug.finalizedBy(uploadArchives)
assembleRelease.finalizedBy(uploadArchives)

但是有时他们会多次调用"uploadArchives",我认为这不是一个好主意.

But sometimes they call "uploadArchives" many times and I think that it's not a good idea.

您要问的是非常具有挑战性的...我试了一个小时:)

What you are asking is very challenging... I tried for an entire hour :)

这篇关于Android Gradle uploadArchives构建时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 20:37