13

When a method or variable is annotated with Lombok annotation, the maven plugin will complain by processing the source generation for JPA.

I get this kind of failure in the console logs:

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

How to make the apt-maven-plugin and queryDSL processor for JPA annotations work together with lombok annotations ?

jchrbrt
  • 1,006
  • 1
  • 11
  • 12

4 Answers4

27

This solution worked for me. Add lombok.launch.AnnotationProcessorHider$AnnotationProcessor in your apt-maven-plugin configuration.

<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>

It seems also to be working the same way with gradle: See https://github.com/ewerk/gradle-plugins/issues/59#issuecomment-247047011

jchrbrt
  • 1,006
  • 1
  • 11
  • 12
  • I would like to use the QueryDSL Sql project http://www.querydsl.com/static/querydsl/latest/reference/html/ch02s03.html Is there a way to add the annotationProcessor there, too? – terix2k11 Dec 09 '20 at 16:52
  • It works! Thank you! If it does not work, make sure there is no spaces between processors in tag. – SMilos Oct 19 '22 at 11:23
  • not working for in spring boot 2.7 and .springframework.data.mongodb.repository.support.MongoAnnotationProcessor – elonderin Jul 10 '23 at 14:13
8

here is the syntax for GRADLE users (maven users please have a look at the other answers)

// this adds lombok correctly to your project then you configure the jpa processor

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'
  ]
}

here is a working version of QueryDSL and Lombok. Dependencies are imported by plugins, therefore no dependencies need to be declared:

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'
}
Leonid Dashko
  • 3,657
  • 1
  • 18
  • 26
Toumi
  • 2,925
  • 4
  • 37
  • 31
2

Below pom snippet works for me with Querydsl, Lombok, Mapstruct all together by maven-compiler-plugin

<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>
Kencourt
  • 205
  • 2
  • 7
  • Thank you, Mapstruct was going to be the next thing I had to figure out. And FYI to anyone, you'll still want to declare your querydsl dependencies like normal, otherwise you'll see an unhelpful NullPointerError when running Maven. – A. Weatherwax Jun 24 '20 at 00:09
0

For gradle, follow the exact same order

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')

}
Deepak
  • 1,506
  • 1
  • 14
  • 13