简体   繁体   中英

Copy file from a jar in a custom gradle plugin

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?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM