Gradle publish sources jar and binary jar


I am trying to publish both a binary jar and a separate sources jar to Nexus. I follow the example from

So I have added a task:

task sourceJar(type: Jar) {
    from sourceSets.main.allJava
  // usage: gradle publish
publishing {
    publications {
        distributable(MavenPublication) {
            artifact jar
        sources(MavenPublication) {
            artifact sourceJar {
                classifier "sources"

When I run gradle jar sourcesJar, I get two jars in my build forlder which seem fine.

However, when ruinning gradle publish, gradle creates jars with a different naming strategy, it seems. And this causes

:publishDistributablePublicationToNexusReleaseRepositoryUploading: de/example/example/0.1.10/example-example-0.1.10.jar to repository remote at
Transferring 2054K from remote
Uploaded 2054K
:publishSourcesPublicationToNexusReleaseRepositoryUploading: de/example/example-example/0.1.10/example-example-0.1.10.jar to repository remote at
  FAILURE: Build failed with an exception.
  * What went wrong:
Execution failed for task ':publishSourcesPublicationToNexusReleaseRepository'.
> Failed to publish publication 'sources' to repository 'nexusRelease'
   > Error deploying artifact 'de.example:example-example:jar': Error deploying artifact: Failed to transfer file: Return code is: 400

What I observe is that in the second case, there is no more suffix “-sources” in the jar filename, and I suspect that’s what causes the problem here. This was using gradle 1.12.

Any Ideas?

Why did you declare separate publications for main Jar and sources Jar? Typically you’d combine them into a single publication. Actually, your ‘sources’ publication does just that.

I thought to publish 2 jars, I need 2 publications. Obviously that was a wrong assumption. Not sure which part of the docs would have helped me. Using just one publication with 2 artifacts seems to work fine. Thanks.