阻止 Gradle 修改我的 image/file 副本属性信息
Stop Gradle from modifying my image/file attribute info on copy
我有一个应用程序可以根据上次修改日期等属性数据对图像进行排序。在我的单元测试资源文件夹中,我有图像 test/resources/folder,在调用 "gradle build" 时会自动复制到 build/resources/test。
问题是我需要文件具有相同的最后修改日期才能进行测试。
是否可以在我的构建脚本中使用 javas Files.copy 来移动数据并维护它的属性?或者有没有办法告诉 gradle 停止弄乱我的文件?
Gradle 任务:
task copyImages(type :Copy){
from 'src/test/resources'
into 'build/resources/test'
}
更新:
根据反馈,我的解决方案是使用 JavaExec
Gradle 构建脚本:
apply plugin: 'java'
repositories{
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.+'
}
task(moveImages, dependsOn: 'classes', type: JavaExec){
main = 'com.lifehug.support.TestSupport'
classpath = sourceSets.test.runtimeClasspath
args 'src/test/resources/navigator', 'build/resources/test/navigator'
}
defaultTasks 'moveImages'
然后这是我用来移动图像的 java 文件
Java 文件:
public class TestSupport{
public static void main(String[] args) throws IOException {
if( args.length < 2) return;
final Path sourceDir = Paths.get(args[0]);
final Path targetDir = Paths.get(args[1]);
Files.walkFileTree(sourceDir, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
Path target = targetDir.resolve(sourceDir.relativize(dir));
Files.copy(dir, target, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.copy(file, targetDir.resolve(sourceDir.relativize(file)), StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
return FileVisitResult.CONTINUE;
}
});
}
}
正在 Gradle Copy task documentation I could not find a way for preserving the file timestamp. Doing some more research apparently there's an open issue 检查。
作为解决方法,您可以使用其他方式进行复制,例如使用 ant.copy 作为 @dnault suggested or just use java code (see examples here 用于 Java 6 和 Java 7).
我有一个应用程序可以根据上次修改日期等属性数据对图像进行排序。在我的单元测试资源文件夹中,我有图像 test/resources/folder,在调用 "gradle build" 时会自动复制到 build/resources/test。
问题是我需要文件具有相同的最后修改日期才能进行测试。
是否可以在我的构建脚本中使用 javas Files.copy 来移动数据并维护它的属性?或者有没有办法告诉 gradle 停止弄乱我的文件?
Gradle 任务:
task copyImages(type :Copy){
from 'src/test/resources'
into 'build/resources/test'
}
更新:
根据反馈,我的解决方案是使用 JavaExec
Gradle 构建脚本:
apply plugin: 'java'
repositories{
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.+'
}
task(moveImages, dependsOn: 'classes', type: JavaExec){
main = 'com.lifehug.support.TestSupport'
classpath = sourceSets.test.runtimeClasspath
args 'src/test/resources/navigator', 'build/resources/test/navigator'
}
defaultTasks 'moveImages'
然后这是我用来移动图像的 java 文件
Java 文件:
public class TestSupport{
public static void main(String[] args) throws IOException {
if( args.length < 2) return;
final Path sourceDir = Paths.get(args[0]);
final Path targetDir = Paths.get(args[1]);
Files.walkFileTree(sourceDir, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
Path target = targetDir.resolve(sourceDir.relativize(dir));
Files.copy(dir, target, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.copy(file, targetDir.resolve(sourceDir.relativize(file)), StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
return FileVisitResult.CONTINUE;
}
});
}
}
正在 Gradle Copy task documentation I could not find a way for preserving the file timestamp. Doing some more research apparently there's an open issue 检查。
作为解决方法,您可以使用其他方式进行复制,例如使用 ant.copy 作为 @dnault suggested or just use java code (see examples here 用于 Java 6 和 Java 7).