Gradle uploadArchives artifacts namespace when depending on a plugin

1.2k Views Asked by At

I have a Android app building with Gradle. I am using the gradle-release plugin to create automatic releases, and then the uploadArchives task from Gradle to upload the generated .apk to a Maven repo (Nexus).

I have to add the archives to upload dynamically at runtime, because my build is using custom Android flavors. Everything works fine when I run the uploadArchives from the command line:

variant.outputs.each { output ->
    def apkFile = output.outputFile

    tasks."assemble${capitalizedVariantName}" << {
        artifacts.archives [file: apkFile, classifier: variant.baseName]
    }
}

uploadArchives {
    repositories {
        mavenDeployer {
            pom.groupId = PROJECT_GROUP
            pom.artifactId = PROJECT_NAME
        }
    }
}

Then I run:

./gradlew assembleFlavorNameRelease uploadArchives

And the .apk is correctly uploaded to Nexus.

I have the need to run the uploadArchives task BEFORE the release plugin automatically changes the version name of the project and commit. Basically:

- current version: 0.1.0-SNAPSHOT
- run release
- version becomes: 0.1.0
- build (build task)
- upload this build to Nexus (uploadArchives task)
- update the version to: 0.1.1-SNAPSHOT (updateVersion task)

To achieve this, what I have done is to have the updateVersion task of the gradle-release plugin depending on the uploadArchives

updateVersion.dependsOn uploadArchives

Well, when I do this, the artifacts.archives. is empty, so no upload.

I suspect that, maybe, since I add the uploadArchives task as dependency of a task of the release plugin, then the "namespace" is different, so basically the uploadArchives task does not use the "same instance" of artifacts.archives, filled during the build.

1

There are 1 best solutions below

0
On BEST ANSWER

updateVersion.dependsOn uploadArchives

If you do that then you end up calling uploadArchives in the same process as the release is done, but not in the same process the build is done. To get the version right for the build task itself, the release plugin spawns a new gradle build which runs the build with the correct version number. (This is done because a lot of other plugins like maven-publish are not able to pickup on a changed project version during runtime)

If you want to execute tasks in the same process as the build you need to either use the tasks beforeReleaseBuild or afterReleaseBuild to depend on. Both of them are run in the same process.

So in your case it would be

afterReleaseBuild.dependsOn uploadArchives

This runs the uploadArchives directly after the build has finished with the release version.

For a better understanding I adapted your taskgraph:

- current version: 0.1.0-SNAPSHOT
- run release
- version becomes: 0.1.0 (and is written to gradle.properties)
- spawn new gradle build
    - build (build task)
    - upload this build to Nexus (uploadArchives task)
- update the version to: 0.1.1-SNAPSHOT (updateVersion task)