build.gradle
plugins { id 'org.springframework.boot' version '2.6.2' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' id "com.ewerk.gradle.plugins.querydsl" version '1.0.10' } group = 'com.example' version = '0.0.1-SNAPSHOT' configurations { compileOnly { extendsFrom annotationProcessor } } repositories { mavenCentral() } dependencies { implementation( 'org.projectlombok:lombok', 'com.querydsl:querydsl-mongodb:5.0.0', 'com.querydsl:querydsl-apt:5.0.0', 'org.springframework.boot:spring-boot-starter-data-mongodb' ) annotationProcessor( 'org.projectlombok:lombok', 'org.springframework.data:spring-data-mongodb', 'com.querydsl:querydsl-apt:5.0.0', ) testImplementation 'org.springframework.boot:spring-boot-starter-test' } test { useJUnitPlatform() } // ====== QueryDSL 编译生成配置 | begin ====== def querydslDir = "$buildDir/generated/querydsl" println "Q类源码目录: ${querydslDir}" // 指定 spring-data-mongo 注解生成Q类 querydsl.springDataMongo = true // 指定 Q类的源码目录 querydsl.querydslSourcesDir = querydslDir // 将 querydsl生成目标包含进源文件目录 sourceSets.main.java.srcDir(querydsl.querydslSourcesDir) // 插件处理路径配置 configurations.querydsl.extendsFrom(configurations.compileClasspath) compileQuerydsl.options.annotationProcessorPath = configurations.querydsl task deleteQuerydsl(type:Delete){ println "清理 querydsl 目录" delete file(querydslDir) } tasks.withType(JavaCompile){ it.dependsOn('deleteQuerydsl') } // ====== QueryDSL 编译生成配置 | end ======
参考:
https://www.jhipster.tech/cn/tips/003_tip_add_querydsl_support.html
https://github.com/ewerk/gradle-plugins/tree/master/querydsl-plugin
https://beanbroker.github.io/2020/08/16/Spring/mongodb_srping3/