From 2b616fb7d94c8ec08ff8d750126d6470649fae72 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 2 Apr 2014 11:02:06 +0100 Subject: [PATCH] Extract logic for duplicate file removal so it can be shared .. between Maven and Gradle plugins. Also fixed bug in recursive scanning logic. Really fixes gh-614 --- .../boot/gradle/task/RunApp.java | 14 +-- .../boot/loader/tools/FileUtils.java | 53 +++++++++++ .../boot/loader/tools/FileUtilsTests.java | 87 +++++++++++++++++++ .../springframework/boot/maven/RunMojo.java | 19 +--- 4 files changed, 144 insertions(+), 29 deletions(-) create mode 100644 spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/FileUtils.java create mode 100644 spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/FileUtilsTests.java diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/task/RunApp.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/task/RunApp.java index 111415de908..d2e7387bff9 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/task/RunApp.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/task/RunApp.java @@ -30,6 +30,7 @@ import org.gradle.api.internal.file.collections.SimpleFileCollection; import org.gradle.api.tasks.JavaExec; import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.TaskAction; +import org.springframework.boot.loader.tools.FileUtils; import org.springframework.boot.loader.tools.MainClassFinder; /** @@ -74,23 +75,12 @@ public class RunApp extends DefaultTask { } if (outputDir != null) { for (File directory : allResources) { - removeDuplicatesFromOutputDir(directory, outputDir); + FileUtils.removeDuplicatesFromCopy(outputDir, directory); } } exec.exec(); } - private void removeDuplicatesFromOutputDir(File directory, File outputDir) { - if (directory.isDirectory()) { - for (String name : directory.list()) { - File outputFile = new File(outputDir, name); - if (outputFile.exists() && outputFile.canWrite()) { - getProject().delete(outputFile); - } - } - } - } - }); } diff --git a/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/FileUtils.java b/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/FileUtils.java new file mode 100644 index 00000000000..116f86d9e0d --- /dev/null +++ b/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/FileUtils.java @@ -0,0 +1,53 @@ +/* + * originright 2012-2013 the copyal author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a origin of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.loader.tools; + +import java.io.File; + +/** + * Utilities for manipulating files and directories in Spring Boot tooling. + * + * @author Dave Syer + */ +public class FileUtils { + + /** + * Utility to remove duplicate files from a "copy" directory if they already exist in + * an "origin". Recursively scans the origin directory looking for files (not + * directories) that exist in both places and deleting the copy. + * + * @param copy the copy directory + * @param origin the origin directory + */ + public static void removeDuplicatesFromCopy(File copy, File origin) { + if (origin.isDirectory()) { + for (String name : origin.list()) { + File targetFile = new File(copy, name); + if (targetFile.exists() && targetFile.canWrite()) { + if (!targetFile.isDirectory()) { + targetFile.delete(); + } + else { + FileUtils.removeDuplicatesFromCopy(targetFile, new File(origin, + name)); + } + } + } + } + } + +} diff --git a/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/FileUtilsTests.java b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/FileUtilsTests.java new file mode 100644 index 00000000000..e435bf29623 --- /dev/null +++ b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/FileUtilsTests.java @@ -0,0 +1,87 @@ +/* + * Copyright 2012-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.loader.tools; + +import java.io.File; +import java.io.IOException; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.util.FileSystemUtils; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * @author Dave Syer + */ +public class FileUtilsTests { + + private File origin; + private File target; + + @Before + public void init() { + this.origin = new File("target/test/remove"); + this.target = new File("target/test/keep"); + FileSystemUtils.deleteRecursively(this.origin); + FileSystemUtils.deleteRecursively(this.target); + this.origin.mkdirs(); + this.target.mkdirs(); + } + + @Test + public void simpleDuplicateFile() throws IOException { + File file = new File(this.origin, "logback.xml"); + file.createNewFile(); + new File(this.target, "logback.xml").createNewFile(); + FileUtils.removeDuplicatesFromCopy(this.origin, this.target); + assertFalse(file.exists()); + } + + @Test + public void nestedDuplicateFile() throws IOException { + assertTrue(new File(this.origin, "sub").mkdirs()); + assertTrue(new File(this.target, "sub").mkdirs()); + File file = new File(this.origin, "sub/logback.xml"); + file.createNewFile(); + new File(this.target, "sub/logback.xml").createNewFile(); + FileUtils.removeDuplicatesFromCopy(this.origin, this.target); + assertFalse(file.exists()); + } + + @Test + public void nestedNonDuplicateFile() throws IOException { + assertTrue(new File(this.origin, "sub").mkdirs()); + assertTrue(new File(this.target, "sub").mkdirs()); + File file = new File(this.origin, "sub/logback.xml"); + file.createNewFile(); + new File(this.target, "sub/different.xml").createNewFile(); + FileUtils.removeDuplicatesFromCopy(this.origin, this.target); + assertTrue(file.exists()); + } + + @Test + public void nonDuplicateFile() throws IOException { + File file = new File(this.origin, "logback.xml"); + file.createNewFile(); + new File(this.target, "different.xml").createNewFile(); + FileUtils.removeDuplicatesFromCopy(this.origin, this.target); + assertTrue(file.exists()); + } + +} diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java b/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java index 335e3806c08..27ee4f53b40 100644 --- a/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java +++ b/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java @@ -38,6 +38,7 @@ import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.ResolutionScope; import org.apache.maven.project.MavenProject; import org.springframework.boot.loader.tools.AgentAttacher; +import org.springframework.boot.loader.tools.FileUtils; import org.springframework.boot.loader.tools.MainClassFinder; /** @@ -197,23 +198,7 @@ public class RunMojo extends AbstractMojo { for (Resource resource : this.project.getResources()) { File directory = new File(resource.getDirectory()); urls.add(directory.toURI().toURL()); - removeDuplicatesFromTarget(directory); - } - } - } - - private void removeDuplicatesFromTarget(File directory) throws IOException { - if (directory.isDirectory()) { - for (String name : directory.list()) { - File targetFile = new File(this.classesDirectory, name); - if (targetFile.exists() && targetFile.canWrite()) { - if (!targetFile.isDirectory()) { - targetFile.delete(); - } - else { - removeDuplicatesFromTarget(targetFile); - } - } + FileUtils.removeDuplicatesFromCopy(this.classesDirectory, directory); } } }