gradle - Why is this a receiver type mismatch when it seems to match what I see in the Gradle docs?
问题描述
I'm converting a build from Buildr to Gradle.
So far I have the following build script:
repositories {
jcenter()
}
allprojects {
version = "0.8.1-SNAPSHOT"
group = "org.trypticon.hex"
}
subprojects {
apply(plugin = "java-library")
apply(plugin = "maven-publish")
apply(plugin = "signing")
configure<JavaPluginExtension> {
sourceCompatibility = JavaVersion.VERSION_11
withJavadocJar()
withSourcesJar()
}
dependencies {
"testImplementation"("junit:junit:4.13")
}
configure<PublishingExtension> {
repositories {
maven {
url = uri(if (version.toString().contains("SNAPSHOT")) {
"https://oss.sonatype.org/content/repositories/snapshots"
} else {
"https://oss.sonatype.org/service/local/staging/deploy/maven2"
})
credentials {
username = System.getenv("DEPLOY_USER")
password = System.getenv("DEPLOY_PASS")
}
}
}
publications {
register<MavenPublication>("mavenJava") {
pom {
// omitting a bunch of metadata unrelatted to the issue
}
}
}
}
configure<SigningExtension> {
sign(publishing.publications["mavenJava"])
}
}
When I try to build, I get this:
build.gradle.kts:124:14: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public val PluginDependenciesSpec.publishing: PluginDependencySpec defined in org.gradle.kotlin.dsl
IntelliJ says that the types match and shows no error.
I started with code from the Gradle docs which said to use this:
signing {
sign(publishing.publications["mavenJava"])
}
But that fails when inside a subprojects
block.
Using:
- Gradle 6.8
- Java 11.0.3
Further investigation:
I tried splitting the lines out to see exactly which bit the issue was on.
configure<SigningExtension> {
val e: PublishingExtension = publishing
val p: Publication = e.publications["mavenJava"]
sign(p)
}
The error points directly at publishing
on the first line. Hovering over it in IDEA, it helpfully tells me that Project.publishing
is a PublishingExtension
, so the call shows no error. Now the question is, why is it fine when inspecting the file, but fails at runtime? I thought this was exactly the kind of thing switching to Kotlin was going to stop. :(
解决方案
我有一个类似的问题,我通过编写以下代码解决了这个问题:
configure<SigningExtension> {
val pubExt = checkNotNull(extensions.findByType(PublishingExtension::class.java))
val publication = pubExt.publications["your_publication"]
sign(publication)
}
在您的情况下,your_publication
应替换为mavenJava
.
推荐阅读
- azure - Azure 托管应用程序 - 带有应用程序链接的 ViewDefinition.json 操作
- javascript - 单击按钮时如何将所有其他布尔值切换为false
- mysql - 检查值是否存在于另一个(连接的)表中并相应地在结果中填写列
- linux - for 循环在 bash 中创建的整数比预期的要多
- jenkins - gitlab gui 不显示从阶段测试返回的 jenkinsfile 报告
- python - 从列表中创建可滚动区域
- windows - 如何使用 NDIS 过滤器驱动程序从协议层重定向 TCP 数据包?
- amazon-web-services - AWS Elastic Beanstalk 日志?访问更详细的日志?
- spring-boot - 是否可以使用 Ehcache 3.x 实现缓存刷新行为?
- docker - 无法使用 minikube IP 和 NodePort 连接 Pod