我的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任务设置。我假设它可以转换为一些文件。因此它应该用作发布任务的输入,作为文件。但错误信息告诉我,我错了。
我该如何解决?
由于
答案 0 :(得分:7)
Gradle不知道如何将Copy
任务转换为MavenArtifact
,AbstractArchiveTask
,PublishArtifact
,....这解释了错误消息。
但是它确实知道如何将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
}
}
}
}