gradle 6.x kotlin spring-boot jar 发布失败,需要 gradle-kotlin-dsl 中的解决方法

gradle 6.x kotlin spring-boot jar publish fails, workaround in gradle-kotlin-dsl needed

gradle 6.x release notes 告诉我们 maven-publishing boot-jars 不起作用,因为默认 jar 任务被 spring-boot 插件禁用。

解决方法是告诉 Gradle 要上传什么。如果你想上传bootJar,那么你需要配置传出配置来做到这一点:

configurations {
   [apiElements, runtimeElements].each {
       it.outgoing.artifacts.removeIf { it.buildDependencies.getDependencies(null).contains(jar) }
       it.outgoing.artifact(bootJar)
   }
}

不幸的是,我将其翻译成 gradle-kotlin-dsl 的所有尝试都失败了:


configurations {
   listOf(apiElements, runtimeElements).forEach {
       it.outgoing.artifacts.removeIf { it.buildDependencies.getDependencies(null).contains(jar) }
       it.outgoing.artifact(bootJar)
   }
}

* What went wrong:
Script compilation errors:

it.outgoing.artifacts.removeAll { it.buildDependencies.getDependencies(null).contains(jar) }
                                                                             ^ Out-projected type 'MutableSet<CapturedType(out (org.gradle.api.Task..org.gradle.api.Task?))>' prohibits the use of 'public abstract fun contains(element: E): Boolean defined in kotlin.collections.MutableSet'

it.outgoing.artifacts.removeAll { it.buildDependencies.getDependencies(null).contains(jar) }
                                                                                      ^ Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
                                                                                                             public val TaskContainer.jar: TaskProvider<Jar> defined in org.gradle.kotlin.dsl
it.outgoing.artifact(bootJar)
                     ^ Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: public val TaskContainer.bootJar: TaskProvider<BootJar> defined in org.gradle.kotlin.dsl

知道如何在 Gradle Kotlin DSL 中实现此 groovy 解决方法吗?

jarbootJar 似乎是 Gradle 任务。您可以像这样在 Kotlin DSL 中获取对任务的引用:

configurations {
   listOf(apiElements, runtimeElements).forEach {
       // Method #1
       val jar by tasks
       it.outgoing.artifacts.removeIf { it.buildDependencies.getDependencies(null).contains(jar) }

       // Method #2
       it.outgoing.artifact(tasks.bootJar)
   }
}