Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android gradle uploadArchives when building

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?

like image 730
Kyryl Zotov Avatar asked Apr 01 '16 11:04

Kyryl Zotov


People also ask

Where are JAR files published in Gradle?

As of Gradle 6.0, the Gradle Module Metadata will always be published alongside the Ivy XML or Maven POM metadata file.

What does Maven publish do?

The Maven Publish Plugin provides the ability to publish build artifacts to an Apache Maven repository. A module published to a Maven repository can be consumed by Maven, Gradle (see Declaring Dependencies) and other tools that understand the Maven repository format.


2 Answers

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

like image 100
Stefano Cappa Avatar answered Sep 20 '22 16:09

Stefano Cappa


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.

like image 33
RaGe Avatar answered Sep 18 '22 16:09

RaGe