从复制任务发布工件

时间:2016-12-08 17:03:13

标签: gradle

我的gradle构建副本文件。我想使用复制任务的输出作为maven artifact publishing

的输入

示例:

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

publishing {
    publications {
        mavenJava(MavenPublication) {
            artifact example
        }
    }
}

但是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()

为什么?

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

我该如何解决?

由于

1 个答案:

答案 0 :(得分:7)

Gradle不知道如何将Copy任务转换为MavenArtifactAbstractArchiveTaskPublishArtifact,....这解释了错误消息。

但是它确实知道如何将String转换为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
           }
        }
    }
}