but this didn’t work because the configurations of the project (project.configurations.testCompile) don’t exist at configuration time, so I need to do this at run time.
The other question I have is how do I only run this if there is a new dependency (right now it unzips every time even if the event types have already been extracted from the jar)
The common solution is to defer the evaluation of the ‘from’ value (including ‘schemaJarFile’) by wrapping it with a closure. That’s a common pattern supported by many Gradle APIs. Whether an API supports this style can be determined by checking the Gradle Build Language Reference.
One reason why task types such as ‘Copy’ should be preferred over ad-hoc solutions is that you’ll get up-to-date checks for free.
task installEventTypes(type: Copy) << {
from {
def schemaJarFile = project.configurations.testCompile.find { File file -> file.absolutePath =~ /eda-eventtypes.*schemas/ }
if (!schemaJarFile) {
throw new Exception("eda eventtypes schema not found!")
}
zipTree(schemaJarFile)
}
def outputDir = file("${buildDir}/eda-eventtypes")
into outputDir
println "EDA event types extracted to $outputPath"
}
I think the “from {…}” is really “from(Closure cl)”. Instead of executing the closure immediately, “from” stores it and evaluates it later in config cycle.
* Where:
Build file '/Users/philswenson/dev/sag/optimize_l/modules/ae/build.gradle' line: 148
* What went wrong:
Execution failed for task ':modules:bam-ae:classes'.
> The task artifact state cache (/Users/philswenson/dev/sag/optimize_l/.gradle/1.5/taskArtifacts) has not been locked.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Yeah, you’re quick! I was just editing to correct it. I also changed “doFirst” to “doLast” from another late night mis-read. Can “mustRunAfter” and “dependsOn” can be used together to ensure that “installEventTypes” runs after classes?
Here’s the corrected code:
classes {
doLast {
copy {
from {
def schemaJarFile = project.configurations.testCompile.find {File file ->
file.absolutePath =~ /eda-eventtypes.*schemas/
}
if (!schemaJarFile) {
throw new Exception("eda eventtypes schema not found!")
}
zipTree(schemaJarFile)
}
into file("$buildDir/eda-eventtypes")
logger.warn "EDA event types extracted to $outputPath"
}
}
}
I doubt that it’s necessary for ‘installEventTypes’ to run after ‘classes’. ‘classes.dependsOn(installEventTypes)’ should do just fine. Then again, I don’t understand why one would unpack what seem to be test-related classes (‘configurations.testCompile’) as part of ‘classes’…
So I guess that’s a “no” on the dependsOn/mustRunAfter reversing the order. I assume that would error out as a cyclical dependency, but I was scratching my head about that earlier today.
You are correct, Peter… I don’t really need it to run after now that I think about it. So your suggestion works nicely. BTW, the reason I’m extracting the jar is it contains a bunch of XSDs that my tests need extracted.