Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: using resources in a dependency jar as a sourceset

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 ?

like image 509
Stewart Avatar asked Aug 29 '18 15:08

Stewart


People also ask

What is Sourceset in Gradle?

Gradle has the concept of source sets for where your code and test sources live. Some Gradle plugins come with default source sets, for example the Java plugin has a "main" source set where the default location is src/main/java .

What does (*) mean in Gradle dependencies?

Dependencies with the same coordinates that can occur multiple times in the graph are omitted and indicated by an asterisk(*).


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
        }
    }
}
like image 85
John Avatar answered Sep 19 '22 18:09

John