从复制任务发布工件

dwu*_*sen 5 gradle

我的gradle构建副本文件.我想使用复制任务的输出作为maven工件发布的输入

例如:

task example(type: Copy) {
    from "build.gradle" // use as example
    into "build/distributions"
}

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

但是gradle不喜欢它:

 * What went wrong:
 A problem occurred configuring project ':myproject'.
 > Exception thrown while executing model rule: PublishingPlugin.Rules#publishing(ExtensionContainer)
     > Cannot convert the provided notation to an object of type MavenArtifact: task ':myproject:example'.
       The following types/formats are supported:
       - Instances of MavenArtifact.
       - Instances of AbstractArchiveTask, for example jar.
       - Instances of PublishArtifact
       - Maps containing a 'source' entry, for example [source: '/path/to/file', extension: 'zip'].
       - Anything that can be converted to a file, as per Project.file()
Run Code Online (Sandbox Code Playgroud)

为什么?

据我了解,任务示例的输出应由Copy任务设置.我假设它可以转换为一些文件.因此它应该用作发布任务的输入,作为文件.但错误信息告诉我,我错了.

我该如何解决?

谢谢

dwu*_*sen 9

摇篮不知道如何将转换Copy任务的MavenArtifact,AbstractArchiveTask,PublishArtifact,...这解释了错误信息.

它确实知道如何将a转换String为a File,正如它在错误消息的最后一行中所解释的那样.

问题是如何在发布之前强制Gradle构建我的任务.MavenArtifact有一个builtBy方法就在这里!

task example(type: Copy) {
    from "build.gradle" // use as example
    into "build/distributions"
}

publishing {
    publications {
        mavenJava(MavenPublication) {
           // file to be transformed as an artifact
           artifact("build/distributions/build.gradle") {
               builtBy example // will call example task to build the above file
           }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)