We need to build multiple distributions of our products and we’ve decided to have each project build multiple .war files. The .war files are quite similar; mostly, we need to apply some distribution-specific changes to some files before adding them to the .war file. So we’ve defined our own custom War task that looks like this:
public class OurWar extends War {
String cssPath = "/some/default/css/path"
String jsPath = "/some/default/js/path"
OurWar() {
super()
copyAction.rootSpec.into('css') {
from cssPath
}
copyAction.rootSpec.into('js') {
from jsPath
}
// Other stuff
}
}
and in our build.gradle we have something like this:
Unfortunately, this doesn’t work, because the copy actions for platformYWar are configured before we have a chance to set ‘cssPath’ to something else. We tried moving the copy actions out of the OurWar constructor into a doFirst() block. It resulted in those files not being included in the war files at all. So finally we had to change OurWar to look like this:
public class OurWar extends War {
String cssPath = /some/default/css/path
String jsPath = /some/default/js/path
OurWar() {
super()
// Other stuff
}
void setupStandardCopyActions() {
copyAction.rootSpec.into('css') {
from cssPath
}
copyAction.rootSpec.into('js') {
from jsPath
}
}
}
Now everything works. However, it seems very ugly/hacky to have to call setupStandardCopyAction() at the end of every war task. Basically, it seems like we could really use a post-config (or rather “end-of-config”) hook on the Task class. Does anything like this exist? What’s the best way to do this?
A straightforward solution is to use ‘from { cssPath }’ and ‘from { jsPath }’ in the constructor. This will defer the evaluation of ‘cssPath’ and ‘jsPath’ until execution time.
The latter declaration is invalid. It effectively passes ‘{ into ‘warPath’ }’ as the ‘from’ (whatever that means), with ‘path’ getting lost. Instead, it would have to be ‘from(‘path’) { into ‘warPath’ }’. Or, to get lazyness for the ‘from’ part, ‘from({ ‘path’ }) { into ‘warPath’ }’.