Android gradle uploadArchives when building

2020-07-08 06:04发布

问题:

I have my uploadArhives to Maven repository .aar publishing.

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'

        }
    }
}

EDIT:

As I think, we can define function:

def uploadToMaven = {
    uploadArchives
}

But how to execute it with every build?

回答1:

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".

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

For example:

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"
        }
    }
}

After every build, uploadArchives will starts.

I tried this solution and it works.

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: 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).

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)

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 :)



回答2:

Just add this line to your build.gradle:

build.finalizedBy(uploadArchives)

This creates a task dependency between build task and uploadArchives task, such that uploadArchives is automatically called everytime build executes successfully.