Mixed Groovy + Java project is refusing to compile


(Ben Fagin) #1

Hello all. I have been trying to get my mixed groovy and java project to compile without much success. Whenever I compile I am getting a NoClassDefFoundError for my Java classes. This is curious because the error would seem to indicate that the joint compilation isn’t processing Java files first.

I suspect that my source sets are not configured properly. My project has mixed groovy and java in src/main, src/test, and src/it, with three source sets configured for those. When I run gradle compileIntegrationTestGroovy is when I get the error. The class it complains about not finding is a java class inside of the src/it/java directory.

Why would the compilation ever fail to find a class it is supposed to be compiling? I am using gradle 1.8, groovy 2.2.2. Build script is below. Thanks!

sourceSets {
    main {
        java {
            srcDirs = []
        }
        groovy {
            srcDirs = ['src/main/java']
        }
        resources {
            exclude 'properties/spring.properties'
        }
    }
    test {
        java {
            srcDirs = []
        }
        groovy {
            srcDirs = ['src/test/java']
        }
    }
    integrationTest {
        compileClasspath += sourceSets.test.compileClasspath + sourceSets.test.output
        runtimeClasspath += output + sourceSets.test.runtimeClasspath + sourceSets.test.output
          java {
            srcDirs = []
        }
        groovy {
            srcDirs = ['src/it/java']
        }
        resources {
            srcDirs = ['src/it/resources']
        }
    }
}

(Benjamin Muschko) #2

I believe your integration test compile classpath also needs the compiled classes from your main SourceSet. Try adding ‘sourceSets.main.output’ to ‘integrationTest.compileClasspath’.


(Ben Fagin) #3

Tried this, but no dice. The class that it is complaining about is actually in the integration compile source, under src/it/java.


(Benjamin Muschko) #4

I guess I’d need to try it out myself. Could you prepare a minimal example on GitHub?


(Ben Fagin) #5

So I broke my project down in to the minimal form by deleting a few files at a time, but still it was erroring out. As a last ditch effort I trashed the system-wide .gradle folder. And then it worked (naturally). I have no idea what the issue was, but it looks like a case of over-eager caching. Anyway I’m glad to have this working finally.

As part of the debugging process I created a stub project which exercises a few different inheritance patterns and acts as a sanity check for the joint compilation. Perhaps it can be of use to others in the future. https://github.com/UnquietCode/Groovy-Java-Gradle

Thanks, Ben


(Ben Fagin) #6

Looks like it was something else. See my response below.