Sources jar making trouble when executing tests


(Robert Kühne) #1

Following (simplified) build.gradle

apply plugin: 'java'
  // two jars from here:
// http://search.maven.org/remotecontent?filepath=org/glassfish/javax.faces/2.1.21/javax.faces-2.1.21.jar
// and here:
// http://search.maven.org/remotecontent?filepath=org/glassfish/javax.faces/2.1.21/javax.faces-2.1.21-sources.jar
dependencies {
 compile fileTree(dir: 'lib', include: '**/*.jar')
}

The lib folder contains two jars from javax.faces. The binary and the sources. Also, in src/test/java/de/rob is a test class (with no useful test):

package de.rob;
  import javax.faces.context.FacesContext;
  public class SourcesTest {
      public void testContext() {
        //FacesContext.getCurrentInstance();
    }
}

If you now do a “gradle test” you get swamped by “cannot find symbol” messages, e.g:

C:\Users\Robert\Documents\gradlesources\lib\javax.faces-2.1.21-sources.jar(javax/faces/component/ComponentStateHelper.java):192: error: cannot find symbol

ValueExpression ve = component.getValueExpression(key.toString());

^

symbol:

class ValueExpression

location: class ComponentStateHelper

Now, for a second test, go into the lib folder and remove the sources jar and repeat “gradle test”. Everything works fine now.

It took me several hours to find the cause of this in a bigger project and I’m not sure if I can prevent sources jars from leaking into libs folders.

Question: Why does gradle get irritated by a sources jar which shouldn’t be touched anyway? Is this correct behaviour?


(Peter Niederwieser) #2

It’s how javac works. It will pick up sources on the class path, even within Jars. Either make sure not to put sources on the class path, or try this:

tasks.withType(JavaCompile) {
    options.compilerArgs << "-sourcepath"
}

The idea is to configure an empty source path, in which case javac should no longer look for sources on the class path.


(Robert Kühne) #3

Yes, that was the cause. Thank you! I had to modify the code a bit because -sourcepath needs a parameter. So this worked for me:

tasks.withType(JavaCompile) {
    options.compilerArgs << "-sourcepath"
    options.compilerArgs << ""
}