Add Source JARS to Published Archives with Gradle - kercheval/GradleCMPlugin GitHub Wiki

Many projects like to publish the source files that were compiled to generate a particular artifact. This allows IDEs to debug into dependent code when working on other projects and is a very convenient source of information.

Using GitHub and the tags associated with a release, you can always obtain the entire source state from the repository for a particular build, but the inclusion of a source JAR simplifies things quite a bit.

This example is a very simple way to accomplish this

allprojects {
    //
    // Add source jars to the build
    //
    task sourcesJar(type: Jar, dependsOn: classes) {
        classifier = 'sources'
        from sourceSets.main.allSource
    }

    //
    // Add the new source jars to the artifacts to be published
    //
    artifacts {
        archives sourcesJar
    }
}