简体   繁体   English

.class文件中不生成Lombok注解

[英]Lombok annotations are not generated in .class file

I'm using inteliJ pro 2020, java 11, maven and lombok .我正在使用 inteliJ pro 2020、java 11、 mavenlombok

After running mvn clean install a jar is generated.运行mvn clean install后,会生成 jar。 When I check a specific class file in the jar that has some lombok annotations like @Data, @NoArgsConstructor, @Setter , the annotations are missing.当我检查 jar 中的特定 class 文件时,该文件具有一些lombok注释,例如@Data, @NoArgsConstructor, @Setter ,这些注释丢失了。

For example:例如:

Original class:原装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: jar 中的 .class 文件:

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. lombock 注释和导入消失了。

My current pom settings:我当前的 pom 设置:

<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.在 pom.xml 的构建部分中使用和。
  • Remove the annotationProcessor from the pom.从 pom 中删除 annotationProcessor。

My goal is to create a jar of this project and import it into another project.我的目标是创建该项目的 jar 并将其导入另一个项目。 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?问题是当我将 jar 导入另一个项目时,所有 lombok 注释都丢失了,我可以使用 get 和 set 方法..有什么建议吗?

Lombok annotations are not supposed to show up in compiled code. Lombok 注释不应该出现在已编译的代码中。 The whole point of lombok is to generate boilerplate code but to make the compiled bytecode not look like it. lombok 的重点是生成样板代码,但使编译后的字节码看起来不像它。 The reason for that is the RetentionPolicy in the declaration of lombok's annotations:原因是lombok注释声明中的RetentionPolicy:

@Retention(RetentionPolicy.SOURCE)

means that the annotation will be removed after the compilation step and not be part of the bytecode.意味着注释将在编译步骤后被删除,而不是字节码的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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