Gradle up-to-date on Exec tasks

Hi I have the following situation:

task foo(type:Exec) {
     ext.dirName = ""
     outputs.dir = dirName
     doFirst {
         workingDir "."
         executable "sh"
         def mkdir = 'mkdir -p ' + dirName
         args '-c', mkdir
   foo.dirName = "/tmp/bar"
 compileJava.dependsOn foo

The problem is the property dirName is only available during doFirst, how do set outputs.dir based on this property. It throws an exception, since the default driName is empty. Also if I put outputs.dir inside doFirst, gradle complain that this is deprecated and it does not work.

You can first set a dummy dirName, although that name will then be used for the up-to-date check. For a mkdir, it’s not worth to have an up-to-date check; also you could just use ‘project.mkdir()’. Another alternative is to use ‘project.exec()’, possibly within a task class whose output dir property is computed on-demand.

Hi Peter thanks for the reply. I am not sure why we dont need an up-to-date check. This is not the only use case where we need parameterized tasks. We need to do it for various other reasons, for files being generated etc. So how do I do that? We dont want the build phase to regenerate files that are already generated. Do you have an example of the project.exec() solution that you mention.

Sorry again for all these questions since I am a noob gradle user.