2

Gradle version: 5.1

Java version: 11

I have the following task defined in gradle file to generate QueryDSL classes:

task generateQClasses (type: JavaCompile) {
    source = sourceSets.main.java.srcDirs
    classpath = sourceSets.main.compileClasspath
    destinationDir = file('src/main/java')
    options.annotationProcessorPath = configurations.annotationProcessor
    options.compilerArgs = ['-proc:only', '-processor', 'com.querydsl.apt.jpa.JPAAnnotationProcessor', '-Aquerydsl.packageSuffix=.querydsl']
}

Below is my dependencies block:

annotationProcessor "com.querydsl:querydsl-apt:4.2.1:jpa"
annotationProcessor "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final"
annotationProcessor "javax.annotation:javax.annotation-api:1.3.2"

If I execute `gradlew generateQClasses, I see the following in the logs:

Attempt to recreate a file for type foo.bar.QClass
error: Attempt to recreate a file for type foo.bar.QClass

It fails with error saying the file already exists. How can I configure this task to overwrite the files if they exist?

Also, the above config is the config for the root project and it has 5 subprojects. AnnotationProcessor is able to overwrite the files in one of the sub project but not the others (all the subprojects have the same config). Am I missing anything?

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • Is your goal to move the generated source code out of the build folder, or is it to only generate them when you run your custom task (instead of being part of the normal compile task), or both? – Bjørn Vester Jun 11 '20 at 12:16
  • It's only to generate them when someone executes that task explicitly or when the code is compiled. I do not want to move the generated source code out of the build folder. – Darshan Mehta Jun 11 '20 at 12:57
  • Are you saying you like to have them generated when running `compileJava` but also `generateQClasses`? Isn't it easier to just rely on the former, or at least only one of them? Sorry if I am being a bit dense :) – Bjørn Vester Jun 11 '20 at 14:07
  • I only have this task as a separate because it requires different `annotationProcessorPath` and `compilerArgs` values compared to `compileJava`. – Darshan Mehta Jun 11 '20 at 14:23
  • for reference: https://stackoverflow.com/questions/54134455/java-11-querydsl-4-gradle-5-springboot-2-1-not-generating-qclasses/58375764?noredirect=1#comment110277657_58375764 – TecHunter Jun 12 '20 at 22:27

1 Answers1

0

I had the same behavior here... It happens when the generated sources folder isn't empty. Try to execute with gradle clean before. Then will be OK. But I'm trying to replace the default QueryDSL task, with the task that you've done. If I have success, I'll update the answer here.

rios0rios0
  • 735
  • 7
  • 20
  • @Darshan Mehta I did some question about it: https://stackoverflow.com/questions/69885572/querydsl-with-gradle-configuration-of-annotation-processor. – rios0rios0 Nov 08 '21 at 16:17