从单个任务发布多个工件

Pie*_*ers 5 gradle

我有一个gradle任务,它执行一个创建多个zip文件的命令行工具.生成文件后,我希望Gradle将生成的zip文件发布到Maven存储库.每个zip文件都有自己的artifactId和groupId(可以从zip文件的文件名派生).zip文件的数量和名称事先是未知的,每次运行时可能会有所不同.

我不是Gradle专家,但在研究了文档后,我认为我应该将zip文件声明为maven-publish插件的发布.我知道如何使用生成单个存档文件的静态文件和任务来执行此操作.我无法在单个任务中找到有关如何使用多个归档执行此操作的示例,就像我的情况一样.

让我们说我build.gradle看起来像这样:

apply plugin: 'base'
apply plugin: 'maven-publish'

task init << {
  buildDir.mkdirs()
}

task makeZipfiles(type: Exec, dependsOn: 'init') {
  workingDir buildDir
  commandLine 'touch', 'test1.zip', 'test2.zip' 
  // actual result files will be different on each run
}

publishing {
  publications {
    // ??? Publication of all files from task makeZipfiles, 
    // each with its own groupId and artifactId
  }
}
Run Code Online (Sandbox Code Playgroud)

我已经能够通过迭代构建目录中的文件来创建出版物,但这只有在我第一次运行makeZipfiles任务然后运行发布任务时才有效.我想要的是使发布任务依赖于makeZipfiles任务,使用makeZipfiles任务的输出文件进行发布.

定义任务,工件和/或出版物以获得所需结果的正确方法是什么?

cjs*_*hno 10

你应该使用这个maven-publish插件.将以下内容添加到build.gradle文件中应用插件的位置:

apply plugin:'maven'
apply plugin:'maven-publish'
Run Code Online (Sandbox Code Playgroud)

然后,在下面的某处添加以下内容(或类似它反映您所需的工件):

task sourcesJar(type: Jar) {
    description = 'Creates sources JAR.'
    classifier = 'sources'

    from project.sourceSets.main.allSource
}

artifacts {
    archives jar
    archives sourcesJar
}

publishing {
    publications {
        mavenJava(MavenPublication){
            artifact jar
            artifact sourcesJar
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这将生成二进制jar和一罐资源.您可以按照类似的模式生成其他所需的罐子.

对于您的具体示例,我建议添加archives makeZipfilesartifacts块中,类似地在下面的出版物中.