运行 Gradle 中的命令行不正确

Running command line is not correct in Gradle

以下是我的代码:

task encodeFile(type: Exec) {
    workingDir dirName
    def files = file(dirName).listFiles()
    files.each { File file ->
        if (file.isFile()) {
            println " *** $file.name *** "
            def tmpName = "tmp$file.name"
            println " === $tmpName"
            commandLine "cmd", "/c native2ascii $file.name $tmpName"
            commandLine "cmd", "/c del $file.name"
            commandLine "cmd", "/c move $tmpName $file.name"
//            commandLine "cmd", "/c move $file.name $tmpName"
            println " === $file.name is moved"
            println "----------------------------------"

//            """executable "native2ascii" "$file.name" "$tmpName""""".execute()
        }
    }
}

我正在尝试对指定文件夹下的所有文件进行本地化编码。但是当我 运行 上面的代码时,只有最后一个文件按预期更改。我打印了一些消息,所有文件都被迭代了。

有人知道这里发生了什么吗?

是的,每个在文件对象上调用的最后一次迭代设置配置并获胜 - 您可以更改传递的文件的顺序以验证它。

native2ascii任务默认内置gradle,你可以试试例如:

task n2a {
   doLast {
      ant.native2ascii(
         src   : project.file('n2a-src'),
         dest  : project.file('n2a-dest') 
      )
   }
}

好像没必要通过Exec任务来做。

对我来说这是正常工作的:

processResources {
    "*.properties, *.txt".split(",").each {pattern ->
        filesMatching ("**/" + pattern.trim ()) {
            filter {
                it
                    .replace ('${projectVersion}', project.version)
                    .replace ('${projectName}', project.name)
            }
        }
    }

    doLast {
        if (file (processResources.destinationDir).exists()) {
            ant.native2ascii (
                src: "${processResources.destinationDir}",
                dest: "${processResources.destinationDir}/../ascii",
                includes: '**/*.properties',
            )

            copy {
                from "${processResources.destinationDir}/../ascii"
                into processResources.destinationDir
            }
        }
    }
}