Mkdir does not work in execution phase of my task


I have the following task:

  task processMailTemplates {
    description "Processes mail templates"
    dependsOn npmInstall

    outputs.upToDateWhen { false }

    def templateSrcDir = "src/main/templates/mail/"
    def templateDestDir = "$buildDir/generated-mail-templates/META-INF/templates/mail/"
    doFirst {
        mkdir templateDestDir

    doLast {
        exec {
            //Fails because template destination directory does not exist
            commandLine "ls $templateDestDir"

        def templateNames = []

        fileTree(dir: templateSrcDir, include: '**/*.html').visit {
            FileVisitDetails details -> templateNames <<

        templateNames.each { templateName -> inlineCss(templateSrcDir + templateName, templateDestDir + templateName) }

I am trying to create a directory from the doFirst closure but it does not work for some reason: the ls $templateDir fails…

Can anyone please help ?

edit: I also have the following sourceSets configuration:

  sourceSets {
    main {
        output.dir("$buildDir/generated-mail-templates", builtBy: 'processMailTemplates')

I am realizing that my issue is related to the way I used the commandLine param of exec.

If I use the following syntax (notice the comma): commandLine "ls", templateDestDir, then the ls command works. Therefore, it seems the directory was indeed created.