我在 Spring 项目中使用 Lombok,因为我使用 IDE Visual Studio Code 和 Java 插件扩展包,所以会自动创建必要的生成源。这在 IDE 内部有效,但在 IDE 外部则失败。
运行“./mvnw clean package”时出现错误:
cannot find symbol
。我坚信这个问题是由于 pom.xml 文件中缺少配置导致的,这会在打包之前触发创建生成的源。
如何在打包之前触发 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.myproject</groupId>
<artifactId>base</artifactId>
<version>0.0.1</version>
<name>base</name>
<description>My Project Base</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>23</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-crypto</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>6.1.7.Final</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.5</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
编辑1:
我在使用 Lombok 的任何地方都会遇到错误,即这个错误:
[ERROR] /home/myuser/Documents/Programming/myproject/backend/base/src/main/java/com/myproject/base/service/logic/SettingService.java:[11,42] cannot find symbol
symbol: class Setting_
location: package com.myproject.base.data.model.entity
在此实例中,类
SettingService
尝试导入此内容但失败:
import com.glasses.base.data.model.entity.Setting_;
像这样使用它
SETTING_KEY
:
Sort sort = Sort.by(Sort.Direction.ASC, Setting_.SETTING_KEY)
.and(Sort.by(Sort.Direction.ASC, Setting_.SETTING_VALUE));
Setting
类看起来像这样:
导入 jakarta.persistence.Entity; 导入 jakarta.persistence.GenerateValue; 导入 jakarta.persistence.GenerationType; 导入 jakarta.persistence.Id; 导入 jakarta.persistence.Table; 导入 jakarta.validation.constraints.NotBlank; 导入 jakarta.validation.constraints.Size; 导入lombok.AllArgsConstructor; 导入lombok.Data; 导入 lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "setting")
public class Setting {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
@Size(min = 1, max = 50)
private String settingKey;
@NotBlank
@Size(min = 1, max = 50)
private String settingValue;
public Setting(String settingKey, String settingValue) {
this.settingKey = settingKey;
this.settingValue = settingValue;
}
}
这里的问题是这个文件缺少“target/ generated-sources/annotations/com/glasses/base/data/model/entity/Setting_.java”,它看起来像这样:
import jakarta.persistence.metamodel.SingularAttribute;
import jakarta.persistence.metamodel.StaticMetamodel;
import javax.annotation.processing.Generated;
@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(Setting.class)
public abstract class Setting_ {
public static volatile SingularAttribute<Setting, String> settingValue;
public static volatile SingularAttribute<Setting, Long> id;
public static volatile SingularAttribute<Setting, String> settingKey;
public static final String SETTING_VALUE = "settingValue";
public static final String ID = "id";
public static final String SETTING_KEY = "settingKey";
}
目前该文件仅由IDE生成,而不是由Maven生成。这是需要改变的。
您需要将
maven-processor-plugin
添加到 the pom.xml
确保生成的类在编译之前创建。 该插件运行注释处理器,例如在生成源阶段生成 Lombok 和 Hibernate JPA 模型所需的注释处理器。
https://mvnrepository.com/artifact/org.bsc.maven/maven-processor-plugin[maven-processor-plugin]