How to publish artifacts on different maven repositories with Gradle?
Asked Answered
O

1

15

I'm using Gradle to publish to a local Artifactory repository. I generate 3 jars during the build: one classic jar with .class, one with javadoc (-javadoc.jar) and one with sources (-sources).

I wand to publish my binary jar and the javadoc jar to a specific repository (a public one) and the sources jar to a private repository.

How can I configure the maven-publish plugin to do that ? I've been able to define multiple repositories, but Gradle tries to push all the jars to all the repositories:

task sourcesJar(type: Zip, dependsOn: classes) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

task javadocJar(type: Zip, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}

publishing {
    repositories {
        maven {
            name "binary"
            credentials {
                username = "${artifactory_user}"
                password = "${artifactory_password}"
            }
            url "http://192.168.2.167:8081/artifactory/libs-snapshot-local"
        }
        maven {
            name "sources"
            credentials {
                username = "${artifactory_user}"
                password = "${artifactory_password}"
            }
            url "http://192.168.2.167:8081/artifactory/sources-snapshot-local"
        }
    }
    publications {
        binary(MavenPublication) {
            // *** I WANT TO USE THE "binary" REPOSITORY HERE ****
            from components.java
            artifact javadocJar
        }
        sources(MavenPublication) {
            // *** I WANT TO USE THE "sources" REPOSITORY HERE ***
            artifact sourcesJar
        }
    }
}

Maybe I'm missing something, but it does not seem to be possible.

Operative answered 8/3, 2016 at 13:44 Comment(1)
Some ideas here: #21433751Mariann
A
4

I had this issue recently and solved as follows. See Gradle documentation for more details

/**
 * Configuration for PublishToMavenRepository according to the repository
 * and publication so that the artifacts are updated to the right repository.
 * */
tasks.withType(PublishToMavenRepository) {
    onlyIf {
        (repository == publishing.repositories.binary &&
                publication == publishing.publications.binaryPublication) ||
                (repository == publishing.repositories.sources &&
                publication == publishing.publications.sourcesPublication)
    }
}
publishing {
    repositories {
        maven {
            name "binary"
            credentials {
                username = "${artifactory_user}"
                password = "${artifactory_password}"
            }
            url "http://192.168.2.167:8081/artifactory/libs-snapshot-local"
        }
        maven {
            name "sources"
            credentials {
                username = "${artifactory_user}"
                password = "${artifactory_password}"
            }
            url "http://192.168.2.167:8081/artifactory/sources-snapshot-local"
        }
    }
    publications {
        binaryPublication(MavenPublication) {
            // *** I WANT TO USE THE "binary" REPOSITORY HERE ****
            from components.java
            artifact javadocJar
        }
        sourcesPublication(MavenPublication) {
            // *** I WANT TO USE THE "sources" REPOSITORY HERE ***
            artifact sourcesJar
        }
    }
}
Allonge answered 7/10, 2022 at 4:33 Comment(1)
Looks pretty much in line with the official suggested solution: docs.gradle.org/current/userguide/…Kemerovo

© 2022 - 2024 — McMap. All rights reserved.