Gradle: using resources in a dependency jar as a sourceset

Viewed 1483

Suppose I have a build.gradle thus:

dependencies {
    compile 'com.group:some-app:1.0.1'
    compile 'com.group:some-app:1.0.1:sources'
}

sourceSets {
    main {
        other {
             srcDir 'src/main/other'
        }
    }
}

So some-app-1.0.1-sources.jar has source files in it - not Java files, but files from which Java can be generated.

How do I include those files in sourceSets ?

1 Answers

You can extract the files from the source jar on the fly into a directory, and add the directory to a source set.

configurations {
   sourceJar
}

dependencies {
    compile 'com.group:some-app:1.0.1'
    sourceJar 'com.group:some-app:1.0.1:sources'
}

def generatedDir = "$build/other"
task extractFiles(type: Copy) {

    from (zipTree(configurations.sourceJar.singleFile)) 

    into generatedDir 
}

sourceSets {
    main {
        resources {
             srcDir generatedDir
        }
    }
}
Related