找不到名称为“java”的 SoftwareComponentInternal

Gui*_*lhE 3 gradle maven-plugin bintray android-gradle-plugin gradle-kotlin-dsl

应用模块 build.gradle

apply plugin: 'com.android.library'
apply from: rootProject.file('deploy-bintray.gradle.kts')
android {...}
Run Code Online (Sandbox Code Playgroud)

deploy-bintray.gradle.kts这是我的 bintray/maven 发布脚本。

我在生成 .jar 文件时遇到问题:

val sourcesJar by tasks.registering(Jar::class) {
    archiveClassifier.set("sources")
    from(project.the<SourceSetContainer>()["main"].allSource)
}

publications {
        create<MavenPublication>(bintrayRepo) {
            groupId = publishedGroupId
            artifactId = artifact
            version = libraryVersion

            from(components["java"])
            artifact(sourcesJar.get())
            artifact(dokkaJar.get())
            ...
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

它失败了:

找不到名称为“java”的 SoftwareComponentInternal。

或者,如果我评论from(components["java"])它失败:

未找到名称为“main”的 SourceSet。

如果我添加java插件:

'java' 插件已被应用,但它与 Android 插件不兼容。

所以我被困在这里。我该如何解决这个问题?

Gui*_*lhE 6

我终于找到了解决办法!
我做错了一些事情,首先,dokkaJarsourceJar任务都必须在 mainbuild.gradle而不是 inside deploy-bintray.gradle.kts。移动它们使其工作并修复:

SourceSet with name 'main' not found.

其次我们不能使用,from(components["java"])因为这是一个 Android 库,所以我用artifact("$buildDir/outputs/aar/${artifactId}-release.aar").

最后但并非最不重要的,如此处所述(第 7 步)

“此外,生成的 POM 文件不包含依赖链,因此必须明确添加......”

我不得不添加这个:

pom {
...
    withXml {
       val dependenciesNode = asNode().appendNode("dependencies")
       configurations.getByName("implementation") {
         dependencies.forEach {
            val dependencyNode = dependenciesNode.appendNode("dependency")
            dependencyNode.appendNode("groupId", it.group)
            dependencyNode.appendNode("artifactId", it.name)
            dependencyNode.appendNode("version", it.version)
         }
       }     
    }       
}
Run Code Online (Sandbox Code Playgroud)