同时使用 lombok 和 protobuf 会导致 gradle 没有依赖项的任务,有时会导致构建失败

Using both lombok and protobuf results in gradle tasks without dependencies resulting in builds failing sometimes

我在gradle中同时使用lombok和protobuf。这会生成独立的任务 generateEffectiveLombokConfiggenerateProto。然而 lombok 任务应该依赖于 protobuf 任务,否则 lombok 生成的代码指的是 Java protoc 尚未生成的代码。

syntax = "proto3";
package my.example.v1;
message Task {
  string id = 1;
  repeated string names_to_print = 2;
}
package org.example;

import lombok.experimental.UtilityClass;
import my.example.v1.*;

@UtilityClass
public class Worker {

    public void work(TaskOuterClass.Task task) {
        // do something
    }

}
plugins {
    id 'java'
    id 'io.freefair.lombok' version '6.4.3'
    id 'com.google.protobuf' version '0.8.18'
}

group 'org.example'

repositories {
    mavenCentral()
}

dependencies {
    implementation "com.google.protobuf:protobuf-java:3.20.1"
    implementation "com.google.protobuf:protobuf-java-util:3.20.1"
}

我尝试将 protobuf 的输出添加为 sourceSet 以确保首先执行 protobuf 任务,但我收到警告:

sourceSets {
    main {
        java {
            srcDir "${projectDir}/build/generated/source/proto/main/java"
        }
    }
}

警告:

Execution optimizations have been disabled for task ':generateProto' to ensure correctness due to the following reasons:
  - Gradle detected a problem with the following location: '/build/generated/source/proto/main'. Reason: Task ':generateEffectiveLombokConfig' uses this output of task ':generateProto' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. Please refer to https://docs.gradle.org/7.4.2/userguide/validation_problems.html#implicit_dependency for more details about this problem.

如何确保所有 protobuf 任务在 lombok 任务之前完成?

你把任务依赖如下

afterEvaluate {
    generateEffectiveLombokConfig.mustRunAfter generateProto
}
Executing 'build -m'...

:extractIncludeProto SKIPPED
:extractProto SKIPPED
:generateProto SKIPPED
:generateEffectiveLombokConfig SKIPPED
:compileJava SKIPPED
:processResources SKIPPED
:classes SKIPPED
:jar SKIPPED
:assemble SKIPPED
:generateTestEffectiveLombokConfig SKIPPED
:extractIncludeTestProto SKIPPED
:extractTestProto SKIPPED
:generateTestProto SKIPPED
:compileTestJava SKIPPED
:processTestResources SKIPPED
:testClasses SKIPPED
:test SKIPPED
:check SKIPPED
:build SKIPPED

BUILD SUCCESSFUL in 299ms