Reputation: 587
I write a custom gradle plugin where I would like to copy a specific file from a jar inside the classpath into the buildDir
. I played around in a sandbox project and got this solution working:
task copyFile(type: Copy) {
from zipTree(project.configurations.compile.filter{it.name.startsWith('spring-webmvc')}.singleFile)
include "overview.html"
into project.buildDir
}
but if copy it into my plugin:
project.task(type: Copy, "copyFile") {
from zipTree(project.configurations.compile.filter{it.name.startsWith('spring-webmvc')}.singleFile)
include "overview.html"
into project.buildDir
}
I got the error:
* What went wrong:
A problem occurred evaluating root project 'gradle-springdoc-plugin-test'.
> Could not find method zipTree() for arguments [/Users/blackhacker/.gradle/caches/artifacts-26/filestore/org.springframework/spring-webmvc/4.0.0.RELEASE/jar/a82202c4d09d684a8d52ade479c0e508d904700b/spring-webmvc-4.0.0.RELEASE.jar] on task ':copyFile'.
The result of
println project.configurations.compile.filter{it.name.startsWith('spring-webmvc')}.singleFile.class
is
class java.io.File
What I am doing wrong?
Upvotes: 3
Views: 3917
Reputation: 123960
Unlike a build script, a plugin does not have an implicit project
context (unless you give it one). Hence you'll have to use project.task
rather than task
, project.zipTree
rather than zipTree
, project.file
rather than file
, etc.
PS: In your case, it's important to use project.zipTree { ... }
(note the curly braces) to defer searching for the file until the Zip contents are actually requested. Otherwise you risk slowing down each build invocation (even ones that never execute copyFile
) and, if the file is being produced by the same build, even build failures (because the configuration is resolved before the file has been added).
Upvotes: 6