Configure Gradle to publish sources and javadoc

Solution as of Gradle 6.0

Here’s the somewhat minimal configuration you can use if you’re on Gradle 6.0 or later; note the newly introduced withSourcesJar() and withJavadocJar() methods:

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

group = 'com.example'

java {
    withSourcesJar()
    withJavadocJar()
}

publishing {
    repositories {
        maven {
            url="file:///tmp/my-repo"
        }
    }
    publications {
        myJava(MavenPublication) {
           from components.java
       }
    }
}

Of course, you can also use the ivy-publish plugin instead of maven-publish.

See also the Gradle docs:

Leave a Comment