Custom GroovyCompilation task fails to find the java sources


(Milan) #1

Hi there,
In a project I’m working on I’ve been using both Java and Groovy. The compilation is done by only the Groovy compiler, and it seems to work fine. Now I’m trying to incorporate the QueryDSL library into the project, but the setup I have exhibits a very strange behaviour, which I’ll explain below.

These are the relevant parts of the gradle file:

    sourceSets.main.java.srcDirs = [] // don't compile Java code twice
    sourceSets.main.groovy.srcDirs += ['src/main/java']
    sourceSets.test.java.srcDirs = [] // don't compile Java code twice
    sourceSets.test.groovy.srcDirs += ['src/test/java']
    sourceSets.generated.java.srcDirs = ['src/main/java/generated']

And this is the task that is supposed to generate the QueryDSL classes:

task generateQueryDSL(type: GroovyCompile, group: 'build', description: 'Generates the QueryDSL query types') {
        source = sourceSets.main.groovy
        classpath = configurations.compile + configurations.querydslapt
        options.compilerArgs = [
                "-proc:only",
                "-processor", "org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor"
//                "-processor", "com.mysema.query.apt.QuerydslAnnotationProcessor"
        ]
        destinationDir = sourceSets.generated.java.srcDirs.iterator().next()
    }

And here is the weird part - in this setup, the generation of classes works in gradle 2.4, but not in a newer version - the exceptions below are printed. If I change the processor to the commented one (QuerydslAnnotationProcessor) I the same exceptions are returned no matter the gradle version.

/path/to/project/common/src/main/groovy/com/company/project/model/common/Person.groovy: 3: unable to resolve class com.company.project.interfaces.Searchable
 @ line 3, column 1.
   import com.company.project.interfaces.Searchable
   ^

/path/to/project/common/src/main/groovy/com/company/project/model/common/Person.groovy: 5: unable to resolve class com.company.noah.util.StringUtils
 @ line 5, column 1.
   import com.company.project.util.StringUtils
   ^
...

And a lot more of these errors are printed, and all of them are about Java classes which are referenced from Groovy ones.


(Tom Hartwell) #2

I’m wrestling with a similar problem regarding QueryDSL + Spring Boot, did you make progress on this? I would recommend moving your generated source directory out from your java directory like this

sourceSets.generated.java.srcDirs = ['src/main/generated']

Having generated sources under the java directory seems incorrect and probably throws gradle for a loop.