我在 Gradle 中有一个多项目。 build.gradle
脚本如下所示:
buildscript {
repositories {
jcenter()
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath "com.github.jengelman.gradle.plugins:shadow:2.0.4"
classpath "io.franzbecker:gradle-lombok:1.14"
}
}
allprojects {
//apply plugin: "base"
}
subprojects {
apply plugin: "com.github.johnrengelman.plugin-shadow"
apply plugin: "idea"
apply plugin: "java"
apply plugin: "io.franzbecker.gradle-lombok"
group = "io.shido"
version = "0.1.0-SNAPSHOT"
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
repositories {
jcenter()
mavenCentral()
}
dependencies {
// [start] Research
//compileOnly "org.projectlombok:lombok:1.18.2"
// [end] Research
testCompile "nl.jqno.equalsverifier:equalsverifier:2.4.5"
testCompile "org.junit.jupiter:junit-jupiter-api:$junit_version"
testImplementation "org.junit.jupiter:junit-jupiter-params:$junit_version"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junit_version"
}
//=================================================================================================
// P L U G I N S
//=================================================================================================
lombok {
version = "1.18.2"
}
//=================================================================================================
// T A S K S
//=================================================================================================
// shadowJar { ... }
test {
useJUnitPlatform()
}
}
我有一个 messages
项目,然后使用这个 build.script
:
plugins {
id "java-library"
}
repositories {
jcenter()
mavenCentral()
}
...以及一个带有此 build.script
的 core
项目:
plugins {
id "io.spring.dependency-management" version "1.0.6.RELEASE"
}
dependencies {
compile project(":messages")
}
所有这些都应该没问题。
如果我在messages
中编写一个简单的类:
package io.shido.event;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
@Getter
@Builder
@ToString
@EqualsAndHashCode(of = "name")
class Prototype {
private String id;
private String name;
}
...然后进行相同的单元测试:
package io.shido.event;
import org.junit.jupiter.api.Test;
final class PrototypeTest {
@Test
void instantiate() {
final Prototype event = Prototype.???
}
}
我希望我可以在那里使用该类的构建器,但没有生成任何内容。
我在设置中遗漏了什么吗?一切都会编译,但我看不到为 Lombok 生成任何内容。不确定还可以尝试什么。
最佳答案
如果您使用 IDEA 和最新版本的 Gradle(我认为 >= 4.7),您可以使用以下设置,该设置在我的不同项目中运行良好:
- 从“设置”->“插件配置”面板安装 IDEA 的 Lombok 插件。
在 Gradle 构建脚本中,您可以摆脱 lombok 插件声明和 lombok block :您只需在项目中添加以下依赖项。
ext{ lombokVersion = '1.16.20' junitVersion = '4.12' } dependencies { compileOnly "org.projectlombok:lombok:${lombokVersion}" annotationProcessor "org.projectlombok:lombok:${lombokVersion}" // other libs ... // test dependencies testCompile group: 'junit', name: 'junit', version: "${junitVersion}" }
项目重新导入后,请确保在 IDEA 中启用注释处理,从Settings -> Build,Execution,Deployment -> Compiler-> AnnotationProcessors菜单:有一个复选框“启用注释处理”,默认情况下处于禁用状态。
这应该可以正常工作,并且您也可以在主代码和单元测试中使用 Lombok 功能。
关于Gradle多项目并没有产生Lombok的优点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51951178/