简体   繁体   中英

Lombok annotations are not generated in .class file

I'm using inteliJ pro 2020, java 11, maven and lombok .

After running mvn clean install a jar is generated. When I check a specific class file in the jar that has some lombok annotations like @Data, @NoArgsConstructor, @Setter , the annotations are missing.

For example:

Original class:

import com.fasterxml.jackson.annotation.JsonTypeName;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import java.util.Date;
import java.util.Set;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Data
@Setter
@NoArgsConstructor
@JsonTypeName("Bclass")
@Slf4j
public class B extends A{

.class file in jar:

import com.fasterxml.jackson.annotation.JsonTypeName;
import java.util.Date;
import java.util.Set;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@JsonTypeName("Bclass")
    public class B extends A{

The lombock annotations and imports disappeared.

My current pom settings:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <compilerVersion>11</compilerVersion>
                <release>11</release>
                <annotationProcessors>
                  <annotationProcessor>lombok.launch.AnnotationProcessorHider$AnnotationProcessor</annotationProcessor>
                </annotationProcessors>
            </configuration>
        </plugin>
    </plugins>
</build>
......
<dependencies>
.....
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
        </dependency>
....

Solutions that I tried:

  • Enable annotation processing in the compiler settings.
  • Use and in the build section in the pom.xml.
  • Remove the annotationProcessor from the pom.

My goal is to create a jar of this project and import it into another project. The problem is that when I import the jar to another project, all lombok annotations are missing and I can use get and set methods.. any suggestions?

Lombok annotations are not supposed to show up in compiled code. The whole point of lombok is to generate boilerplate code but to make the compiled bytecode not look like it. The reason for that is the RetentionPolicy in the declaration of lombok's annotations:

@Retention(RetentionPolicy.SOURCE)

means that the annotation will be removed after the compilation step and not be part of the bytecode.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM