issue - creating a directory with gradle not worki

2019-07-01 22:55发布

问题:

I have the following build.gradle:

task wrapper(type: Wrapper) {
    gradleVersion = '2.0'
}

apply plugin: 'java'

task filter(type: Copy) {
    def subdir = new File(project.buildDir, "subdir")
    println("subdir: "+subdir)

    if( !subdir.exists() ) {
        println("creating subdir")

        if(subdir.mkdirs()) {
            println("succeeded in making folder")

            if(subdir.exists()) {
                println("folder exists")
            } else {
                println("folder does not exist")
            }
        } else {
            println("failed to make folder")
        }
    } else {
        println("folder exists already")
    }
}

After downloading the wrapper using gradle wrapper I then run:

/tmp/test $ ./gradlew clean filter
subdir: /tmp/test/build/subdir
creating subdir
succeeded in making folder
folder exists
:clean
:filter UP-TO-DATE

BUILD SUCCESSFUL

Total time: 4.121 secs

Which seems to indicate everything went okay. However, when I double check I get this:

/tmp/test $ ls -l /tmp/test/build/subdir
ls: /tmp/test/build/subdir: No such file or directory

Notes:

  • This is on MacOS Mavericks.
  • The executing user is able to create the directory at the shell.
  • There is sufficient disk space.

Please advise what I might be doing wrong here with Gradle that fails to create the directory, but that Gradle indicates it was successful? Any troubleshooting tips would be appreciated.

Thanks!

回答1:

Might be the copy task has done nothing, is "UP-TO-DATE".

13:21:40.045 [INFO] [org.gradle.api.internal.tasks.execution.SkipEmptySourceFile
sTaskExecuter] Skipping task ':filter' as it has no source files.

I think this is due to a Copy task requires a from and into.

Try creating a non copy task like,

task filter() << {
    def subdir = new File(project.buildDir, "subdir")
    println("subdir: "+subdir)

    if( !subdir.exists() ) {
        println("creating subdir")

        if(subdir.mkdirs()) {
            println("succeeded in making folder")

            if(subdir.exists()) {
                println("folder exists")
            } else {
                println("folder does not exist")
            }
        } else {
            println("failed to make folder")
        }
    } else {
        println("folder exists already")
    }
}