Remove duplicate resources from classpath

We had been making a special case for logback.xml anyway, so
extending that to simply deleting recursively all of
src/main/resources (or equivalent) from target/classes (or
equivalent) seems like it's perfectly justifiable.

Fixes gh-451
This commit is contained in:
Dave Syer 2014-03-10 16:56:55 +00:00
parent 6657e3ef84
commit 4d172ca742
2 changed files with 34 additions and 21 deletions

View File

@ -52,8 +52,7 @@ public class RunApp extends DefaultTask {
SourceDirectorySet resources = main.getResources(); SourceDirectorySet resources = main.getResources();
allResources.addAll(resources.getSrcDirs()); allResources.addAll(resources.getSrcDirs());
outputs = main.getOutput().getResourcesDir(); outputs = main.getOutput().getResourcesDir();
} } else {
else {
outputs = null; outputs = null;
} }
@ -80,11 +79,16 @@ public class RunApp extends DefaultTask {
getLogger().info("Found main: " + mainClass); getLogger().info("Found main: " + mainClass);
} }
if (outputs != null) { if (outputs != null) {
// Special case: this file causes logback to worry that it has been // remove duplicates from resources and build
// configured twice, so remove it from the target directory... for (File directory : allResources) {
File logback = new File(outputs, "logback.xml"); if (directory.isDirectory()) {
if (logback.exists()) { for (String name : directory.list()) {
logback.delete(); File file = new File(outputs, name);
if (file.exists() && file.canWrite()) {
getProject().delete(file);
}
}
}
} }
} }
exec.exec(); exec.exec();
@ -100,8 +104,7 @@ public class RunApp extends DefaultTask {
getLogger().info("Looking for main in: " + main.getOutput().getClassesDir()); getLogger().info("Looking for main in: " + main.getOutput().getClassesDir());
try { try {
return MainClassFinder.findMainClass(main.getOutput().getClassesDir()); return MainClassFinder.findMainClass(main.getOutput().getClassesDir());
} } catch (IOException ex) {
catch (IOException ex) {
throw new IllegalStateException("Cannot find main class", ex); throw new IllegalStateException("Cannot find main class", ex);
} }
} }

View File

@ -37,6 +37,7 @@ import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope; import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject; 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.AgentAttacher;
import org.springframework.boot.loader.tools.MainClassFinder; 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 * 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 * resources.
* 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.
*/ */
@Parameter(property = "run.addResources", defaultValue = "true") @Parameter(property = "run.addResources", defaultValue = "true")
private boolean addResources; private boolean addResources;
@ -185,6 +183,9 @@ public class RunMojo extends AbstractMojo {
catch (MalformedURLException ex) { catch (MalformedURLException ex) {
throw new MojoExecutionException("Unable to build classpath", ex); throw new MojoExecutionException("Unable to build classpath", ex);
} }
catch (IOException ex) {
throw new MojoExecutionException("Unable to build classpath", ex);
}
} }
private void addUserDefinedFolders(List<URL> urls) throws MalformedURLException { private void addUserDefinedFolders(List<URL> urls) throws MalformedURLException {
@ -195,16 +196,25 @@ public class RunMojo extends AbstractMojo {
} }
} }
private void addResources(List<URL> urls) throws MalformedURLException { private void addResources(List<URL> urls) throws MalformedURLException, IOException {
if (this.addResources) { if (this.addResources) {
for (Resource resource : this.project.getResources()) { for (Resource resource : this.project.getResources()) {
urls.add(new File(resource.getDirectory()).toURI().toURL()); File directory = new File(resource.getDirectory());
} urls.add(directory.toURI().toURL());
// Special case: this file causes logback to worry that it has been configured if (directory.isDirectory()) {
// twice, so remove it from the target directory... // Remove duplicates from the target directory...
File logback = new File(this.classesDirectory, "logback.xml"); for (String name : directory.list()) {
if (logback.exists() && logback.canWrite()) { File file = new File(this.classesDirectory, name);
logback.delete(); if (file.exists() && file.canWrite()) {
if (file.isDirectory()) {
FileUtils.deleteDirectory(file);
}
else {
file.delete();
}
}
}
}
} }
} }
} }