Building a JAR file containing all the sources is pretty easy with Gradle. It’s very helpful when publishing artifacts to Maven so developers can have a better insight into what your module is actually doing.

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

When using Gradle Script Kotlin it gets a little harder since sourceSets is not available in the global scope.

To access it you need access to the JavaPluginConvention which can be done with the the (yes, you read that correctly) function!

val sourceJar = task("sourceJar", type = Jar::class) {
    classifier = "sources"

And that’s already it! To publish this with Maven you can configure the Publishing extension e.g. like this:

configure<PublishingExtension> {
    publications {
        create<MavenPublication>("maven") {

Hope this helps you - until next time!