如何使QueryDSL和Lombok协同工作

时间:2017-06-13 13:09:07

标签: java maven jpa querydsl lombok

当使用Lombok注释注释方法或变量时,maven插件会通过处理JPA的源生成来抱怨。

我在控制台日志中遇到了这种失败:

symbol:   class __
location: class ServiceBaseMessage
C:\workspaces\[...]\service\ServiceBaseMessage.java:44: error: cannot find symbol
@Getter(onMethod = @__({ @JsonProperty("TYPE") }))

如何使用JPA注释的apt-maven-plugin和queryDSL处理器与lombok注释一起工作?

4 个答案:

答案 0 :(得分:14)

这个解决方案对我有用。 在apt-maven-plugin配置中添加lombok.launch.AnnotationProcessorHider$AnnotationProcessor

<plugin>
    <groupId>com.mysema.maven</groupId>
    <artifactId>apt-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>process</goal>
            </goals>
            <configuration>
                <outputDirectory>target/generated-sources/java</outputDirectory>
                <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor,lombok.launch.AnnotationProcessorHider$AnnotationProcessor</processor>
            </configuration>
        </execution>
    </executions>
</plugin>

似乎也是以gradle的方式工作: 见https://github.com/ewerk/gradle-plugins/issues/59#issuecomment-247047011

答案 1 :(得分:5)

以下是 GRADLE 用户的语法(macen用户请查看其他答案)

//这会将lombok正确添加到您的项目中,然后配置jpa处理器

plugins {
 ...
  id 'io.franzbecker.gradle-lombok' version '1.7'
}
project.afterEvaluate {

  project.tasks.compileQuerydsl.options.compilerArgs = [
          "-proc:only",
          "-processor", project.querydsl.processors() +
                  ',lombok.launch.AnnotationProcessorHider$AnnotationProcessor'
  ]
}

这是一个完整的工作版本 查询dsl和lombock由插件导入,不需要声明依赖项。

buildscript {
    repositories {
        mavenCentral()
    }
}

plugins {
    id 'io.franzbecker.gradle-lombok' version '1.7'
    id "com.ewerk.gradle.plugins.querydsl" version "1.0.9"
}

querydsl {
    jpa = true
}

// plugin needed so that the
project.afterEvaluate {
    project.tasks.compileQuerydsl.options.compilerArgs = [
            "-proc:only",
            "-processor", project.querydsl.processors() +
                    ',lombok.launch.AnnotationProcessorHider$AnnotationProcessor'
    ]
}
dependencies {
    compile group: 'com.querydsl', name: 'querydsl-jpa', version: '4.1.3'
}

答案 2 :(得分:1)

下面的pom代码片段通过maven-compiler-plugin与Querydsl,Lombok,Mapstruct一起使用

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <annotationProcessors>
                        <annotationProcessor>lombok.launch.AnnotationProcessorHider$AnnotationProcessor</annotationProcessor>
                        <annotationProcessor>com.querydsl.apt.jpa.JPAAnnotationProcessor</annotationProcessor>
                        <annotationProcessor>org.mapstruct.ap.MappingProcessor</annotationProcessor>
                    </annotationProcessors>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>com.querydsl</groupId>
                            <artifactId>querydsl-apt</artifactId>
                            <version>${querydsl.version}</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>
                        <path>
                            <groupId>javax.annotation</groupId>
                            <artifactId>javax.annotation-api</artifactId>
                            <version>1.3.1</version>
                        </path>
                        <path>
                            <groupId>org.eclipse.persistence</groupId>
                            <artifactId>javax.persistence</artifactId>
                            <version>2.0.0</version>
                        </path>

                    </annotationProcessorPaths>
                </configuration>
            </plugin>

答案 3 :(得分:0)

对于gradle,遵循完全相同的顺序

sourceSets {
  generated {
    java {
      srcDirs = ['build/generated/sources/annotationProcessor/java/main']
    }
  }
}


dependencies {
    api 'com.querydsl:querydsl-jpa:4.4.0'
    annotationProcessor 'org.projectlombok:lombok'
    annotationProcessor('com.querydsl:querydsl-apt:4.4.0:jpa')
    annotationProcessor('javax.annotation:javax.annotation-api')

}