虽然它仍然支持 not officially,但仍然可以使用签名和 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 插件 leverages support for late
configuration 这意味着在配置签名部分时生成 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 文件,需要将文件扩展名 xml.asc 替换为 pom.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 在本地测试)。