Prevent Java plugin from compiling code in jar file

We have jar files as compile dependencies for a Java project. These jar files also contain source code. The compileJava tasks attempts to compile these sources and fails, since there are some legacy dependencies no longer present in the project. If we delete the sources and just have a “.class file only” jar, the compilation of the actual project succeeds.

Is there a way to disable the compilation of the sources in the jar file?

To prevent this from happening, you’ll need to set a javac flag (‘compileJava.options.compilerArgs = […]’). From what I remember, it’s ‘“implicit:none”’, but better double-check the javac docs.

I tried this, but it did not work:

tasks.withType(JavaCompile) {
    options.compilerArgs << '-implicit:none'
}

However, this did the trick:

tasks.withType(JavaCompile) {
    options.compilerArgs += ['-sourcepath', '']
}

Found it here: http://forums.gradle.org/gradle/topics/compilation_fails_when_i_use_guavas_optional_class_duplicate_class_com_google_common_collect_abstractiterator