java 问题 - 创建一个带有 gradle 的目录不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25241895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:40:14  来源:igfitidea点击:

issue - creating a directory with gradle not working

javashellgradle

提问by Edward Q. Bridges

I have the following build.gradle:

我有以下几点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 wrapperI then run:

使用gradle wrapperI下载包装器后,然后运行:

/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.
  • 这是在 MacOS Mavericks 上。
  • 执行用户能够在 shell 中创建目录。
  • 有足够的磁盘空间。

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.

请告知我在 Gradle 中可能做错了什么,导致无法创建目录,但 Gradle 表明它已成功?任何故障排除提示将不胜感激。

Thanks!

谢谢!

回答by Mike Rocke

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")
    }
}