简体   繁体   English

[错误]:使用 maven 和 lombok 构建时找不到符号变量日志

[英][ERROR]: cannot find symbol variable log when building with maven and lombok

I am trying to build a Java 11 project with maven and lombok's @Slf4j Logger, but maven does not recognize the log variables.我正在尝试使用 maven 和 lombok 的 @Slf4j Logger 构建 Java 11 项目,但 maven 无法识别log变量。 IntelliJ does though and is able to build the project. IntelliJ 确实并且能够构建该项目。

The error is错误是

[ERROR]: cannot find symbol variable log 

Project and Module SDK is both Java 11. Lombok Version is 1.18.2:项目和模块 SDK 都是 Java 11. Lombok 版本是 1.18.2:

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
    <scope>provided</scope>
</dependency>

My maven compiler setup:我的 maven 编译器设置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>11</source>
                <target>11</target>
                <forceJavacCompilerUse>true</forceJavacCompilerUse>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>1.18.12</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

I already tried:我已经尝试过:

  • turning Annotaion Processing off and on again关闭并再次打开注释处理
  • reinstalling Lombok plugin重新安装龙目岛插件
  • clearing.m2/repository folder clearing.m2/repository 文件夹
  • manually adding lombok.jar as Annotation Processor手动添加 lombok.jar 作为注释处理器
  • adding Lombok path to maven-compiler-plugin list of Annotation Processor将 Lombok 路径添加到注释处理器的maven-compiler-plugin列表

This is a really minimal example configuration for using the @Slf4j lombok logging annotation.这是使用@Slf4j日志注释的一个非常小的示例配置。

You need a logging facade and an implementation, in this case I'm going to use slf4j (as facade) and logback (as implementation).你需要一个日志外观和一个实现,在这种情况下,我将使用slf4j (作为外观)和logback (作为实现)。


pom.xml pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>untitled</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>provided</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
    </dependencies>
</project>   

main.java主.java

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Main {

    public static void main(String[] args) {
        log.debug("Hello");
    }
}

If you get some trouble try always to force the maven dependencies updates running in your project folder mvn -U clean package and reimporting maven project in your IDE If you get some trouble try always to force the maven dependencies updates running in your project folder mvn -U clean package and reimporting maven project in your IDE

看截图

My suspicion is that this is a misleading error message as a consequence of the point that Lombok hooks in during compilation.我怀疑这是一个误导性的错误消息,因为 Lombok 在编译期间挂钩。

In bytecode, there is no concept of an import.在字节码中,没有导入的概念。 Classes are replaced by their fully qualified names (eg Integer to java.lang.Integer ).类被它们的完全限定名称替换(例如Integerjava.lang.Integer )。 Therefore at some point in compilation, the imports are parsed, applied, and any unknown classes (eg due to lack of the correct dependency) will throw an error at this stage.因此,在编译的某个时刻,导入会被解析、应用,并且任何未知的类(例如,由于缺乏正确的依赖关系)都会在这个阶段抛出错误。

Since @Slf4j means you do not need to import org.slf4j.Logger , the step described above is missed for this class.由于@Slf4j意味着您不需要导入org.slf4j.Logger ,因此对于此 class 省略了上述步骤。

After Lombok has appended the log field, the compiler must subsequently look at it's usage, see the class org.slf4j.Logger which it does not recognise and throws an error.在 Lombok 附加log字段后,编译器必须随后查看它的用法,请参阅 class org.slf4j.Logger ,它无法识别并引发错误。 Under normal circumstances, due to the earlier compilation stage, the only possible cause is that the field doesn't exist, so infers that the symbol log must be missing.一般情况下,由于编译阶段较早,唯一可能的原因是该字段不存在,因此推断符号log一定是缺失的。 What it is really failing to understand is the type of the field log .它真正无法理解的是字段log类型

Because Lombok makes changes in the middle of compilation, I guess spurious errors such as these are always a possibility.因为 Lombok 在编译过程中进行了更改,所以我想这些虚假错误总是有可能的。 Perhaps Lombok developers could fix it by doing their own check for org.slf4j.Logger .也许 Lombok 开发人员可以通过自己检查org.slf4j.Logger来修复它。 Most of the functionality provided by Lombok does not involve "importing" external classes, so I'm not surprised that it doesn't handle this edge case as elegantly as possible. Lombok 提供的大部分功能不涉及“导入”外部类,所以我并不惊讶它没有尽可能优雅地处理这种边缘情况。

If you add the dependency for SLF4J, the compiler will no longer complain.如果添加 SLF4J 的依赖项,编译器将不再报错。

In lombok.config file for lombok.log.custom.declaration property use full name including the package for the classes lombok.log.custom.declaration=com.mycomp.logging.Log com.mycomp.logging.LogFactory.getLog(TYPE)(TOPIC)在 lombok.log.custom.declaration 属性的 lombok.config 文件中,使用全名,包括 Lombok.log.custom.declaration=com.mycomp.logging.Log com.mycomp.logging.LogFactory.getLog(TYPE) 类的 package (话题)

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

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