Handle on "sources jar" created by the Java plugin

Hi,

As of Gradle 6 one can create source jars using the Java plugin as follows:

java {
    withSourcesJar()
}

How can I get a handle on the generated sources jar? I wish to copy it to a different folder.

Thank you kindly :slight_smile:

Hi @NS_du_Toit,

There are two ways to do that. If it all happens in the same project, you can use the output of the “sourcesJar” task as input to your copy task:

tasks.create("myCopy", Copy) {
    from(tasks.named("sourcesJar"))
    ....
}

If you want to process the “sources.jar” in another project, you can depend on that project, request the “sources” variant and use that as an input:

configurations {
    sourcesPath {
        extendsFrom(runtimeClasspath)
        canBeConsumed = false
        attributes {
            attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category, Category.DOCUMENTATION))
            attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType, DocsType.SOURCES))
        }
    }
}

dependencies {
    implementation project("other-project")
}

tasks.create("myCopy", Copy) {
    from(configurations.sourcesPath)
}

More details on sharing artifacts between projects: https://docs.gradle.org/6.0.1/userguide/cross_project_publications.html