kotlingradlegradle-plugin

Custom gradle task works differently when declared in external gradle plugin


I absolutely ran out of ideas how to fix it (wasted 5 hrs debugging this already) so I hope you can help me. I have following task

open class FatJarTask: Jar() {

    init {

        val configuration = project.configurations.getByName("runtimeClasspath")

        manifest.apply {
            attributes["Timestamp"] = System.currentTimeMillis()
        }

        project.plugins.withType(JavaPlugin::class.java) {
            val sourceSets = project.extensions.getByType(SourceSetContainer::class.java)
            from(sourceSets.getByName("main").output)
        }

        from(configuration.map { if (it.isDirectory) it else project.zipTree(it) }, {
            duplicatesStrategy = DuplicatesStrategy.EXCLUDE
        })

        isZip64 = true
    }
}

implemented in my custom gradle plugin (which is published to maven repo). When it's being used like this

tasks {
    register<FatJarTask>("fatJar")
}

I get error

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':project:fatJar'.
> Entry META-INF/LICENSE is a duplicate but no duplicate handling strategy has been set. Please refer to https://docs.gradle.org/8.10.1/dsl/org.gradle.api.tasks.Copy.html#org.gradle.api.tasks.Copy:duplicatesStrategy for details.

If I move implementation of this task as is to build.gradle.kts of the project that uses it there will be no error.

So, for some reason duplicatesStrategy is not set when task implementation is coming from external gradle plugin. What am I missing ?


Solution

  • It was a scope issue. The fix is

    from(configuration.map { if (it.isDirectory) it else project.zipTree(it) }, {
        it.duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    })
    

    Notice added it.