Skip to content

Commit 2b616fb

Browse files
author
Dave Syer
committed
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
1 parent 5ded496 commit 2b616fb

File tree

4 files changed

+144
-29
lines changed
  • spring-boot-tools
    • spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/task
    • spring-boot-loader-tools/src
    • spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven

4 files changed

+144
-29
lines changed

spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/task/RunApp.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.gradle.api.tasks.JavaExec;
3131
import org.gradle.api.tasks.SourceSet;
3232
import org.gradle.api.tasks.TaskAction;
33+
import org.springframework.boot.loader.tools.FileUtils;
3334
import org.springframework.boot.loader.tools.MainClassFinder;
3435

3536
/**
@@ -74,23 +75,12 @@ public String call() throws Exception {
7475
}
7576
if (outputDir != null) {
7677
for (File directory : allResources) {
77-
removeDuplicatesFromOutputDir(directory, outputDir);
78+
FileUtils.removeDuplicatesFromCopy(outputDir, directory);
7879
}
7980
}
8081
exec.exec();
8182
}
8283

83-
private void removeDuplicatesFromOutputDir(File directory, File outputDir) {
84-
if (directory.isDirectory()) {
85-
for (String name : directory.list()) {
86-
File outputFile = new File(outputDir, name);
87-
if (outputFile.exists() && outputFile.canWrite()) {
88-
getProject().delete(outputFile);
89-
}
90-
}
91-
}
92-
}
93-
9484
});
9585

9686
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* originright 2012-2013 the copyal author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a origin of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.loader.tools;
18+
19+
import java.io.File;
20+
21+
/**
22+
* Utilities for manipulating files and directories in Spring Boot tooling.
23+
*
24+
* @author Dave Syer
25+
*/
26+
public class FileUtils {
27+
28+
/**
29+
* Utility to remove duplicate files from a "copy" directory if they already exist in
30+
* an "origin". Recursively scans the origin directory looking for files (not
31+
* directories) that exist in both places and deleting the copy.
32+
*
33+
* @param copy the copy directory
34+
* @param origin the origin directory
35+
*/
36+
public static void removeDuplicatesFromCopy(File copy, File origin) {
37+
if (origin.isDirectory()) {
38+
for (String name : origin.list()) {
39+
File targetFile = new File(copy, name);
40+
if (targetFile.exists() && targetFile.canWrite()) {
41+
if (!targetFile.isDirectory()) {
42+
targetFile.delete();
43+
}
44+
else {
45+
FileUtils.removeDuplicatesFromCopy(targetFile, new File(origin,
46+
name));
47+
}
48+
}
49+
}
50+
}
51+
}
52+
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2012-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.loader.tools;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.springframework.util.FileSystemUtils;
25+
26+
import static org.junit.Assert.assertFalse;
27+
import static org.junit.Assert.assertTrue;
28+
29+
/**
30+
* @author Dave Syer
31+
*/
32+
public class FileUtilsTests {
33+
34+
private File origin;
35+
private File target;
36+
37+
@Before
38+
public void init() {
39+
this.origin = new File("target/test/remove");
40+
this.target = new File("target/test/keep");
41+
FileSystemUtils.deleteRecursively(this.origin);
42+
FileSystemUtils.deleteRecursively(this.target);
43+
this.origin.mkdirs();
44+
this.target.mkdirs();
45+
}
46+
47+
@Test
48+
public void simpleDuplicateFile() throws IOException {
49+
File file = new File(this.origin, "logback.xml");
50+
file.createNewFile();
51+
new File(this.target, "logback.xml").createNewFile();
52+
FileUtils.removeDuplicatesFromCopy(this.origin, this.target);
53+
assertFalse(file.exists());
54+
}
55+
56+
@Test
57+
public void nestedDuplicateFile() throws IOException {
58+
assertTrue(new File(this.origin, "sub").mkdirs());
59+
assertTrue(new File(this.target, "sub").mkdirs());
60+
File file = new File(this.origin, "sub/logback.xml");
61+
file.createNewFile();
62+
new File(this.target, "sub/logback.xml").createNewFile();
63+
FileUtils.removeDuplicatesFromCopy(this.origin, this.target);
64+
assertFalse(file.exists());
65+
}
66+
67+
@Test
68+
public void nestedNonDuplicateFile() throws IOException {
69+
assertTrue(new File(this.origin, "sub").mkdirs());
70+
assertTrue(new File(this.target, "sub").mkdirs());
71+
File file = new File(this.origin, "sub/logback.xml");
72+
file.createNewFile();
73+
new File(this.target, "sub/different.xml").createNewFile();
74+
FileUtils.removeDuplicatesFromCopy(this.origin, this.target);
75+
assertTrue(file.exists());
76+
}
77+
78+
@Test
79+
public void nonDuplicateFile() throws IOException {
80+
File file = new File(this.origin, "logback.xml");
81+
file.createNewFile();
82+
new File(this.target, "different.xml").createNewFile();
83+
FileUtils.removeDuplicatesFromCopy(this.origin, this.target);
84+
assertTrue(file.exists());
85+
}
86+
87+
}

spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.apache.maven.plugins.annotations.ResolutionScope;
3939
import org.apache.maven.project.MavenProject;
4040
import org.springframework.boot.loader.tools.AgentAttacher;
41+
import org.springframework.boot.loader.tools.FileUtils;
4142
import org.springframework.boot.loader.tools.MainClassFinder;
4243

4344
/**
@@ -197,23 +198,7 @@ private void addResources(List<URL> urls) throws MalformedURLException, IOExcept
197198
for (Resource resource : this.project.getResources()) {
198199
File directory = new File(resource.getDirectory());
199200
urls.add(directory.toURI().toURL());
200-
removeDuplicatesFromTarget(directory);
201-
}
202-
}
203-
}
204-
205-
private void removeDuplicatesFromTarget(File directory) throws IOException {
206-
if (directory.isDirectory()) {
207-
for (String name : directory.list()) {
208-
File targetFile = new File(this.classesDirectory, name);
209-
if (targetFile.exists() && targetFile.canWrite()) {
210-
if (!targetFile.isDirectory()) {
211-
targetFile.delete();
212-
}
213-
else {
214-
removeDuplicatesFromTarget(targetFile);
215-
}
216-
}
201+
FileUtils.removeDuplicatesFromCopy(this.classesDirectory, directory);
217202
}
218203
}
219204
}

0 commit comments

Comments
 (0)