Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple publications with coordinates warning when running publishToMavenLocal for a gradle plugin artifact #10384

Closed
embee1981 opened this issue Aug 28, 2019 · 10 comments

Comments

@embee1981
Copy link

Expected Behavior

No warning should be displayed when publishing a gradle plugin.

Current Behavior

Using gradle 5.6.1 the following is output:

Task :publishPluginMavenPublicationToMavenLocal
Multiple publications with coordinates 'blah:com.blah:1.0-SNAPSHOT' are published to repository 'mavenLocal'. The publications will overwrite each other!

Context

In version 5.5.1, no warning was displayed.

5.6. an error occurs

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':publishPluginMavenPublicationToMavenLocal'.
> Cannot publish multiple publications with coordinates 'blah:com.blah:1.0-SNAPSHOT' to repository 'mavenLocal'

5.6.1 shows a warning now instead of an error which implies something "bad" will happen which doesn't seem to be the case.

> Task :publishPluginMavenPublicationToMavenLocal
Multiple publications with coordinates 'blah:com.blah:1.0-SNAPSHOT' are published to repository 'mavenLocal'. The publications will overwrite each other!

Steps to Reproduce

Use the following build.gradle file

plugins {
    id 'java-gradle-plugin'
    id 'maven-publish'
}

group 'blah'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8

task sourcesJar(type: Jar) {
    from sourceSets.main.allJava
    archiveClassifier = 'sources'
}

gradlePlugin {
    plugins {
        blah {
            id = "com.blah"
            implementationClass = "com.blah.BlahPlugin"
        }
    }
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
            artifact sourcesJar
        }
    }
}

wrapper {
    gradleVersion = '5.6.1'
    distributionType = Wrapper.DistributionType.ALL
}

Run gradle clean publishToMavenLocal

Your Environment

Gradle 5.6.1

@jjohannes
Copy link
Contributor

@embee1981 thanks for bringing this up. What you are doing seems to work ok, but there is still the potential that it may cause issues if you do more modifications to the publication. That's why we introduced the warning.

However, the example shows that the warning should carry more context. In particular to help you to get rid of it. Because in this case one of the publications is contributed by the java-gradle-plugin, it is less obvious where the duplication is.

You can change the publishing { } block in your script as follows:

publishing {
    publications {
        pluginMaven(MavenPublication) {
            artifact sourcesJar
        }
    }
}

By referring pluginMaven, you extend the publication that has been contributed by the plugin instead of creating a second one. (If this publication name is not documented anywhere, that's another shortcoming we should address.)

Now, there is no need to redeclare from components.java, which was already done by the plugin.

@embee1981
Copy link
Author

@jjohannes thanks for that, that tweak did the job, no more warning output :-)
This would be good to document somewhere as I'm pretty sure the code I pasted in was copied from the gradle plugin examples section on publishing your own plugin.

@jjohannes jjohannes removed the a:regression This used to work label Sep 19, 2019
martijndwars added a commit to martijndwars/spoofax-gradle-plugin that referenced this issue Sep 24, 2019
@jjohannes
Copy link
Contributor

@embee1981 I couldn't find any wrong documentation specifically for plugin publishing. If you should know about anything, please let me know.

But I can see that the sources jar publishing is a common use case for adjusting the publication directly. And if you just copy and paste that from the current documentation about publishing Java, you can easily run into this.

Gradle 6 will support publishing of sources (and Javadoc) out-of-the-box. The sample code in the updated documentation can now be "copy-pasted" independent of the publication name: https://docs.gradle.org/release-nightly/userguide/building_java_projects.html#sec:java_packaging

@asarkar
Copy link

asarkar commented Jul 24, 2020

@jjohannes I'm using Kotlin DSL, and get the same warning. It's not clear to me how to extend the pluginMaven publication. The complete example doesn't seem so complete after all.

publishing {
    publications {
        create<MavenPublication>("mavenJava") {
            pom {
              // bunch of stuff
            }
        }
    }
 }

@jjohannes
Copy link
Contributor

Hi @asarkar. You need to use the name publication name pluginMaven (because that is introduced by the java-gradle-plugin).

For Kotlin DSL it should be:

publishing {
    publications {
        create<MavenPublication>("pluginMaven") {
            pom {
              // bunch of stuff
            }
        }
    }
 }

@TWiStErRob
Copy link
Contributor

The built-in pluginMaven publication can be disabled with:

  • From build.gradle:
    gradlePlugin { automatedPublishing = false }
  • From buildSrc:
    project.plugins.withId("java-gradle-plugin") { // only do it if it's actually applied
    	project.configure<GradlePluginDevelopmentExtension> {
    		isAutomatedPublishing = false
    	}
    }

This disables adding the publication in MavenPluginPublishPlugin.

Tried and tested in Gradle 5.6.4

@asarkar
Copy link

asarkar commented Oct 14, 2022

Sorry to resurrect a closed thread, but in my case, I want to publish a regular artifact and a uber jar, and get the same warning. This situation is not documented in the Gradle public docs (not a multimodule project).

My questions:

  1. What is the correct way to do this?
  2. How can I attach sources and Javadoc (same ones) to both publications, a requirement for Maven central?
publishing {
    publications {
        mavenJava(MavenPublication) {
            artifactId = "${project.name}"
            from components.java

            pom {
                ...
            }
        }
        shadow(MavenPublication) { publication ->
            project.shadow.component(publication)

            pom {
                ...
            }
        }
    }
}

@TWiStErRob
Copy link
Contributor

TWiStErRob commented Oct 15, 2022

  1. isn't the problem that both your publications have the same coordinates, you might want to change the artifactId for the shadow publication to be different (artifactId = "${project.name}-fat"). I haven't done this, but it feels like that would work.

  2. It's described in the OP of this issue. If the automated publishing is off, then that shows the way and won't fail.
    You can a full integrated example here:
    https://github.com/TWiStErRob/net.twisterrob.gradle/blob/cc11555e3ac274c8359aa2085a25a12b6e624923/gradle/plugins/src/main/kotlin/net/twisterrob/gradle/build/PublishingPlugin.kt#L112-L113

@jjohannes
Copy link
Contributor

If you don't want the shadow version to have different coordinates, the best solution is probably not to have a second publication, but add the shadow Jar as a addditional variant to the components.java. Something like:

def shadowElements = configurations.create('shadowElements') {
  canBeConsumed = true
  canBeResolvedFalse = false
  attributes {
      attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category, Category.LIBRARY))
      attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage, Usage.JAVA_RUNTIME))
      attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling, Bundling.SHADOWED))
  }
  outgoing.artifact(tasks.named('shadowJar'))
}

components.java.addVariantsFromConfiguration(shadowElements) {  }

The shadow Jar will then be available with a classifier in the name and will be listed in the Gradle Metadata (.module file).

In you current setup the metadata is "broken", because it is generated two times and one overrides the other. That's why you get the warning.

See:

@asarkar
Copy link

asarkar commented Oct 15, 2022

Thank you for your responses. The library in question is currently only packaged as a shadow jar. My intent is to publish the “thin” jar to Maven Central as usual with source and Javadoc, but also make the shadow jar available for existing users. It sounds like adding a variant is the best way to approach that. I’ll try it out and report back.

syall added a commit to smithy-lang/smithy-gradle-plugin that referenced this issue Nov 7, 2022
Due to the unreliability of jcenter, use the latest version of
the com.gradle.plugin-publish plugin.

After upgrading, there were warning messages that artifacts were
being published to Maven Local multiple times, so removed the
redundant source / docs jar artifacts.

References:

- gradle/gradle#10384
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants