In the context of writing a custom plugin.
In the apply
function of the plugin, I register
a task.
This is a copy
task.
The task copies a project resource (‘src/main/proto/foo.proto’) into a target directory.
Based on Working With Files it seems like the following should work.
tasks.register<Copy>("retrieveSchema") {
group = "Retrieve"
into(project.extensions.schemaDir)
from('src/main/proto') {
include("*.proto")
}
}
}
It appears that it does not work because it searches the consuming project for the source file rather than the plugin resources.
This question has been previously asked a few times with answers.
- Gradle plugin copy directory tree with files from resources - #7 by Shinya_Mochida
- How do I tell custom-gradle-plugin to read its own file instead of looking for it in the plugin project?
This lead me to the following, which does work.
tasks.register<Copy>("retrieveSchema") {
group = "retrieve"
val loader = javaClass.classLoader
val url= loader.getResource("foo.proto")
if (url == null) {
logger.quiet("resource not available")
}
else if (url.toURI().scheme.equals("jar")) {
val fs = FileSystems.newFileSystem(url.toURI(), emptyMap<String, Any>())
into(project.extensions.schemaDir)
from(project.zipTree(fs.toString())) {
include("foo.proto")
}
}
else {
logger.quiet("resource not found in a jar")
}
}
While this works it seems wrong.
- The
project.zipTree(fs.toString())
- Will it always be a
jar
?
Any advice?