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 db885b0ef06..46c48559a77 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 @@ -52,8 +52,7 @@ public class RunApp extends DefaultTask { SourceDirectorySet resources = main.getResources(); allResources.addAll(resources.getSrcDirs()); outputs = main.getOutput().getResourcesDir(); - } - else { + } else { outputs = null; } @@ -80,11 +79,16 @@ public class RunApp extends DefaultTask { getLogger().info("Found main: " + mainClass); } if (outputs != null) { - // Special case: this file causes logback to worry that it has been - // configured twice, so remove it from the target directory... - File logback = new File(outputs, "logback.xml"); - if (logback.exists()) { - logback.delete(); + // remove duplicates from resources and build + for (File directory : allResources) { + if (directory.isDirectory()) { + for (String name : directory.list()) { + File file = new File(outputs, name); + if (file.exists() && file.canWrite()) { + getProject().delete(file); + } + } + } } } exec.exec(); @@ -100,8 +104,7 @@ public class RunApp extends DefaultTask { getLogger().info("Looking for main in: " + main.getOutput().getClassesDir()); try { return MainClassFinder.findMainClass(main.getOutput().getClassesDir()); - } - catch (IOException ex) { + } catch (IOException ex) { throw new IllegalStateException("Cannot find main class", ex); } } 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 c840664fe94..5ffccaebc58 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 @@ -37,6 +37,7 @@ import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.ResolutionScope; import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.util.FileUtils; import org.springframework.boot.loader.tools.AgentAttacher; import org.springframework.boot.loader.tools.MainClassFinder; @@ -62,10 +63,7 @@ public class RunMojo extends AbstractMojo { /** * Add maven resources to the classpath directly, this allows live in-place editing or - * resources. Since resources will be added directly, and via the target/classes - * folder they will appear twice if ClassLoader.getResources() is called. In practice - * however most applications call ClassLoader.getResource() which will always return - * the first resource. + * resources. */ @Parameter(property = "run.addResources", defaultValue = "true") private boolean addResources; @@ -185,6 +183,9 @@ public class RunMojo extends AbstractMojo { catch (MalformedURLException ex) { throw new MojoExecutionException("Unable to build classpath", ex); } + catch (IOException ex) { + throw new MojoExecutionException("Unable to build classpath", ex); + } } private void addUserDefinedFolders(List urls) throws MalformedURLException { @@ -195,16 +196,25 @@ public class RunMojo extends AbstractMojo { } } - private void addResources(List urls) throws MalformedURLException { + private void addResources(List urls) throws MalformedURLException, IOException { if (this.addResources) { for (Resource resource : this.project.getResources()) { - urls.add(new File(resource.getDirectory()).toURI().toURL()); - } - // Special case: this file causes logback to worry that it has been configured - // twice, so remove it from the target directory... - File logback = new File(this.classesDirectory, "logback.xml"); - if (logback.exists() && logback.canWrite()) { - logback.delete(); + File directory = new File(resource.getDirectory()); + urls.add(directory.toURI().toURL()); + if (directory.isDirectory()) { + // Remove duplicates from the target directory... + for (String name : directory.list()) { + File file = new File(this.classesDirectory, name); + if (file.exists() && file.canWrite()) { + if (file.isDirectory()) { + FileUtils.deleteDirectory(file); + } + else { + file.delete(); + } + } + } + } } } }