public SpringBootPluginsClassLoader(final ClassLoader parent) throws IOException { super(new URL[]{}, parent); // find all jar plugins final Collection<File> jarFiles = FileUtils.listFiles(new File("plugins/"), new String[]{"jar"}, false); for(final File jarFile : jarFiles) { // add the jar's own classes to classpath final URL jarURL = new URL("jar:file:" + jarFile.getPath() + "!/BOOT-INF/classes/"); addURL(jarURL); final JarFileArchive jarFileArchive = new JarFileArchive(jarFile); final List<Archive> nestedArchives = jarFileArchive .getNestedArchives(entry -> entry.getName().endsWith(".jar")); for(final Archive archive : nestedArchives) { // add all bundled dependencies to classpath addURL(archive.getUrl()); } } }
protected final Archive createArchive() throws Exception { ProtectionDomain protectionDomain = getClass().getProtectionDomain(); CodeSource codeSource = protectionDomain.getCodeSource(); URI location = (codeSource == null ? null : codeSource.getLocation().toURI()); String path = (location == null ? null : location.getSchemeSpecificPart()); if (path == null) { throw new IllegalStateException("Unable to determine code source archive"); } File root = new File(path); if (!root.exists()) { throw new IllegalStateException( "Unable to determine code source archive from " + root); } return (root.isDirectory() ? new ExplodedArchive(root) : new JarFileArchive(root)); }
private Archive getArchive(File file) throws IOException { String name = file.getName().toLowerCase(); if (name.endsWith(".jar") || name.endsWith(".zip")) { return new JarFileArchive(file); } return null; }
@Test public void archivedWarHasOnlyWebInfClassesAndContentsOfWebInfLibOnClasspath() throws Exception { File warRoot = createWarArchive(); WarLauncher launcher = new WarLauncher(new JarFileArchive(warRoot)); List<Archive> archives = launcher.getClassPathArchives(); assertThat(archives).hasSize(2); assertThat(getUrls(archives)).containsOnly( new URL("jar:" + warRoot.toURI().toURL() + "!/WEB-INF/classes!/"), new URL("jar:" + warRoot.toURI().toURL() + "!/WEB-INF/lib/foo.jar!/")); }
@Override public List<Archive> getNestedArchives(EntryFilter ignored) throws IOException { try { List<Archive> archives = new ArrayList<>(mavenProject.getRuntimeClasspathElements().size()); for (String dep : mavenProject.getRuntimeClasspathElements()) { File file = new File(dep); archives.add(file.isDirectory() ? new ExplodedArchive(file) : new JarFileArchive(file)); } return archives; } catch (DependencyResolutionRequiredException e) { throw new IOException("Could not create boot archive", e); } }
private void addParentClassLoaderEntries(List<Archive> lib) throws IOException, URISyntaxException { ClassLoader parentClassLoader = getClass().getClassLoader(); List<Archive> urls = new ArrayList<Archive>(); for (URL url : getURLs(parentClassLoader)) { if (url.toString().endsWith(".jar") || url.toString().endsWith(".zip")) { urls.add(new JarFileArchive(new File(url.toURI()))); } else if (url.toString().endsWith("/*")) { String name = url.getFile(); File dir = new File(name.substring(0, name.length() - 1)); if (dir.exists()) { urls.add(new ExplodedArchive( new File(name.substring(0, name.length() - 1)), false)); } } else { String filename = URLDecoder.decode(url.getFile(), "UTF-8"); urls.add(new ExplodedArchive(new File(filename))); } } // The parent archive might have a "lib/" directory, meaning we are running from // an executable JAR. We add nested entries from there with low priority (i.e. at // end). addNestedArchivesFromParent(urls); for (Archive archive : urls) { // But only add them if they are not already included if (findArchive(lib, archive) < 0) { lib.add(archive); } } }
@Test public void archivedWarHasOnlyWebInfClassesAndContentsOfWebInfLibOnClasspath() throws Exception { File warRoot = createWarArchive(); WarLauncher launcher = new WarLauncher(new JarFileArchive(warRoot)); List<Archive> archives = launcher.getClassPathArchives(); assertEquals(2, archives.size()); assertThat(getUrls(archives), hasItems(new URL("jar:" + warRoot.toURI().toURL() + "!/WEB-INF/classes!/"), new URL("jar:" + warRoot.toURI().toURL() + "!/WEB-INF/lib/foo.jar!/"))); }
private Archive resolveAsArchive(Resource app) throws IOException { File moduleFile = app.getFile(); return moduleFile.isDirectory() ? new ExplodedArchive(moduleFile) : new JarFileArchive(moduleFile); }