Ok, this time, I’m most likely just being a gradle noob.
I have a lot of almost identical repetitive tasks, that I naturally want to create a custom task (perhaps even a plugin) for.
That means, I want to define a plugin, an extension, and a task, and I want these to correctly resolve my input files (depending on extension/task configuration), and output files (depending on the configuration).
So, in simple terms, this is what I have:
class GenbeansPlugin implements Plugin<Project> {
void apply(Project project) {
project.extensions.create("genbeans", GenbeansExtension)
project.task('genbeans', type: GenbeansTask)
project.compileJava.dependsOn += 'genbeans'
}
}
class GenbeansExtension {
def generatedSrc = "src/generated/java"
String projectName = "MyProject"
String projectPackage = "/com/core/component"
String dataSrc = "src/resources/ejbeans"
String templateSrc = "src/resources/templates"
}
class GenbeansTask extends DefaultTask {
def generatedSrc
String dataSrc
String templateSrc
String projectName
String projectPackage
GenbeansTask() {
description = 'Generates java beans, servlets and services based on the beans from src/resources/beans'
project.afterEvaluate {
def ext = project.getExtensions().findByName("genbeans")
if (!ext) {
ext = new GenbeansExtension()
}
generatedSrc = generatedSrc ?: ext.getGeneratedSrc()
projectName = projectName ?: ext.getProjectName()
projectPackage = projectPackage ?: ext.getProjectPackage()
dataSrc = dataSrc ?: ext.getDataSrc()
templateSrc = templateSrc ?: ext.getTemplateSrc()
inputs.dir templateSrc
inputs.sourceDir project.file(dataSrc)
generatedSrc = project.file("src/generated/java")
inputs.getSourceFiles().each { File f ->
def bname = baseName(f)
outputs.file project.file("$generatedSrc/$projectPackage/ejb/generated/beans/Abstract${bname}Bean.java")
}
}
}
@TaskAction
void genbeans(IncrementalTaskInputs inputs) {
// Do logic
}
}
Now, this is annoying, esp. the need for afterEvaluate clause.
But, I cannot fathom how else I can initialize the task with the values from the extension (if available), and still support overruling the values when using the task directly, and still get a chance to setup the input and output files when the task is NOT run (i.e. the TaskAction is not executed, when the input-files have not changed).
So what am I missing here?
Is there a “doAfterInitializationButBeforeLastAndAlwaysDoThisBeforeUpToDateCheckAnnotation”?
EDIT: Thunderstruck, am I simply ignoring doFirst?
EDIT2: Nope, doFirst do not accomplish what I need. Any help?