173
votes

I am working with an open source project that is built with gradle. I would like to generate a (project)-sources.jar file that I can load into my IDE (IntelliJ IDEA) and debug through the project. I know how to load the file if I can generate it.

I have looked through the available gradle tasks for the project and there isn't one that generates a sources jar file.

What is the easiest way to generate a sources jar file for this project?

Adding the source into the jar file that contains the compiled class files would be fine as well.

7

7 Answers

255
votes
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 sourcesJar
    archives javadocJar
}
55
votes

If you're using Android:

task sourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier = 'sources'
}

task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

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

artifacts {
    archives javadocJar
    archives sourcesJar
}

from here

46
votes

Solution as of Gradle 6.0

Assuming that you use the java/java-library plugin with Gradle 6.0 or later, you can get a sourcesJar task using the following configuration:

java {
    withSourcesJar()
    // and/or analogously use “withJavadocJar()” to get a “javadocJar” task
}

If you additionally use the maven-publish/ivy-publish plugin (recommended nowadays), then this will also publish a *-sources.jar artifact along with your main Java publication.

See also the Gradle docs.

39
votes

If you wish to add the sources to the compiled classes JAR file, which you also said would be acceptable, you can do that easily enough. Just add the following to your build file. You can see that, in theory, it is quite like the solution for putting sources into a separate JAR:

 jar {
     from sourceSets.main.allSource
 }

The difference is that you are adding it to the main JAR file by saying "jar" in lieu of sourcesJar.

11
votes

this should work

assemble.dependsOn 'propertyJar'

task propertyJar(type: Jar) {
 archiveName = "myJarName.jar"
 from('src/main/resources') {
    include '**'
 }}
8
votes

When using:

  • Gradle: 5+
  • java and maven-publish plugins
task sourceJar(type: Jar) {
    from sourceSets.main.allJava
    archiveClassifier = "sources"
}

publishing {
    publications {
        maven(MavenPublication) {
            from components.java
            artifact sourceJar
        }
    }
}

https://docs.gradle.org/current/dsl/org.gradle.api.publish.maven.MavenPublication.html

6
votes

The Kotlin DSL equivalent would be:

tasks {

    val sourcesJar by creating(Jar::class) {
        dependsOn(JavaPlugin.CLASSES_TASK_NAME)
        classifier = "sources"
        from(java.sourceSets["main"].allSource)
    }

    val javadocJar by creating(Jar::class) {
        dependsOn(JavaPlugin.JAVADOC_TASK_NAME)
        classifier = "javadoc"
        from(java.docsDir)
    }

    artifacts {
        add("archives", sourcesJar)
        add("archives", javadocJar)
    }
}