In my build file I have the following snippet:
subprojects {
jar
{
onlyIf {
compileJava.didWork
}
}
}
(and I have to say, that is one beautiful DSL…onlyIf { compileJava.didWork }. Wow. Gradle team, I lift my hat in admiration)
however, even in the projects (this is a multi project build) where the jar task is “SKIPPED”, the “build” directory is still created. Digging into the jar task source we find the following:
//Jar.groovy in gradle source
public class Jar extends Zip {
<snip>
Jar() {
extension = DEFAULT_EXTENSION
manifest = new DefaultManifest(project.fileResolver)
// Add these as separate specs, so they are not affected by the changes to the main spec
metaInf = copyAction.rootSpec.addFirst().into('META-INF')
metaInf.addChild().from {
MapFileTree manifestSource = new MapFileTree(temporaryDir)
manifestSource.add('MANIFEST.MF') {OutputStream outstr ->
Manifest manifest = getManifest() ?: new DefaultManifest(null)
manifest.writeTo(new OutputStreamWriter(outstr))
}
return new FileTreeAdapter(manifestSource)
}
copyAction.mainSpec.eachFile { FileCopyDetails details ->
if (details.path.equalsIgnoreCase('META-INF/MANIFEST.MF')) {
details.exclude()
}
}
}
Note the use of mysterious variable ‘temporaryDir’? Well he comes from the following snippet in AbstractTask.java a bunch of inheritance levels up the chain:
//AbstractTask.java in gradle source
public File getTemporaryDir() {
File dir = getServices().get(TemporaryFileProvider.class).newTemporaryFile(getName());
dir.mkdirs();
return dir;
}
So to summarize: just by creating the ‘Jar’ task, we access the ‘temporaryDir’ property which in turn creates the directory “build/tmp” under the executing project dir.
Wouldn’t it be nice if we were a tad more lazy about this evaluation? I have a large multi-project build where I would like to leave the 50 sub projects which do not have any sources or resources untouched which is why I added the onlyIf clause.
Right now we get “build/tmp” directories in them even when we prevent the jar task from executing and as far as I can see there is no way to apply either the groovy or java plugins without getting a ‘build/tmp’ directory however much we prevent the tasks from running for that specific subproject.
Any help much appreciated. If I don’t hear back from anybody on a way to work around this within a few days, I’ll open a JIRA and close this thread.