如何使用Gradle对Maven发布进行签名

6

有一个使用新发布插件的build.gradle脚本:

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'signing'
apply plugin: 'maven-publish'

// ...

publishing {
    publications {
        maven(MavenPublication) {
            from components.java

            artifact sourcesJar {
                classifier 'source'
            }
        }
    }

    repositories {
        maven {
            name 'Temporary'
            url "file://${rootProject.buildDir}/repo"
        }
    }
}

signing {
    sign configurations.archives
}

问题如下:

  1. 如何签署maven pom文件?
  2. 如何将签名发布到maven仓库?
2个回答

6
新的孵化阶段的maven-publish插件目前还不支持签名功能。

谢谢,那我会回滚使用 maven 插件。 - sody
2
@PeterNiederwieser,当你说“maven-publish插件尚不支持签名”时,我不确定你的意思。 鉴于上面示例中的签名插件可以正常工作,即按预期生成.asc文件,这不是找到一种方法告诉maven-publish插件像上传其他任何构件一样上传它们吗? - Chris Beams
现在是2015年11月,已经快一年了;我正在使用gradle 2.9,这个插件仍然不支持签名吗? :/ - fge
截至2021年,发布插件是否支持签名? - Greg
@Greg 现在已经支持了 https://docs.gradle.org/current/userguide/publishing_signing.html - Daniel
显示剩余3条评论

4

虽然官方尚未支持,但使用签名和maven-publish插件仍然可以上传已签名的构建输出。

首先,我们像往常一样设置签名部分:

apply plugin: 'signing'

signing {
  sign configurations.archives
}

这将对项目存档进行签名。为了签署由maven-publish插件创建的POM,我们需要添加一个签名任务:
task signPom(type: Sign) {
  sign project.file('build/publications/maven/pom-default.xml')
  outputs.upToDateWhen { false }  // the signing plugin does not seem to notice
                                  // it when the publications folder with the
                                  // signature has been deleted. So we always
                                  // create a new signature
}

无法简单地将sign generatePomFileForMavenPublication行添加到signing中,因为maven-plublish插件利用了对于后期配置的支持,这意味着在配置签名部分时生成pom的任务不可用。
现在我们拥有所需的所有签名文件。我们只需要将它们添加到发布中即可。
apply plugin: 'maven-publish'

publishing {
  publications {
    maven(MavenPublication) {
      from components.java

      project.tasks.withType(Sign) {
        signatures.all {
          def type = it.type
          if (it.file.name.endsWith('.tar.gz.asc')) {  // Workaround in case a tar.gz file should published
            type = 'tar.gz.asc'
          } else if (it.type.equals('xml.asc')) {  // Set correct extension for signature of pom file
            type = 'pom.asc'
          }
          artifact source: it.file, classifier: it.classifier ?: null, extension: type
        }
      }

      pom.withXml {
        // The pom can be enriched as usual
      }
    }
  }
}

这将把构建创建的所有签名文件作为构件添加到发布中。为了使pom文件拥有正确的名称,需要用pom.asc替换文件扩展名xml.asc(maven-publish插件将pom本地存储为pom-default.xml)。
所有任务都已经存在并相互连接,最后一件要做的事情是在模型中设置依赖项。
model {
  tasks.publishMavenPublicationToMavenLocal {
    dependsOn project.tasks.withType(Sign)
  }
  tasks.publishMavenPublicationToNexusLocalSnapshotsRepository {
    dependsOn project.tasks.withType(Sign)
  }
  tasks.signPom {
    dependsOn tasks.generatePomFileForMavenPublication
  }
}

第二个任务的名称取决于“publications.repository”部分中仓库的名称。我的称为“NexusLocalSnapshots”。
唯一的不利因素是对于每个签名文件,都会创建一个md5和sha1校验和文件。不过,对于存储库管理器来说似乎并不是问题(在本地使用Nexus 3进行了测试)。

Gradle在6.5或7中是否支持签署Maven发布?我似乎已经成功使用签名插件对我的构件进行了签名,但它们不再被上传。在设置签名之前,未签名的构件可以被上传... - Greg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接