同时使用Lombok和Protobuf会导致无依赖性的Gradle任务,导致构建失败有时

发布于 2025-01-25 23:00:57 字数 1875 浏览 2 评论 0原文

我在Gradle中同时使用Lombok和Protobuf。这将生成任务generateFectivectivelectivelectivelombokconfiggenerateProto是独立的。然而,Lombok任务应取决于Protobuf任务,否则Lombok生成的代码指的是尚未由Protoc生成的Java代码。

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的输出添加为源,以确保首先执行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.

如何确保在Lombok任务之前完成所有ProtoBuf任务?

I use lombok and protobuf simultaneously in gradle. This generates the tasks generateEffectiveLombokConfig and generateProto which are independent. Yet the lombok task should depend on the protobuf task, otherwise the code generated by lombok is referring to Java code not yet generated by 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"
}

I tried to add the output of protobuf as sourceSet to make sure the protobuf task is performed first, yet I get a warning:

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

warning:

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.

How can I make sure that all protobuf tasks are done before the lombok tasks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

A君 2025-02-01 23:00:57

您将任务依赖性如下

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

you put task dependency as below

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文