I’m developing an external gradle plugin that can be imported and used by other gradle projects.
One of the things I want my plugin to do is define a “Copy”-type task which copies a collection of files into the buildDirectory. Something like this:
task generateFile(type: Copy) {
from "myfile.src"
into "${buildDir}"
rename '(.*).src', '$1'
expand([
project: project,
])
}
However, what I really want to do is pack myfile.src on the classpath of my plugin and pull it from there. My plugin build would have a src dir like this:
src/main/resources/myfile.src
And my plugin source would look something like this:
public class MyPlugin implements Plugin<Project> {
void apply(Project project) {
project.task([type: Copy], 'generateFile') {
from classpath('myfile.src') // This is pseudo-code expressing my desired havior
into "${project.buildDir}"
rename '(.*).src', '$1'
expand([
project: project,
])
}
Is there a way to directly use a copy task in this way? I couldn’t figure out how to do it, so I resorted to something ugly where I have an initial task which get’s a handle to a classloader and call getResourceAsStream(“myfile.src”) and copies that to the target directory, like this:
public class MyPlugin implements Plugin<Project> {
void apply(Project project) {
project.task('copyTemplateFiles') << {
['myfile'].each {
file ->
def is = this.class.classLoader.getResourceAsStream("${file}.src")
File target = new File("${project.buildDir}/${file}.src")
target.withOutputStream {
os ->
os << is
}
if (is) {
is.close()
}
}
}
}
The above code is ugly and I can only get it to work if I pass the --no-daemon flag to gradle. When running with the daemon, is turns out to be null. Is there a better way to get the InputStream?
Any help would be greatly appreciated.
Thanks.
Doug