【问题标题】:Lombok not generating boilerplate in .class file and can't find any solutionsLombok 没有在 .class 文件中生成样板文件并且找不到任何解决方案
【发布时间】:2019-07-01 08:07:10
【问题描述】:

我正在尝试使用 maven 和 hibernate 创建一个模块化的 javafx 应用程序。昨天,我的 module-info.java 文件中出现了很多错误,因此我决定从头开始重新创建项目以隔离问题。

项目无法编译,因为 IDE 找不到 lombok 生成的方法。当我检查 .class 文件时,它没有任何自动生成的样板文件。当我没有 module-info.java 文件时,我不会遇到这个问题。

根据我在其他帖子中看到的有关 lombok 的一些建议,我确保已启用注释处理并在 IntelliJ 中启用了 lombok 插件。 当我将 lombok.jar 添加到项目结构中的依赖项时,module-info.jar 不会编译,并且当我将光标放在“需要 lombok”上时,intelliJ 会显示“不明确的模块引用:lombok”。 模块声明也带有红色下划线,显示以下消息:“模块‘AlienDB’从‘lombok’和‘lombok’读取包‘lombok’。

这是我的 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>Aliens</groupId>
  <artifactId>AlienDB</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>AlienDB</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

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

  <dependencies>


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

    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>5.4.3.Final</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.16</version>
    </dependency>



  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>3.0.0-M1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

module-info.java 文件:


module AlienDB {

    requires lombok;
    requires java.persistence;
    requires org.hibernate.orm.core;
    requires java.naming;

    exports Aliens;
}

外星人1.java

package Aliens;

import lombok.Data;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Data
@Entity (name = "alien2")
public class Alien1 {

    @Id
    @Column(name = "aID", nullable = false)
    private int aID;

    @Column(name = "aName")
    private String aName;


}

主应用:

package Aliens;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;


public class App 
{
    public static void main( String[] args )

    {
        Configuration con = new Configuration().configure().addAnnotatedClass(Alien1.class);
        ServiceRegistry reg = new StandardServiceRegistryBuilder().applySettings(con.getProperties()).build();
        SessionFactory sf = con.buildSessionFactory(reg);

        Session session = sf.openSession();
        Transaction tx = session.beginTransaction();

        Alien1 alienName = session.get(Alien1.class, 1);
        //System.out.println(alienName.getAName());

        tx.commit();

        session.close();
        sf.close();
    }
}

当我运行上面显示的应用程序时,将 lombok jar 添加到依赖项中,我收到以下错误:

启动层初始化时出错 java.lang.module.ResolutionException:模块 lombok 未读取导出 org.mapstruct.ap.spi 的模块

当我从项目结构中的依赖项中删除 lombok.jar 时,module-info.jar 没有显示任何错误,但我收到以下错误:

启动层初始化时出错 java.lang.module.FindException:找不到模块 lombok,AlienDB 需要

无论哪种情况,当我取消注释打印行语句时,我都会得到以下方法,大概是因为 lombok 无法生成样板:

错误:(24, 37) java: 找不到符号 符号:方法 getAName() 位置: Aliens.Alien1 类型的变量 AlienName

模块结构:

C:.
├───.idea
├───src
│   ├───main
│   │   ├───java
│   │   │   └───Aliens
│   │   └───resources
│   └───test
│       └───java
│           └───Aliens
└───target
    ├───classes
    ├───generated-sources
    │   └───annotations
    ├───generated-test-sources
    │   └───test-annotations
    ├───maven-archiver
    ├───maven-status
    │   └───maven-compiler-plugin
    │       ├───compile
    │       │   └───default-compile
    │       └───testCompile
    │           └───default-testCompile
    ├───surefire-reports
    └───test-classes
        ├───Aliens
        └───META-INF

我正在使用 IntelliJ Ultimate 2019.1 SDK是java 11 语言级别设置为 9。

编辑:我也尝试在 Eclipse 上运行它,但我遇到了同样的问题 - 没有生成 Lombok 方法,即使它们在建议框中显示为有效建议。

【问题讨论】:

  • 你启用注释处理了吗?
  • 您是否尝试过重新安装最新版本的 IDE 插件?
  • 是的,注释处理已启用,并且插件在 intelliJ 中没有更新?

标签: java maven intellij-idea module lombok


【解决方案1】:

我设法通过添加以下 pom.xml 文件来解决这个问题。

 <configuration>
            <source>9</source>
            <target>9</target>
            <annotationProcessorPaths>
              <annotationProcessorPath>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.8</version>
              </annotationProcessorPath>
            </annotationProcessorPaths>
          </configuration>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    相关资源
    最近更新 更多