Excluding one or two sub-projects from publication in gradle
Asked Answered
B

5

6

I have a multi-project gradle build and I have applied the maven-publish plugin to all subprojects in my master build file.

I have also defined a default publication there called mavenJava.

This is all fine and now when I run ./gradlew artifactoryPublish I deploy artifacts from all the subprojects.

However, I would like to exclude two of my subprojects from publishing this mavenJava publication.

I've tried a bunch of things such as defining a boolean (skipDefaultPublish) in the ext block in the subprojects block in my master build file and including the publication definition inside a conditional if block and overwriting this variable in the ext block of the subprojects I do not want to publish it. That doesn't work and gradle complains that it Cannot configure the 'publishing' extension after it has been accessed..

I've tried other things but nothing seems to work.

The only thing that I know will work is defining the default publication block in all subprojects except for the ones that I do not want to publish from, but I have ~20 subprojects but only two that should not publish this type of artifact so this doesn't seem like the best thing to do.

So is there any way I can configure all subprojects to publish an artifact but override that in only two of the subprojects so that they don't do that?

UPDATE

To try to clarify what my project looks like.

root build.gradle

subprojects {
    apply plugin: 'maven-publish'

    publishing {
        publications {
            mavenJava(MavenPublication) {
                from components.java

                artifactId = getCustomProjectId()
                groupId = 'com.our.group.id'
            }
        }
    }

    apply plugin: "com.jfrog.artifactory"

    artifactory {
        contextUrl = ourContextUrl
        publish {
            repository {
                repoKey = "ourRepoKey"
                username = "ourArtifactoryUser"
                password = "ourArtifactoryPass"
            }
            defaults {
                publications('mavenJava')
                publishArtifacts = true
                publishPom = true
            }
        }
    }
}

and in a subproject which should publish an artifact, just not the default one:

project(':exsub'){
    publishing {
        publications {
            mavenSrc(MavenPublication) {
                groupId "com.our.group.id"
                artifactId "stuff-src"
                artifact srcStuff
            }
        }
    }

    artifactoryPublish {
        // publications.removeAll() //<- putting this here doesn't work
        // publications.remove('mavenJava') //<- neither does this

        publications ('mavenSrc') //<- this adds mavenSrc to the publication list but does not remove the mavenJava publication from it
    }
}
Bilberry answered 8/4, 2019 at 17:47 Comment(1)
May be useful, though doesn't use Artifactory (I didn't either in my case): https://mcmap.net/q/550599/-gradle-plugin-maven-publish-how-to-publish-only-specific-publication-to-a-repository Can be modified to publish only specific subprojects artifacts based off conditions, rather then different repositories.Lattie
D
7

There are several ways to publish artifacts, depending on used plugins.

To exclude particular module from publishing with modern (Gradle 4.x-7.x) publish like tasks:

tasks.withType(PublishToMavenRepository).configureEach { it.enabled = false }

Legacy uploadArchives:

uploadArchives.enabled = false

With com.jfrog.artifactory plugin:

artifactoryPublish.skip = true

Kotlin syntax (tnx @jaguililla):

tasks.withType<PublishToMavenRepository>().configureEach { enabled = false }
tasks.withType<PublishToMavenLocal>().configureEach { enabled = false }
Daily answered 17/3, 2021 at 11:38 Comment(1)
Thanks for the answer. You could improve it adding the Kotlin DSL syntax: tasks.withType<PublishToMavenRepository>().configureEach { enabled = false } and stating that you may need to skip other publication tasks. I.e.: tasks.withType<PublishToMavenLocal>().configureEach { enabled = false }Mump
B
2

Pheww... I think I finally figured out a way to do this (the aesthetics of the solution are quite debatable).

For me, it was sufficient to put a publishing.publications.remove(publishing.publications.mavenJava) line in the subproject.

However, I had to take care to put that line below the publishing{...} block in that subproject.

So now, the subprojects that should not publish the "default publication" look something like this:

project(':exsub'){
    publishing {
        publications {
            mavenSrc(MavenPublication) {
                groupId "com.our.group.id"
                artifactId "stuff-src"
                artifact srcStuff
            }
        }
    }
    // Remove the default publication (note: this must be located after the publishing block above)
    publishing.publications.remove(publishing.publications.mavenJava)

    artifactoryPublish {
        publications ('mavenSrc') //<- this adds mavenSrc to the publication list but does not remove the mavenJava publication from it
    }
}
Bilberry answered 8/4, 2019 at 19:2 Comment(0)
B
1

Let's consider the following multi-module project structure.

 photos:
   -> photo-client
   -> photo-model
   -> photo-service

In the above multimodule project, If I want to exclude the functionality of the photo-service submodule build jar (any distributive file(jar/war)) from uploading to the configured artifactory, then we have to use the below code snippet in the photo-service's build.gradle file

build.gradle

dependencies {
   .....
}

project.afterEvaluate {
    project.tasks.artifactoryPublish.enabled(false)
}

Broadax answered 8/9, 2020 at 14:47 Comment(0)
T
1

This question is specifically about what to do when some general configuration introduces one publication for all subprojects, but in one specific project you want to remove that "default" publication, but still have another publication. Most of the answers don't address this.

The answer by the original questioner does, but it didn't work for me.

This did:

tasks.withType(PublishToMavenRepository) {
    onlyIf {
        publication != publishing.publications.mavenJava
    }
}

(where mavenJava is the name of the "default" publication to prevent).

This kind of solution is suggested by the Gradle documentation here: https://docs.gradle.org/current/userguide/publishing_customization.html#sec:publishing_maven:conditional_publishing

Tacet answered 16/7, 2022 at 3:37 Comment(0)
V
0

Setting the following on subprojects you want to exclude worked for me:

project.tasks.publish.enabled = false

This does still require the maven-publish plugin to be applied to the project, but no additional configuration is needed.

I found this setting here: https://discuss.gradle.org/t/disable-maven-publish-related-tasks-not-possible/13488

Vories answered 28/11, 2019 at 22:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.