在 DefaultTask Class 中复制并重命名文件?
Copy and rename a File inside a DefaultTask Class?
我想复制我的 srcDir
中名称中包含 $$ 的所有文件。例如,如果一个文件是:
x$$y.java
我想创建该文件的副本并将其命名为 x$y.java
。
class MyTask extends DefaultTask {
@InputDirectory File srcDir
@TaskAction
def task() {
def srcFiles = project.files(project.fileTree(dir: srcDir)).getFiles()
srcFiles.each { file ->
if (file.name.contains("$$")) {
// TODO copy file and rename it to the same name with one dollar sign in the middle
}
}
}
}
如何在自定义任务中复制和重命名文件 class?
您可以使用 Files class:
if (file.name.contains("$$")) {
// TODO copy file and rename it to the same name with one dollar sign in the middle
String newFilePath = file.getParent()+"\"+ "YOUR NEW File"; // use separator which you want
File newFile = new File(newFilePath);
Files.copy(file.toPath(), newFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
有关更多选项,请参阅 StandardCopyOption。
尝试:
@TaskAction
def task() {
project.copy {
from(project.fileTree(dir: srcDir).files) {
include {
it.file.name.contains('$$')
}
}
into('somewhere')
rename { name ->
name.replace('$$', '$')
}
}
}
我想复制我的 srcDir
中名称中包含 $$ 的所有文件。例如,如果一个文件是:
x$$y.java
我想创建该文件的副本并将其命名为 x$y.java
。
class MyTask extends DefaultTask {
@InputDirectory File srcDir
@TaskAction
def task() {
def srcFiles = project.files(project.fileTree(dir: srcDir)).getFiles()
srcFiles.each { file ->
if (file.name.contains("$$")) {
// TODO copy file and rename it to the same name with one dollar sign in the middle
}
}
}
}
如何在自定义任务中复制和重命名文件 class?
您可以使用 Files class:
if (file.name.contains("$$")) {
// TODO copy file and rename it to the same name with one dollar sign in the middle
String newFilePath = file.getParent()+"\"+ "YOUR NEW File"; // use separator which you want
File newFile = new File(newFilePath);
Files.copy(file.toPath(), newFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
有关更多选项,请参阅 StandardCopyOption。
尝试:
@TaskAction
def task() {
project.copy {
from(project.fileTree(dir: srcDir).files) {
include {
it.file.name.contains('$$')
}
}
into('somewhere')
rename { name ->
name.replace('$$', '$')
}
}
}