Gradle: using resources in a dependency jar as a sourceset
Asked Answered
S

1

8

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 ?

Sigismondo answered 29/8, 2018 at 15:42 Comment(0)
P
4

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
        }
    }
}
Prey answered 31/8, 2018 at 21:57 Comment(8)
I'm running into 2 errors with this. Possibly I'm just doing something wrong? 1) InvalidUserDataException: Cannot change strategy of configuration ':sourceJar' after it has been resolved. 2) Expected configuration ':sourceJar' to contain exactly one file, however, it contains 18 files.Sigismondo
The source jar may have transitive dependencies. Try adding the "transitive=false". dependencies { sourceJar ('com.group:some-app:1.0.1:sources') { transitive=false }}Prey
transitive=false hasn't fixed it. I can include all your code except the from(zipTree(...)) line. That appears to be what breaks it. But is that referencing something earlier in the config?Sigismondo
Based upon what I've read elsewhere, I tried adding doLast { ...} to the task. It doesn't error, but I think it's also not running at all.Sigismondo
If I explicitly call the task with gradle extractFiles, I don't see anything extracted :(Sigismondo
$build does not exist, it should be $buildDirFlorentinaflorentine
can you show the dependencies of com.group:some-app:1.0.1:sources?Prey
The transitive dependencies are many. I did add { transitive=false }. And the non-source .jar is used as part of the normal build just fine.Sigismondo

© 2022 - 2024 — McMap. All rights reserved.