Hi,
Anyone know of a good example for passing complex objects to a task via a closure or any other method?
Thanks
I’m trying to pass a complex object to a custom task I’m writing and I’m not sure how to handle it.
Goal: create BuildValidationTask that takes a collection of expected output and fails the build if its not present.
I don’t quite know how to write my task so I can pass a single or collection of validation Objects which contain a boolean and two fileTrees each.
Imagined Task Config
task checkBuildOutput(type:BuildValidationTask) {
artifacts {
test { failOnError: false, expected: fileTree(dir: “.”, includes [“/target/surefire-reports/xml"]) }
installer { failOnError: true, expected: fileTree(dir: “.”, includes ["/target/myProduct.tar.gz"]) }
internalLicense {failOnError: true, prohibited: fileTree(dir: “.”, includes ["/target/licenseGenerator.exe”]) }
}
complex object to pass to task artifactValidationSpec
class ArtifactValidationSpec {
String name
private FaultType fType
private FileTree expected = null
private FileTree prohibited = null
public ArtifactValidationSpec(String name, boolean failOnError, expected = null, prohibited = null) { this.name = name if (failOnError) { fType = FaultType.FAIL } else { fType = FaultType.WARN} this.expected = expected this.prohibited = prohibited } public ArtifactValidationSpec(String name, FaultType fType, expected = null, prohibited = null) { this.name = name this.fType = fType this.expected = expected this.prohibited = prohibited }
public String toString() { return """${name}[${fType.desc}]
Expected: ${expected.toString()} : ${expected.getFiles()}
Prohibited: ${prohibited.toString()} : ${prohibited.getFiles()}“”"
}
/** * * @return true if expected criteria found */ public boolean foundExpected() { return expected.getFiles().size() > 0 }
/** * * @return true if not prohibited criteria found */ public boolean foundProhibited() { return prohibited.getFiles().size() > 0 }
/** * * @return true we find expected and not prohibited content */ public boolean isValid() { return foundExpected() && ! foundProhibited() }
}
Task
class BuildValidationTask extends MyTask {
@Input public ArtifactValidationSpec artifact
@TaskAction public void checkOutput() { if (! artifact.foundExpected()) { logger.lifecycle("Error: ${artifact.name} missing (spec: ${artifact.toString()}") } if (! artifact.foundProhibited()) { logger.lifecycle("Error: ${artifact.name} prohibited content found (spec: ${artifact.toString()}") } }
}