Home > database >  Using both lombok and protobuf results in gradle tasks without dependencies resulting in builds fail
Using both lombok and protobuf results in gradle tasks without dependencies resulting in builds fail

Time:05-04

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?

CodePudding user response:

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
  • Related