0

I'm working on a repo with a set of releasable libraries each in a module. The idea is each library should be able to be released individually with i.e., ./gradlew upload.

Currently in each module I have the following code to publish it:

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "<url>") {
                authentication(userName: System.getenv('USER_NAME'), password: System.getenv('PASSWORD'))
                pom.groupId = "$groupId"
                pom.artifactId = "$artifactId"
                pom.version = android.defaultConfig.versionName
            }
        }
    }
}

afterEvaluate {
    publishing {
        publications {
            library(MavenPublication) {
                setGroupId "$groupId"
                setArtifactId "$artifactId"
                version android.defaultConfig.versionName

                artifact bundleReleaseAar
            }
        }
    }
}

I'd love to have a way to share those gradle tasks and to avoid repeating them in each module but haven't found a way to do that. One tricky thing probably is that artifactId would be different in each module so assuming I could extract those gradle tasks there should be a way to set artifactId individually.

Can someone shed me some light? Thanks

H.Nguyen
  • 1,621
  • 5
  • 19
  • 31

1 Answers1

0

You can put all of your code into a simple build.gradle and use apply from: to apply to multiple build.gradle files.

For instance, I have this compile.gradle script that I use in many places: kotlin/build.gradle and java/build.gradle.

Instead of writing your own "publishing" build.gradle, I would suggest you a popular script that is open source and readily available, gradle-mvn-push.gradle.

Here is an example setup for your project:

root
 |
 | gradle
 | --- gradle-mvn-mpp-push.gradle
 |
 | - module a
 | --- build.gradle - apply from: rootProject.file("gradle/gradle-mvn-mpp-push.gradle")
 |
 | - module b
 | --- build.gradle - apply from: rootProject.file("gradle/gradle-mvn-mpp-push.gradle")
 |

See the docs here: https://docs.gradle.org/current/userguide/plugins.html#sec:script_plugins

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185