虽然官方尚未支持,但使用签名和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 }
}
无法简单地将
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')) {
type = 'tar.gz.asc'
} else if (it.type.equals('xml.asc')) {
type = 'pom.asc'
}
artifact source: it.file, classifier: it.classifier ?: null, extension: type
}
}
pom.withXml {
}
}
}
}
这将把构建创建的所有签名文件作为构件添加到发布中。为了使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进行了测试)。
maven
插件。 - sody