'Duplicate handling strategy error with gradle while using protobuf for java

I am using the below configuration build.gradle

plugins {
    id "com.google.protobuf" version "0.8.17"
    id "java"
}

group "de.prerna.aws.tests"
version "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

ext {
  
    protobufVersion = "3.18.1"
}


dependencies {

    implementation "com.google.protobuf:protobuf-java:$protobufVersion"


sourceSets {
    main {
        proto {
            srcDir 'src/main/proto'
        }
        java {
            // include self written and generated code
            srcDirs 'src/main/java'
        }
    }
}

protobuf {
    protoc {

        artifact = 'com.google.protobuf:protoc:4.0.0-rc-2'
    }

    plugins {
        grpc {
            artifact = "io.grpc:protoc-gen-grpc-java:1.39.0"
        }
    }
    generateProtoTasks.generatedFilesBaseDir = 'generated-sources'

    generateProtoTasks {
        all().each { task ->
            task.plugins { grpc{} }
        }
        ofSourceSet('main')

    }
}

Error


* What went wrong:
Execution failed for task ':processResources'.
> Entry Person.proto is a duplicate but no duplicate handling strategy has been set. Please refer to https://docs.gradle.org/7.2/dsl/org.gradle.api.tasks.Copy.html#org.gradle.api.tasks.Copy:duplicatesStrategy for details.



Solution 1:[1]

I could fix this problem by adding the following code to my build.gradle.kts:

tasks {
    withType<Copy> {
        filesMatching("**/*.proto") {
            duplicatesStrategy = DuplicatesStrategy.INCLUDE
        }
    }
} 

Extra info: I'm using Gradle 7.3-rc-3 and Java 17.

Solution 2:[2]

A variant of BParolini for build.gradle (Groovy DSL)

tasks.withType(Copy) {
    filesMatching("**/*.proto") {
        duplicatesStrategy = DuplicatesStrategy.INCLUDE
    }
}

Solution 3:[3]

Unfortunately nobody explains reasons for this problem, so here is some of my explorations and guesses. Please correct me if you know more.

If found that following build script code causes this error:

proto { srcDir 'src/main/proto' }

If look inside "build/extracted-include-protos" directory, there are original .proto files copied into "build/extracted-include-protos/test" (but not into main).

My guess is that those auto-copied .proto files are originally uses as the only sources, but when adding "src/main/proto" source set we give some compiler tool second set of same files.

Removing this srcDir is not a good idea, because it required for IDEA to correctly open included .proto on Ctrl+click (otherwise it is opened extracted copies which is useless).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 G-Shadow