Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create A Directory Before Several Gradle Tasks

Tags:

gradle

I have several tasks that require a sub-directory of build to exist.

What is the best way to achieve this?

So far I have this:

afterEvaluate {
    mkdir "$buildDir/work"
}

And this:

task prepareDirs {
    mkdir "$buildDir/test"
}

The second one works because it is run in the configuration phase and so I don't need to put dependsOn on all the tasks that require it.

  • Are there other options?
  • Is mkdir even the right way to do it?
  • At what point is the buildDir created? Can I extend that task?

EDIT: I noticed that both of these fail if I chain the clean task on the command line before my main tasks.

like image 368
opticyclic Avatar asked Dec 30 '25 03:12

opticyclic


1 Answers

The following should work. Note that prepareDirs must have constraint mustRunAfter clean, to solve the issue you mentioned in your EDIT

// task responsible to create the tmp/work dir
task prepareDirs {
    mustRunAfter clean
    doLast {
        mkdir "$buildDir/work"
    }
}

// make all tasks depend on the prepareDirs task
// exept 'clean' and 'prepareDirs' (avoid circular dependency)
tasks.each {
    if (it != prepareDirs && it != clean) {
        it.dependsOn prepareDirs
    }
}

If you don't have much tasks that require the build/work dir and don't want to make all tasks depend on you prepareDirs task, you can do that :

[myTask1, myTask2, myTask3].each {
    it.dependsOn prepareDirs
}
like image 90
M.Ricciuti Avatar answered Jan 01 '26 23:01

M.Ricciuti