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 ?

Stewart
  • 17,616
  • 8
  • 52
  • 80

1 Answers1

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
        }
    }
}
John
  • 363
  • 1
  • 6
  • 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.` – Stewart Sep 05 '18 at 09:46
  • 1
    The source jar may have transitive dependencies. Try adding the "transitive=false". dependencies { sourceJar ('com.group:some-app:1.0.1:sources') { transitive=false }} – John Sep 05 '18 at 18:18
  • `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? – Stewart Sep 06 '18 at 12:26
  • 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. – Stewart Sep 06 '18 at 12:37
  • If I explicitly call the task with `gradle extractFiles`, I don't see anything extracted :( – Stewart Sep 06 '18 at 13:05
  • 1
    `$build` does not exist, it should be `$buildDir` – ToYonos Sep 06 '18 at 13:07
  • 1
    can you show the dependencies of com.group:some-app:1.0.1:sources? – John Sep 06 '18 at 18:12
  • 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. – Stewart Sep 07 '18 at 04:45