Gradle 6 Using Built-in javadoc and sources packaging and publishing

Moving to Gradle 6 I wanted to use the new methods for adding sources and javadoc.

This:

java {
    withJavadocJar()
    withSourcesJar()
}

Instead of this:

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

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

artifacts {
    archives jar
    archives sourcesJar
    archives javadocJar
}

However this does not work.

* What went wrong:
Execution failed for task ':publishApiPublicationToMavenRepository'.
> Failed to publish publication 'api' to repository 'maven'
   > Invalid publication 'api': multiple artifacts with the identical extension and classifier ('jar', 'javadoc').

The publication configuration of my API project

publishing {
    repositories {
        maven {
            url buildRelease ? releasesRepoUrl : snapshotsRepoUrl
            credentials {
                username = nexusUsername
                password = nexusPassword
            }
        }
    }

    publications {
        api(MavenPublication) {
            from components.java
            artifact sourcesJar
            artifact javadocJar
        }
    }
}

Not sure if I am missing something? I found some suggestions that I should use different publications, but I am not sure what is the “correct” way of doing it?

    publications {
        api(MavenPublication) {
            from components.java
        }
        sources(MavenPublication) {
            artifact sourcesJar
        }
        javadoc(MavenPublication) {
            artifact javadocJar
        }
    }

I managed to get it working by removing artifact sourcesJar and artifact javadocJar from the publications. These was uploaded to the repository even if not specified within publications.

1 Like

Hi @DJViking,

This is the right solution. :slight_smile: Are there any issues with it?

withJavadocJar() and withSourcesJar() not only add the jar tasks, but also add additional variants with the artifacts to the Java component (components.java). So from components.java adds everything to the publication already. And that’s why you do not need to add them to the publication separately anymore.

Generally, it is now prefered to configure everything through the component, because then the information about which artifacts are published is known when the metadata is generated and can be included there. See also: Customizing publishing

Found no issues with it.

1 Like