Gradle 7.6无法向Sonatype仓库发布Jar制品的问题排查
问题描述
我正遵循Sonatype文档发布Gradle项目,Gradle版本信息如下:
$ ./gradlew --version ------------------------------------------------------------ Gradle 7.6 ------------------------------------------------------------ Build time: 2022-11-25 13:35:10 UTC Revision: daece9dbc5b79370cc8e4fd6fe4b2cd400e150a8 Kotlin: 1.7.10 Groovy: 3.0.13 Ant: Apache Ant(TM) version 1.10.11 compiled on July 10 2021 JVM: 1.8.0_181 (Oracle Corporation 25.181-b13) OS: Mac OS X 10.16 x86_64
尝试使用示例中的uploadArchives任务时,出现以下错误:
$ ./gradlew uploadArchives FAILURE: Build failed with an exception. * Where: Build file '/Users/ferozed/stash/libs/mask-json-field-transform/build.gradle' line: 167 * What went wrong: A problem occurred evaluating root project 'mask-json-field-transform'. > Could not find method uploadArchives() for arguments [build_6yphclnk6m8p3rtmq5h7m56li$_run_closure12@19fbeecd] on root project 'mask-json-field-transform' of type org.gradle.api.Project.
我的build.gradle文件内容如下:
buildscript { repositories { gradlePluginPortal() } dependencies { classpath 'gradle.plugin.com.github.johnrengelman:shadow:7.1.2' } } plugins { id 'java' // so that we can use 'implementation', 'testImplementation' for dependencies id 'maven-publish' id 'signing' } apply plugin: 'maven-publish' apply plugin: 'signing' apply plugin: 'com.github.johnrengelman.shadow' repositories { jcenter() mavenCentral() maven { url = "https://packages.confluent.io/maven" } maven { url = "https://jitpack.io" } } group = 'io.github.ferozed.kafka.connect' version = '0.1' dependencies { // Kafka implementation group: 'org.apache.kafka', name: 'connect-api', version: '3.3.1' implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.14.1' implementation 'com.github.jcustenborder.kafka.connect:connect-utils:0.7.173' implementation 'com.github.jcustenborder.kafka.connect:kafka-connect-transform-common:0.1.0.14' //test testImplementation(platform('org.junit:junit-bom:5.9.0')) testImplementation('org.junit.jupiter:junit-jupiter') testImplementation("io.mockk:mockk:1.9.2") } sourceSets { main { java { srcDirs = ["src/main/java"] } resources { srcDirs = ["src/main/avro", "src/main/resources"] } } test { java { srcDirs = ["src/test/java"] } } } test { useJUnitPlatform() testLogging { events "passed", "skipped", "failed" } } task javadocJar(type: Jar) { classifier = 'javadoc' from javadoc } task sourcesJar(type: Jar) { classifier = 'sources' from sourceSets.main.allSource } tasks.withType(AbstractArchiveTask).configureEach { preserveFileTimestamps = false reproducibleFileOrder = true } plugins.withId("com.github.johnrengelman.shadow"){ //this block requires the java plugin to be applied first. plugins.withId("java"){ shadowJar { //We are overriding the default jar to be the shadow jar classifier = null exclude 'META-INF' exclude 'META-INF/*.INF' exclude 'META-INF/license/*' } jar { manifest { attributes( 'Built-By' : System.properties['user.name'], 'Build-Timestamp': new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(new Date()), 'Created-By' : "Gradle ${gradle.gradleVersion}", 'Build-Jdk' : "${System.properties['java.version']} (${System.properties['java.vendor']} ${System.properties['java.vm.version']})", 'Build-OS' : "${System.properties['os.name']} ${System.properties['os.arch']} ${System.properties['os.version']}" ) } } tasks.build.dependsOn tasks.shadowJar tasks.shadowJar.mustRunAfter tasks.jar tasks.shadowJar.mustRunAfter tasks.javadocJar tasks.shadowJar.mustRunAfter tasks.sourcesJar } } publishing { publications { mavenJava(MavenPublication) { pom { name = 'Mask Json Field Transform' description = 'A kafka connect transform to remove the value of a sensitive field in a json document.' url = 'https://github.com/ferozed/mask-json-field-transform' properties = [ myProp: "value", "prop.with.dots": "anotherValue" ] licenses { license { name = 'The Apache License, Version 2.0' url = 'http://www.apache.org/licenses/LICENSE-2.0.txt' } } developers { developer { id = 'ferozes' name = 'Feroze Daud' email = 'ferozed.oss@gmail.com' } } scm { connection = 'scm:git:git@github.com:ferozed/mask-json-field-transform.git' developerConnection = 'scm:git:git@github.com:ferozed/mask-json-field-transform.git' url = 'https://github.com/ferozed/mask-json-field-transform' } } } } } signing { sign configurations.archives sign publishing.publications.mavenJava } tasks.signArchives.dependsOn tasks.shadowJar artifacts { archives javadocJar, sourcesJar } uploadArchives { repositories { mavenDeployer { beforeDeployment { deployment -> signing.signPom(deployment) } repository(url: "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/") { authentication(userName: ossrhUsername, password: ossrhPassword) } snapshotRepository(url: "https://s01.oss.sonatype.org/content/repositories/snapshots/") { authentication(userName: ossrhUsername, password: ossrhPassword) } pom.project { name 'Example Application' packaging 'jar' // optionally artifactId can be defined here description 'A kafka connect transform to remove the value of a sensitive field in a json document.' url 'https://github.com/ferozed/mask-json-field-transform' scm { connection 'scm:svn:http://foo.googlecode.com/svn/trunk/' developerConnection 'scm:svn:https://foo.googlecode.com/svn/trunk/' url 'https://github.com/ferozed/mask-json-field-transform' } licenses { license { name 'The Apache License, Version 2.0' url 'http://www.apache.org/licenses/LICENSE-2.0.txt' } } developers { developer { id 'ferozed' name 'Feroze Daud' email 'feroz@gmail.com' } } } } } }
请问我哪里操作有误?
解决方法
核心问题
uploadArchives是旧版maven插件提供的任务,而你已经在使用Gradle推荐的新版maven-publish插件,两个插件不兼容,所以找不到该方法。
修复步骤
- 删除旧的
uploadArchives配置块:直接删掉build.gradle末尾的整个uploadArchives{...}代码段。 - 在
publishing块中配置Sonatype仓库:把原uploadArchives里的仓库配置移到publishing.repositories下,同时补充要发布的构件:
publishing { publications { mavenJava(MavenPublication) { // 关联Java构件和shadowJar、源码包、文档包 from components.java artifact shadowJar artifact javadocJar artifact sourcesJar // 保留你已有的pom配置 pom { name = 'Mask Json Field Transform' description = 'A kafka connect transform to remove the value of a sensitive field in a json document.' url = 'https://github.com/ferozed/mask-json-field-transform' properties = [ myProp: "value", "prop.with.dots": "anotherValue" ] licenses { license { name = 'The Apache License, Version 2.0' url = 'http://www.apache.org/licenses/LICENSE-2.0.txt' } } developers { developer { id = 'ferozes' name = 'Feroze Daud' email = 'ferozed.oss@gmail.com' } } scm { connection = 'scm:git:git@github.com:ferozed/mask-json-field-transform.git' developerConnection = 'scm:git:git@github.com:ferozed/mask-json-field-transform.git' url = 'https://github.com/ferozed/mask-json-field-transform' } } } } repositories { maven { name = "SonatypeStaging" // 根据版本自动切换正式/快照仓库 url = uri(version.endsWith('SNAPSHOT') ? "https://s01.oss.sonatype.org/content/repositories/snapshots/" : "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/") credentials { username = ossrhUsername password = ossrhPassword } } } }
- 调整签名配置:移除旧的
sign configurations.archives,只保留针对mavenJava发布的签名:
signing { sign publishing.publications.mavenJava }
- 删除冗余插件应用:你已经通过
plugins{}块应用了maven-publish和signing,可以删掉重复的apply plugin语句:
// 删掉这两行 apply plugin: 'maven-publish' apply plugin: 'signing'
- 删除旧的构件和签名依赖配置:
maven-publish会自动处理构件发布,所以删掉:
// 删掉这两段 artifacts { archives javadocJar, sourcesJar } tasks.signArchives.dependsOn tasks.shadowJar
发布命令
完成修改后,使用新版插件的发布命令:
./gradlew publish
内容的提问来源于stack exchange,提问作者feroze
相关产品推荐
相关产品推荐

