This commit is contained in:
Phillip Webb
2014-03-14 16:17:08 -07:00
parent bb3ea39d80
commit 80ac1fb0cd
50 changed files with 469 additions and 366 deletions

View File

@@ -26,7 +26,6 @@ import java.util.concurrent.Callable;
import org.gradle.api.Action;
import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.file.SourceDirectorySet;
import org.gradle.api.internal.file.collections.SimpleFileCollection;
import org.gradle.api.tasks.JavaExec;
import org.gradle.api.tasks.SourceSet;
@@ -45,15 +44,10 @@ public class RunApp extends DefaultTask {
final Project project = getProject();
final SourceSet main = ComputeMain.findMainSourceSet(project);
final File outputDir = (main == null ? null : main.getOutput().getResourcesDir());
final Set<File> allResources = new LinkedHashSet<File>();
final File outputs;
if (main != null) {
SourceDirectorySet resources = main.getResources();
allResources.addAll(resources.getSrcDirs());
outputs = main.getOutput().getResourcesDir();
} else {
outputs = null;
allResources.addAll(main.getResources().getSrcDirs());
}
project.getTasks().withType(JavaExec.class, new Action<JavaExec>() {
@@ -78,21 +72,25 @@ public class RunApp extends DefaultTask {
});
getLogger().info("Found main: " + mainClass);
}
if (outputs != null) {
// remove duplicates from resources and build
if (outputDir != null) {
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);
}
}
}
removeDuplicatesFromOutputDir(directory, outputDir);
}
}
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);
}
}
}
}
});
}
@@ -104,7 +102,8 @@ 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

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2014 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.
@@ -31,6 +31,7 @@ import org.springframework.boot.loader.jar.JarFile;
* {@link ClassLoader} used by the {@link Launcher}.
*
* @author Phillip Webb
* @author Dave Syer
*/
public class LaunchedURLClassLoader extends URLClassLoader {
@@ -67,31 +68,29 @@ public class LaunchedURLClassLoader extends URLClassLoader {
@Override
public URL findResource(String name) {
if (name.equals("")) {
URL[] urls = getURLs();
if (urls.length > 0) {
return urls[0];
}
}
try {
if (name.equals("") && hasURLs()) {
return getURLs()[0];
}
return super.findResource(name);
}
catch (IllegalArgumentException e) {
catch (IllegalArgumentException ex) {
return null;
}
}
@Override
public Enumeration<URL> findResources(String name) throws IOException {
if (name.equals("")) {
URL[] urls = getURLs();
if (urls.length > 0) {
return Collections.enumeration(Arrays.asList(urls));
}
if (name.equals("") && hasURLs()) {
return Collections.enumeration(Arrays.asList(getURLs()));
}
return super.findResources(name);
}
private boolean hasURLs() {
return getURLs().length > 0;
}
@Override
public Enumeration<URL> getResources(String name) throws IOException {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2014 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.
@@ -25,6 +25,8 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* Tests for {@link LaunchedURLClassLoader}.
*
* @author Dave Syer
*/
public class LaunchedURLClassLoaderTests {

View File

@@ -50,9 +50,6 @@ import org.springframework.boot.loader.tools.MainClassFinder;
@Execute(phase = LifecyclePhase.TEST_COMPILE)
public class RunMojo extends AbstractMojo {
/**
*
*/
private static final String SPRING_LOADED_AGENT_CLASSNAME = "org.springsource.loaded.agent.SpringLoadedAgent";
/**
@@ -201,18 +198,21 @@ public class RunMojo extends AbstractMojo {
for (Resource resource : this.project.getResources()) {
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();
}
}
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()) {
FileUtils.deleteDirectory(targetFile);
}
else {
targetFile.delete();
}
}
}