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();
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);
}
}

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.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<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) {
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();
}
}
}
}
}
}
}