2015-03-25 45 views
1

以下是我的代码:运行命令行是不正确的摇篮

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

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

有谁知道这里发生了什么?

+0

是的,'最后一次迭代each'上'调用files'对象设置配置了和*胜* - 您可以更改为了验证它。 'native2ascii'能够同时处理多个文件吗? – Opal 2015-03-25 05:45:07

回答

0

是的,每个调用上的文件的最后一次迭代对象集的配置并赢 - 你可以改变传递给验证文件的顺序。

native2ascii任务是内置的默认gradle这个,你可以尝试例如为:

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

似乎没有必要通过Exec任务去做。

+0

谢谢,欧泊。我对Gradle中的ant方法并不熟悉。现在我知道了。对于上面的脚本,当我在此文件夹中进行一些更改时,我的错误是迭代文件夹下的文件。 – Sky 2015-03-25 08:47:47

+0

@Sky,不客气。如果问题已解决,请接受答案。 – Opal 2015-03-25 08:51:54

+0

对不起。我该如何接受答案? – Sky 2015-03-25 09:05:18

-1

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

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