Can't compile with source-jar in classpath: empty sourcePath


(Aviv Eyal) #1

Gradle Version: 3.1
Operating System and JVM version: Linux, Java 1.8.0_101 (Oracle Corporation 25.101-b13)
Used to work in 2.3

I think this is a bug/misfeature of the incubating org.gradle.api.tasks.compile.CompileOptions:sourcepath feature.

Having a Java project, with a dependency that’s Sources-jar (ie, a jar file with uncompiled .java files).

In Gradle 2.3, this produces a javac command that looks something like:
javac -cp sources-file.jar file1.java file2.java
which works fine.
With Gradle 3.1, this produces:
javac -sourcepath ......./build/tmp/compileJava/emptySourcePathRef -cp sources-file.jar file1.java file2.java

which makes javac not be able to compile the sources from the provided jar. This is somewhat documented in https://docs.gradle.org/current/dsl/org.gradle.api.tasks.compile.CompileOptions.html#org.gradle.api.tasks.compile.CompileOptions:sourcepath:

The default value for the source path is null, which indicates an empty source path. Note that this is different to the default value for the -sourcepath option for javac, which is to use the value specified by -classpath.

The result is that it’s very hard to use source-jar as dependency, which used to be easy.

The workaround is adding something like this:

compileJava {
  doFirst { task ->
    options.sourcepath = task.classpath
  }
}

Which I suspect might have some un-intended side-effects.

Overall, I don’t understand why the default for gradle’s sourcepath is different from the javacs’ sourcepath option.