Hi
I am trying to use the Exec task within my custom plugin along with the extension configuration mechanism and am running into the problem of the Exec task being configured before the extension evaluates, resulting in the task using the default values instead of the user supplied ones. I don’t think this is really a problem with Exec itself, it just happens to be what I am trying to use.
Here is a snipet of an example build.gradle, with a task to print the commandLine from the Exec task:
apply plugin: 'launch4j'
launch4j {
launch4jCmd = "NewValueFromExtension"
mainClassName = "com.example.myapp.Start"
icon = 'icons/myApp.ico'
}
task printCmd() << {
println project.getTasksByName('createExe', false).iterator().next().commandLine
}
Simply flipping the order of the apply plugin and the launch4j extension doesn’t work as the plugin has to be applied before the extension exists.
My plugin creates the task like this:
def task = project.tasks.add(TASK_RUN_NAME, Exec)
task.group = LAUNCH4J_GROUP
task.commandLine "$project.launch4j.launch4jCmd", "${project.buildDir}/${project.launch4j.outputDir}/${project.launch4j.xmlFileName}"
task.workingDir "${project.buildDir}/${project.launch4j.outputDir}"
return task
I have also tried this
def task = project.tasks.add(TASK_RUN_NAME, Exec)
task.configure {
description = "Runs launch4j to generate an .exe file"
group = LAUNCH4J_GROUP
commandLine "$project.launch4j.launch4jCmd", "${project.buildDir}/${project.launch4j.outputDir}/${project.launch4j.xmlFileName}"
workingDir "${project.buildDir}/${project.launch4j.outputDir}"
}
and this
def task = project.task(TASK_RUN_NAME, Type:Exec) << {
description = "Runs launch4j to generate an .exe file"
group = LAUNCH4J_GROUP
commandLine "$project.launch4j.launch4jCmd", "${project.buildDir}/${project.launch4j.outputDir}/${project.launch4j.xmlFileName}"
workingDir "${project.buildDir}/${project.launch4j.outputDir}"
}
but in all cases the configuration of the task happens before the extension is evaluated, and so the commandLine does not reflect the users configuration values from the build file.
Digging into the Exec source code, I found it accepts an Object for setting the commandLine, so I thought I might be able to pass a closure in to delay the evaluation, but Exec only does a toString() on the object (DefaultProcessForkOptions.java" line 43). And so no evaluation of the closure, and hence no delayed configuration.
I assume I must be missing something obvious about how to wait until after the extension is evaluated to grab the contents to configure the Exec task, but am stumped.
BTW, the (slightly broken) plugin is here: http://code.google.com/p/gradle-launch4j/
Thanks Philip