gralde生成Q类 idea
1. 生成Q类:
IDEA ->File-> Settings-> Build, Execution, Deployment->Compiler->Annotation Processors
选择右侧的Enable annotation processing(右侧第一行)
选择Module content root(右侧第4行)
点击Apply
点击OK
2. 如果不生效,则copy如下部分到build.gradle中,然后鼠标移到 runAPT 方法的内部,右键,Run 'projectName[runAPT]'
生成Q类后,在generaed文件夹上,右键Mark Directory as -> Generated Sources Root 即可
ext {
querydslGeneratedSourcesDir = file("$projectDir/src/main/generated")
}
//单独运行QueryDSL的APT来生成Q源代码
task runAPT(type: JavaCompile) {
doFirst {
/*
* This is a workaround to delete the file that will be created by the annotation processor if it already exists.
* There is a known bug in the Java compiler and JDK 8 which should be fixed at JDK 9.
* http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8067747 <-- Master report
* http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8146348 <-- duplicates master report
*/
if ( querydslGeneratedSourcesDir.exists() ) {
FileCollection collection = files { querydslGeneratedSourcesDir.listFiles() }
collection.each { delete it }
}
querydslGeneratedSourcesDir.mkdirs()
println "classpath:"+classpath.getFiles()
println "Generate QueryDsl CLass..."
println "options:"+options
println "options.compilerArgs:"+options.compilerArgs
println "AnnotationProcessorPath for $name is ${options.getAnnotationProcessorPath().getFiles()}"
}
source = sourceSets.main.java.srcDirs
destinationDir = sourceSets.main.java.outputDir
classpath = configurations.compileClasspath + configurations.annotationProcessor
options.annotationProcessorPath = classpath //@wjw_note: 设置这个annotationProcessorPath很关键
options.encoding = 'UTF-8'
options.deprecation = true
options.compilerArgs += ["-parameters","-Xlint:deprecation"]
options.compilerArgs += ['-proc:only']
options.compilerArgs += ['-processor', 'com.querydsl.apt.jpa.JPAAnnotationProcessor']
options.compilerArgs += ['-s', querydslGeneratedSourcesDir] //设定生成的Q类存放的位置
options.compilerArgs += ["-Aquerydsl.excludedPackages=xyz.erupt.upms,xyz.erupt.bi"] //设定QueryDsl要排除的包列表(逗号来分割)
}
其中runAPT来自: https://blog.csdn.net/wjw465150/article/details/126669946