Querydsl setting in intellij

Hyunsoo Han·2021년 7월 22일
0

jpa+querydsl

목록 보기
1/1

서두

Querydsl이 세팅된 프로젝트에선 gradle build 시 QClass가 있는 src/main/generated package를 처음 만들면 문제 없이 빌드됐지만, 그 이후 빌드 시, 아래와 같은 에러를 뱉으며 빌드가 되지 않는 문제가 있었다.
(bootRun은 잘 동작하는 문제가 있었다. )

> Task :compileQuerydsl FAILED
...
QManager.java:44: error: cannot find symbol
    public QManager(Path<? extends Manager> path) {
                                   ^
  symbol:   class Manager
  location: class QManager

이 문제는 구글링했을 때, 많은 이들이 같은 고통을 받는 것을 확인했으나, 그렇다할 해결책을 발견하지 못했다. 그래서 나름의 고민으로 해결하게 되어 이렇게 정리한다.
(물론 이 방식이 적절치 않다면, 댓글로 의견을 달아주시면 감사하겠다.)

해결

이전부터 세팅한 build.gradle은 아래와 같다.

configuration {
	...
   querydsl extendsFrom compileClasspath
}

def querydslDirectory = "src/main/generated"
querydsl {
   library = "com.querydsl:querydsl-apt"
   jpa = true
   querydslDefault = true
   querydslSourcesDir = querydslDirectory
}

sourceSets {
   main.java.srcDirs += [ querydslDirectory ]
}

compileQuerydsl {
   options.annotationProcessorPath = configurations.querydsl
}

tasks.withType(JavaCompile) {
   options.generatedSourceOutputDirectory = file(querydslDirectory)
}

clean.doLast {
   delete file(querydslDirectory)
}

유명하신 jojoldu님의 세팅을 그대로 가져와 썼었다. 그러나 위의 것은 re-build하기 전에 gradle clean을 통해 generated package를 제거하지 않는다면, 위 에러가 발생한다.

그래서 생각했다. compileQuerydsl task 이전에 generated package를 있으면 제거하는 작업을 넣기로. 그래서 아래와 같이 바꾸었다.

configuration {
	...
    querydsl extendsFrom compileClasspath
}

def querydslDirectory = "src/main/generated"
querydsl {
    library = "com.querydsl:querydsl-apt"
    jpa = true
    querydslDefault = true
    querydslSourcesDir = querydslDirectory
}

sourceSets {
    main.java.srcDirs += [ querydslDirectory ]
}

compileQuerydsl {
    options.annotationProcessorPath = configurations.querydsl
}

/**
 *  compileQuerydsl.doFirst 추가
 */
compileQuerydsl.doFirst {
	if ( file(querydslDirectory).exists )
    	delete(file(querydslDirectory))
}
/**
 *  compileQuerydsl.doFirst end
 */

tasks.withType(JavaCompile) {
     options.generatedSourceOutputDirectory = file(querydslDirectory)
}

clean.doLast {
    delete file(querydslDirectory)
}

이 후, 빌드할 때마다, compileQuerydsl 작업 전에 generated package를 삭제하고, 다시 컴파일해서 생성하도록 해서 문제는 없어졌다.

왜 빌드할때마다 re-compile이 업데이트가 되지 않는지는 좀 더 연구해봐야 할 것 같다.

참고

profile
한량 개발자

5개의 댓글

comment-user-thumbnail
2021년 7월 25일

감사합니다 웹개발 공부하면서 처음 난관이었는데,, 덕분에 해결하였습니다.

1개의 답글
comment-user-thumbnail
2021년 7월 27일

No such property: exists 이렇게 뜨는데 혹시 해결방안 아시나요..?

1개의 답글